How can I find the intersection of two arrays using Hash functions in C++?
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++? Than开发者_如何学JAVAks.
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();
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.
use std::map
, you can do similar things as HashMap
in java
精彩评论