| View previous topic :: View next topic |
| Author |
Message |
kathy Guest
|
Posted: Tue Feb 28, 2006 12:06 am Post subject: can I compare 2 vectors by using ==? |
|
|
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
|
Posted: Tue Feb 28, 2006 12:06 am Post subject: Re: can I compare 2 vectors by using ==? |
|
|
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
|
Posted: Tue Feb 28, 2006 2:06 am Post subject: Re: can I compare 2 vectors by using ==? |
|
|
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
|
Posted: Tue Feb 28, 2006 2:06 am Post subject: Re: can I compare 2 vectors by using ==? |
|
|
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
|
Posted: Tue Feb 28, 2006 2:06 am Post subject: Re: can I compare 2 vectors by using ==? |
|
|
vector<int> is can.
but if the data type is you define ,you must overload operation ==. |
|
| Back to top |
|
 |
Daniel T. Guest
|
Posted: Tue Feb 28, 2006 2:06 am Post subject: Re: can I compare 2 vectors by using ==? |
|
|
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 |
|
 |
|