stl questions: how can I compare 2 stl list?

S

silverburgh.meryl

is there a better way to compare 2 stl list? I write a function like
this below, but I wonder if there is a better way to achieve that (e.g.
less code)?


bool isSame(const list<int>& srcList, const list<int>& destList ) {
if (srcList.size() != destList.size()) {
return false;
}

int size = srcList.size();
for (int i = 0; i < size; i++) {
if (srcList != destList) {
return false;
}
}

return true;
}

Thank you.
 
J

Jakob Bieling

is there a better way to compare 2 stl list? I write a function like
this below, but I wonder if there is a better way to achieve that
(e.g. less code)?


bool isSame(const list<int>& srcList, const list<int>& destList ) {
if (srcList.size() != destList.size()) {
return false;
}

int size = srcList.size();
for (int i = 0; i < size; i++) {
if (srcList != destList) {
return false;
}
}

return true;
}


I suppose you did not compile this, did you? Try it .. it will fail
to compile, because std::list does not have an 'operator []'.

Try this instead:

bool isSame (std::list <int> const& l1, std::list <int> const& l2)
{
if (l1.size () == l2.size ())
return std::equal (l1.begin (), l1.end (), l2.begin ());
else
return false;
}

hth
 
L

lallous

Hello

AFAIK, lists implement '==' operator.

So just test as:

bool isSame = list1 == list2;

Also check the std::equal()
 
P

peter koch

(e-mail address removed) skrev:
is there a better way to compare 2 stl list? I write a function like
this below, but I wonder if there is a better way to achieve that (e.g.
less code)?


bool isSame(const list<int>& srcList, const list<int>& destList ) {
if (srcList.size() != destList.size()) {
return false;
}

int size = srcList.size();
for (int i = 0; i < size; i++) {
if (srcList != destList) {
return false;
}
}

return true;
}

Thank you.


How about list1 == list2?

/Peter
 
D

diligent.snail

is there a better way to compare 2 stl list? I write a function like
this below, but I wonder if there is a better way to achieve that (e.g.
less code)?


bool isSame(const list<int>& srcList, const list<int>& destList ) {
if (srcList.size() != destList.size()) {
return false;
}

int size = srcList.size();
for (int i = 0; i < size; i++) {
if (srcList != destList) {
return false;
}
}

return true;
}

Thank you.



Hello,

How about operator ==() ?
See
"http://www.dinkumware.com/manuals/reader.aspx?b=p/&h=list.html#operator=="
 
M

Markus Schoder

is there a better way to compare 2 stl list? I write a function like
this below, but I wonder if there is a better way to achieve that (e.g.
less code)?


bool isSame(const list<int>& srcList, const list<int>& destList ) {
if (srcList.size() != destList.size()) {
return false;
}

int size = srcList.size();
for (int i = 0; i < size; i++) {
if (srcList != destList) {
return false;
}
}

return true;
}

Thank you.


What's wrong with

srcList == destList

?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top