C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

can I compare 2 vectors by using ==?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
kathy
Guest





PostPosted: Tue Feb 28, 2006 12:06 am    Post subject: can I compare 2 vectors by using ==? Reply with quote



I want to compare 2 vectors to see if all element are equal or not:

std::vector <int> v1;
std::vector <int> v2;

v1.push_back(1);
v1.push_back(2);
v1.push_back(3);

v2.push_back(1);
v2.push_back(2);
v2.push_back(2);

if(v1 == v2)
{
// means every item in v1 is equal to every item in v2
}
else
{

}
Back to top
Victor Bazarov
Guest





PostPosted: Tue Feb 28, 2006 12:06 am    Post subject: Re: can I compare 2 vectors by using ==? Reply with quote



kathy wrote:
Quote:
I want to compare 2 vectors to see if all element are equal or not:


Why ask when you can simply try?

V
--
Please remove capital As from my address when replying by mail
Back to top
Daniel T.
Guest





PostPosted: Tue Feb 28, 2006 2:06 am    Post subject: Re: can I compare 2 vectors by using ==? Reply with quote



In article <31MMf.1559$a97.48 (AT) newsread1 (DOT) mlpsca01.us.to.verio.net>,
Victor Bazarov <v.Abazarov (AT) comAcast (DOT) net> wrote:

Quote:
kathy wrote:
I want to compare 2 vectors to see if all element are equal or not:


Why ask when you can simply try?

Kathy is doing the right thing. Simply trying it isn't good enough. Just
because it works on her compiler doesn't mean the standard guarantees it.


--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Back to top
hde
Guest





PostPosted: Tue Feb 28, 2006 2:06 am    Post subject: Re: can I compare 2 vectors by using ==? Reply with quote

Run this and you shall see....... >:-}

#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;

int main() {
vector<int> x;
vector<int> y;

x.push_back(1);
x.push_back(2);
x.push_back(3);
x.push_back(4);

y.push_back(1);
y.push_back(2);
y.push_back(3);
y.push_back(4);

if (x == y)
cout << "x == y" << endl;
else
cout << "x != y" << endl;


x.push_back(4);

if (x == y)
cout << "x == y" << endl;
else
cout << "x != y" << endl;

vector<string>a;
vector<string>b;

a.push_back("Foo");
a.push_back("Bar");
a.push_back("Baz");
a.push_back("Quxx");
a.push_back("Quxxx");

b.push_back("Foo");
b.push_back("Bar");
b.push_back("Baz");
b.push_back("Quxx");
b.push_back("Quxxx");

if (a == b)
cout << "a == b" << endl;
else
cout << "a != b" << endl;

b.push_back("Blah");
if (a == b)
cout << "a == b" << endl;
else
cout << "a != b" << endl;

return 0;
}
Back to top
Guest






PostPosted: Tue Feb 28, 2006 2:06 am    Post subject: Re: can I compare 2 vectors by using ==? Reply with quote

vector<int> is can.
but if the data type is you define ,you must overload operation ==.
Back to top
Daniel T.
Guest





PostPosted: Tue Feb 28, 2006 2:06 am    Post subject: Re: can I compare 2 vectors by using ==? Reply with quote

In article <1141082503.383342.266510 (AT) i39g2000cwa (DOT) googlegroups.com>,
"kathy" <yqin_99 (AT) yahoo (DOT) com> wrote:

Quote:
I want to compare 2 vectors to see if all element are equal or not:

std::vector <int> v1;
std::vector <int> v2;

v1.push_back(1);
v1.push_back(2);
v1.push_back(3);

v2.push_back(1);
v2.push_back(2);
v2.push_back(2);

if(v1 == v2)
{
// means every item in v1 is equal to every item in v2
}
else
{

}

For future reference: <http://www.sgi.com/tech/stl/>

op== tests two vectors for equality. IE the two vectors have the same
size and running "std::equal" over both ranges returns true.


--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.