I’m searching for a set container in C++. I want something where I could add elements but they would not repeat more than once and search in that collection would be O(1). What is current defacto cross-compiler container for this now.
I seen some in boost (like mpl) and there is one in future c++ standards, but what is best to use now and here?
EDIT
Example of storing vector in boost::unordered_set container. So for me it seems to fit my need pretty well but I will have a lot of data in it so if somebody sees some potential bug right away can you comment on what can go wrong. Again, all elements will be of sorted vector with no pointers.
vector<string> values1;
values1.push_back("aaa");
values1.push_back("bbb");
values1.push_back("ccc");
vector<string> values2;
values2.push_back("aa");
values2.push_back("bbb");
values2.push_back("ccc");
vector<string> values3;
values3.push_back("aaa");
values3.push_back("bbb");
vector<string> values4;
values4.push_back("aaa");
values4.push_back("bbb");
values4.push_back("ccc");
values4.push_back("ddd");
vector<string> values5;
values5.push_back("aaa");
values5.push_back("bbb");
values5.push_back("ccc");
vector<string> values6;
values6.push_back("aaa");
values6.push_back("bbb");
values6.push_back("ccc");
values6.push_back("ddd");
boost::unordered_set<vector<string> > collection;
collection.insert(values1); // 1
cout << collection.size() << endl;
collection.insert(values2); // 2
cout << collection.size() << endl;
collection.insert(values3); // 3
cout << collection.size() << endl;
collection.insert(values4); // 4
cout << collection.size() << endl;
collection.insert(values5); // 4
cout << collection.size() << endl;
collection.insert(values6); // 4
cout << collection.size() << endl;
You can use std::unordered_set if you have a C++0x compliant compiler that supports it.
If you are not in that situation, intercepts are available in Microsoft VC++ as stdext::hash_set, or in general using boost::unordered_set. The latter is your best bet for portability at present, pending wider C++0x availability. As noted in the comments by @Nemo, there is widespread support for
std::tr1::unordered_setas well, as an alternative to Boost usage.[
std::setwill be O(log n), as it’s based on a search tree. To get O(1), you need to use a hashtable-based container, with due regard to efficient implementation of the hash function for your member objects.]