I expected the order of std::set
to be consistent like std::list
or std::vector
, only that a value will not be appended a second time. MSVC 110 prooved me wrong. I expected the following code the yield the result shown below, which is the case when running the code on ideone (http://ideone.com/e47GwQ) (not sure which compiler and version they use, though).
#include <iostream>
#include <set>
template <typename C>
int insert_get_index(C& container, typename C::value_type value)
{
typename C::iterator it = container.insert(value).first;
return std::distance(container.begin(), it);
}
int main()
{
int a = 3, b = 1, c = 9;
std::set<int*> set;
std::cout << insert_get_index(set, &a) << "\n";
std::cout << insert_get_index(set, &b) << "\n";
std::cout << insert_get_index(set, &c) << "\n";
std::cout << insert_get_index(set, &a) << "\n";
std::cout << insert_get_index(set, &b) << "\n";
std::cout << insert_get_index(set, &c) << "\n";
return 0;
}
0
1
2
0
1
2
Running the code in Visual Studio 2012 yields what you can see below.
0
0
2
1
0
2
Now, why did I thin that std::set
behaves like described above? Because cplusplus.com states
Sets are containers that store unique elements following a specific order.
and additionally, there is std::unordered_set
.
- Did I understand the documentation incorrectly and
std::set
is instead value ordered? - And what's
std::unordered_set
for then? - Is there a different standard library container that fulfils my requirements?