R
R
romy42012-04-20 15:33:18
C++ / C#
romy4, 2012-04-20 15:33:18

Container that does not change the order of elements when iterating

let there be a hash container

typedef .... container
/// ...
container<string,string> c;
where the elements are added in this order:
c["Update"] = "...";
c["Connection"] = "..."; 
c["Sec"] = "...";

and there is some iteration on it
for ( auto it : c )
{
    cout << c.first << ": " << c.second <<  endl;
}


Expected:
Update: ...
Connection: ...
Sec: ...


If it were a map, then the order would be:
Connection: ...
Sec: ...
Update: ...


If unordered_map, then just do any order
Sec: ...
Connection: ...
Update: ...


Prompt the container for the required result?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
An, 2012-04-20
@romy4

Look at a similar question
. There are many answers in the answers, partially overlapping with those given here.

I
ixSci, 2012-04-20
@ixSci

Well, you came up with the name. Before entering, I thought some kind of container changes the order during iteration :)
Regarding the question: “Do you want checkers or go?” Do you want to have quick access and keep order? but that won't work. Choose either this or that. If you do not have large volumes, then you can store a vector with the necessary strings and std::hash / map with strings, the values ​​of which will be indices in the vector.

R
Ramires, 2012-04-20
@Ramires

If I understand correctly, you need a FIFO queue. I may be wrong, but look at std::queue (if the elements are extracted one by one) or std::vector, std::list (as far as I remember, the order does not change in them). Already in the queue, you can push your structure with two fields.

A
Anatoly, 2012-04-20
@taliban

Regular array is numeric. 100% order saving;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question