STL algorithm to compare if 2 vector<int> have same value?

S

silverburgh.meryl

Hi,

Is there a STL algorithm to compare if 2 vector<int> have same values?

Thank you.
 
K

Kai-Uwe Bux

Hi,

Is there a STL algorithm to compare if 2 vector<int> have same values?

Assuming

vector<int> a;
vector<int> b;

and some code that fills them, what is wrong with

a == b


Best

Kai-Uwe Bux
 
S

Salt_Peter

Hi,

Is there a STL algorithm to compare if 2 vector<int> have same values?

Thank you.

That depends what you mean by 'same values'. operator== will do the
job if the two containers are identical (also same size(), capacity()
is ignored). If the containers have the same values but in a different
order then op== returns false.
 
J

James Kanze

(e-mail address removed) writes:
a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin())

Which is exactly what a == b would do. And which tests if the
two vector have the same values IN THE SAME ORDER.

The simplest way to see if they have the same values is to sort
them first, then compare. For types other than int, though,
this may mean that you have to define a possibly arbitrary
ordering relationship.
 

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,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top