'iterating over an unordered_set
I need help iterating over an unordered map in C++. I am trying to put the elements of the set into an array so that I can sort the array.
for(auto it=s.begin();it!=s.end();it++){
a[i]=*it;
i++;
}
Solution 1:[1]
Your are using many differen terms here.
- unordered_set
- unordered_map
- set
- array
So, it is a little bit unclear what you really want to do.
If you have a std::unordered_setand want to put the data into a std::set, then you can simply use its range constructor and write something like std::set<int> ordered(s.begin(),s.end());
The same would work with a std::vector which has also a range constructor. Arrays, either C-style or C++ std::array are more complicated, because you need to know the size of the original data in advance. Because: Arrays have a compile time definde fixed size.
For sorting std::maps or std::unordered_maps according to their "value" and not the key, you need to use a std::multiset with a special sorting functor or lambda. If you edit your question and give more details, then I will provide source code to you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Armin Montigny |
