By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,661 Members | 779 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,661 IT Pros & Developers. It's quick & easy.

stl questions: how can I compare 2 stl list?

P: n/a
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[i] != destList[i]) {
return false;
}
}

return true;
}

Thank you.

Apr 16 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
si***************@gmail.com wrote:
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[i] != destList[i]) {
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
--
jb

(reply address in rot13, unscramble first)
Apr 16 '06 #2

P: n/a
Hello

AFAIK, lists implement '==' operator.

So just test as:

bool isSame = list1 == list2;

Also check the std::equal()

--
Elias
<si***************@gmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
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[i] != destList[i]) {
return false;
}
}

return true;
}

Thank you.

Apr 16 '06 #3

P: n/a

si***************@gmail.com 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[i] != destList[i]) {
return false;
}
}

return true;
}

Thank you.


How about list1 == list2?

/Peter

Apr 16 '06 #4

P: n/a

si***************@gmail.com wrote:
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[i] != destList[i]) {
return false;
}
}

return true;
}

Thank you.

Hello,

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

Apr 16 '06 #5

P: n/a
si***************@gmail.com wrote:
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[i] != destList[i]) {
return false;
}
}

return true;
}

Thank you.


What's wrong with

srcList == destList

?

Apr 16 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.