Ava
Ava

Reputation: 6053

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.

Upvotes: 1

Views: 893

Answers (3)

Richard
Richard

Reputation: 15592

use std::map, you can do similar things as HashMap in java

Upvotes: 0

Tony Delroy
Tony Delroy

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

Ben Voigt
Ben Voigt

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

Related Questions