473,770 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13024
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.goo glegroups.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#op erator=="

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
17433
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 give interviewees seems to trip them up all the time, and I wonder if I'm being too much of a hardass... it seems easy enough to ME, but these guys, when I get them up to the whiteboard, seem to get really confused. The exercise is this:
8
16604
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
3347
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 speed. I like books with practical exercises, and also with test questions (like cert books) *2*
4
2055
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 and the second is that I couldn't find a characterreading unbuffered function. Also I couldn't compare the cursorpositions in files. And the last thing is that I couldn't insert text in a file(or delete
23
27111
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 I should be prepared for? It's going to be on VB.Net and connectivity with SQL Server perhaps.. any help would be appreciated. Thank you Rick
3
2300
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 there a way to compare the new input point x,y without having to a loop and compare the whole object in the array list. I can't use the indexof method because it would compare the whole object. Public Structure
4
1533
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
3183
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 element of list that are even or a compare function to analyze odd element. example: void f (compare )
0
1254
by: bruce | last post by:
valid point...!! here's the test python.. ugly as it is!! Lodge It New All About ? Paste #83093
0
9619
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.