Is there a function that I can use that given a few vectors which elements are common to all?

for example
vector<int> a;
vector<int> b;

a.push_back( 1 );
a.push_back( 2 );
a.push_back( 3 );
a.push_back( 4 );

b.push_back( 2 );
b.push_back( 4 );

Something that will give me back the answer of 2 and 4? (The results would be stored in another vector).

What if I have more than 2 vectors to compare - would I somehow call this function against each pair? (ie a vs. b, b vs. c, a vs. c?)

Recommended Answers

All 2 Replies

boost may have one. search that link for the word intersect and see how it is used.

>Is there a function that I can use that given a few vectors which elements are common to all?
Look up set_intersection.

>What if I have more than 2 vectors to compare
Look up set_intersection with a focus on the return value and consider making multiple calls.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.