473,405 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

stl questions: how can I compare 2 stl list?

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
5 12991
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
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

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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

54
by: Spammay Blockay | last post by:
I've been tasked with doing technical interviews at my company, and I have generally ask a range of OO, Java, and "good programming technique" concepts. However, one of my favorite exercises I...
8
by: laniik | last post by:
Hi. I have a problem using STL's built in sort that seems impossible to get around. if i have: -------------------------------- struct object { int val; }
20
by: Olav.NET | last post by:
I am a .NET/C++ developer who is supposed to do some work with Access. I do not know much about it except for the DB part. Questions: *1* I am looking for INTENSIVE books to get quickly up to...
4
by: Erik Paul | last post by:
I got some maybe stupid questions about file output and functions. At first I have to say I'm programming on Linux. The first thing is I couldn't find a function which can tell me microseconds...
23
by: Rick | last post by:
Hi, I'm currently preparing for an interview on VB.Net Development.. could someone please give me an idea as to what type of questions can one ask (it's a practical test) and what sort of things...
3
by: Sam | last post by:
Hi Everyone, I have a stucture below stored in an arraylist and I want to check user's input (point x,y) to make sure there is no duplicate point x,y entered (string label can be duplicated). Is...
4
by: Schüle Daniel | last post by:
Hello, first question In : cmp("ABC",) Out: 1 against what part of the list is the string "ABC" compared? second question
5
by: antani | last post by:
I need to implement a function with a argument that is a compare function. This compare function must be several for every necessity. For example , I would like a compare function to analyze...
0
by: bruce | last post by:
valid point...!! here's the test python.. ugly as it is!! Lodge It New All About ? Paste #83093
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.