Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

0 views
Skip to first unread message

silverbu...@gmail.com

unread,
Oct 28, 2008, 6:40:36 PM10/28/08
to
Hi,

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

Thank you.

Sam

unread,
Oct 28, 2008, 6:49:50 PM10/28/08
to
silverbu...@gmail.com writes:

> 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())


Juha Nieminen

unread,
Oct 28, 2008, 7:03:56 PM10/28/08
to
silverbu...@gmail.com wrote:
> Is there a STL algorithm to compare if 2 vector<int> have same values?

std::equal() is probably the function you are looking for.

Kai-Uwe Bux

unread,
Oct 28, 2008, 7:14:59 PM10/28/08
to
silverbu...@gmail.com wrote:

> 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

Salt_Peter

unread,
Oct 29, 2008, 3:38:41 AM10/29/08
to
On Oct 28, 5:40 pm, "silverburgh.me...@gmail.com"

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.

James Kanze

unread,
Oct 29, 2008, 5:35:38 AM10/29/08
to
On Oct 28, 11:49 pm, Sam <s...@email-scan.com> wrote:
> silverburgh.me...@gmail.com writes:

> > 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

0 new messages