[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

STL algorithm to compare if 2 vector have same value?

silverburgh.meryl@gmail.com

10/28/2008 10:41:00 PM

Hi,

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

Thank you.
5 Answers

Sam

10/28/2008 10:50:00 PM

0

silverburgh.meryl@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

10/28/2008 11:04:00 PM

0

silverburgh.meryl@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

10/28/2008 11:15:00 PM

0

silverburgh.meryl@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

10/29/2008 7:39:00 AM

0

On Oct 28, 5:40 pm, "silverburgh.me...@gmail.com"
<silverburgh.me...@gmail.com> wrote:
> 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.

James Kanze

10/29/2008 9:36:00 AM

0

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.kanze@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