Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is currently most widely used set collection in C++

Tags:

c++

containers

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;
like image 258
Sergej Andrejev Avatar asked Sep 01 '25 10:09

Sergej Andrejev


2 Answers

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_set as well, as an alternative to Boost usage.

[std::set will 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.]

like image 197
Steve Townsend Avatar answered Sep 03 '25 22:09

Steve Townsend


C++03: boost::unordered_set

C++0x: std::unordered_set

Older implementations (stdext::hash_set in VC++) are not cross-compilers.

Note: boost::unordered_set interface has been reused for std::unordered_set, so the migration is easy too

edit: interesting addition ==> if performance is a worry and you want a quick test for the absence, you might be interested in looking up Bloom Filters.

like image 27
Matthieu M. Avatar answered Sep 03 '25 22:09

Matthieu M.