开发者

Vector values not in inserting order

开发者 https://www.devze.com 2023-02-06 21:32 出处:网络
I\'m inserting values into a vector: vector<map<vector<string> , vector<string> > > listedParameterMap;

I'm inserting values into a vector:

vector<map<vector<string> , vector<string> > > listedParameterMap;

like this:

listedParameterMap.insert(listedParameterMap.end(), 1, parameterMap);

If I check later the vector, the order is inverted. Did I miss something?

EDIT:

I think it may be somewhere else... I just got an idea. But I'll try tomorrow. I almost had today aleady at least one brain-stackoverflow with my code ;)

Anyway thank y开发者_如何学编程ou all for helping. I'll tell if somthing changes!

EDIT2:

Seems like the error is somewhere else. I just could see where it happens, but until now not why. On a point where the map should get filled just once, it gets filled twice. For some reason each time only on a specific value. That's why it was looking like the values are inverted.

If I need some help I'll open a new question. Thanks to everyone!


UPDATE:

The vector is ok now. Found the problem. Blindness of own code ;)

It doen't influence my programm my the map is inverted.

I just created a new map inserting and then printing the values, this works then as expected.


Why don't you simply write:

listedParameterMap.push_back(parameterMap);

It's simpler interface than what you're doing in your post.


I can't make much sense of your sample code, but if you want to insert items into a vector in order, you usually want to use push_back. For a trivial example:

std::vector<int> numbers;
for (int i=0; i<10; i++)
    numbers.push_back(i);

The numbers should now be in order, like: (1 2 3 4 5 6 7 8 9 10).


I really don't think you want to do a map that uses a vector as a key, do you? This smells like you have more significant design problems.

As for adding stuff to vectors in a known (and presered order), use push_back().

0

精彩评论

暂无评论...
验证码 换一张
取 消