Finding 2 lists if there is equal element?

A

Allerdyce.John

Hi,

I have 2 list of class A, is there a STL algorithm which find out if
there is equal element between them (using == operator of A)?

Thank you.
 
N

Neil Cerutti

Hi,

I have 2 list of class A, is there a STL algorithm which find out if
there is equal element between them (using == operator of A)?

Thank you.

std::sort the lists and use std::set_intersection to create a
list of common elements.
 
A

Andrew Koenig

I have 2 list of class A, is there a STL algorithm which find out if
there is equal element between them (using == operator of A)?

If the only comparison operator available is ==, then anything you do is
going to be slow because you have to compare all pairs of elements from the
two lists. You can use the find_first_of algorithm to do those comparisons.

If you have an order relation such as < available, there are other
algorithms, typically involving sorting, that will answer your question more
quickly on average. If you tell us what operations are available, we may be
able to suggest a strategy.
 
M

Michiel.Salters

Neil said:
std::sort the lists and use std::set_intersection to create a
list of common elements.

That assumes an operator<. You could also assume a function int
hash(A);
that could reduce the complexity to O(1).

HTH,
Michiel Salters
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top