简体   繁体   中英

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++? Thanks.

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/

eg

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.

使用std::map ,您可以执行与Java中的HashMap类似的操作

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM