Is there a STL algorithm to compare if 2 vector<int> have same values?
Thank you.
> Hi,
>
> Is there a STL algorithm to compare if 2 vector<int> have same values?
a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin())
std::equal() is probably the function you are looking for.
> 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
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.
> > Is there a STL algorithm to compare if 2 vector<int> have
> > same values?
> 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.
--
James Kanze (GABI Software) email:james...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34