Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > stl questions: how can I compare 2 stl list?

Reply
Thread Tools

stl questions: how can I compare 2 stl list?

 
 
silverburgh.meryl@gmail.com
Guest
Posts: n/a
 
      04-16-2006
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.

 
Reply With Quote
 
 
 
 
Jakob Bieling
Guest
Posts: n/a
 
      04-16-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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)


 
Reply With Quote
 
 
 
 
lallous
Guest
Posts: n/a
 
      04-16-2006
Hello

AFAIK, lists implement '==' operator.

So just test as:

bool isSame = list1 == list2;

Also check the std::equal()

--
Elias
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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.
>



 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      04-16-2006

(E-Mail 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[i] != destList[i]) {
> return false;
> }
> }
>
> return true;
> }
>
> Thank you.


How about list1 == list2?

/Peter

 
Reply With Quote
 
diligent.snail@gmail.com
Guest
Posts: n/a
 
      04-16-2006

(E-Mail Removed) 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=="

 
Reply With Quote
 
Markus Schoder
Guest
Posts: n/a
 
      04-16-2006
(E-Mail Removed) 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

?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
STL map compare function - can I pass a parameter? Martoon C++ 9 04-20-2006 12:11 PM
Re: Fast way to compare two files using STL? Thomas Matthews C++ 1 08-18-2003 04:28 PM
Re: Fast way to compare two files using STL? Gianni Mariani C++ 1 08-16-2003 11:34 AM
Re: Fast way to compare two files using STL? Kevin Goodsell C++ 0 08-16-2003 01:04 AM



Advertisments