Reputation: 6053
I am new to hash and not sure how to do this in C++. In java, we have functions like ContainsKey, put, get etc for Hash. Is there anything similar in C++? Thanks.
Upvotes: 1
Views: 893
Reputation: 106236
You could start with std::set<>
, which is a balanced binary tree. Most recent compilers also provide unordered_set<>
, which is a hash table but not part of C++03: it will be part of C++0x. The boost library also has a hash set implementation.
For std::set<>, see http://www.cplusplus.com/reference/stl/set/
e.g.
std::set<int> s;
for (int i = 0; i < first_vector.size(); ++i)
s.insert(first_vector[i]);
for (int i = 0; i < second_vector.size(); ++i)
if (s.find(second_vector[i]) != s.end())
do_something();
Upvotes: 2
Reputation: 283803
You're probably wanting the unordered_set
class. It's part of TR1 and standardized in C++0x, older compilers can find an implementation in the boost library.
Upvotes: 1