LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-24-2011, 03:39 PM   #1
ArthurHuang
Member
 
Registered: Jan 2006
Posts: 174

Rep: Reputation: 30
std::list question: copy, sort and Merge....


Hi, I had a data structure like this:

typedef struct node
{
int id;
int grade;
string name;
}

....
list<node> *node_list1, *node_list2, *node_list3;
....

I sort the list like this:
bool compareID(node *n1, node *n2)
{
return true?n1->id>n2->id:false;
}
node_list1.sort(compareID);
I think the logic is correct, maybe just some details.
Now my question is
1) Can I simply merge node1 and 2 without sorting?
Like node_list1.merge(node_list2);
2) If not, how can I copy list1 and 2 into list3?

thanks!
 
Old 06-26-2011, 10:18 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
the compareID function is a trifle odd. Typically the ?: operator is as follows
Code:
condition ? value if true : value if false
Thus the condition will always be true

It's not possible to give a reasonable answer to your question without knowing how you want the lists to be stored. Should they be sorted after the merge? Are the already sorted before?

If they are sorted before and you want them to be sorted after then look up the details of merge sort (google can help).
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
STD sort with g++ -O3 segmentation fault janinko Programming 1 01-27-2010 05:55 PM
merge sort for mpich chandan_shetty Programming 4 02-20-2008 10:20 PM
A question about std::list container shreks Programming 8 02-09-2006 04:08 PM
how to sort std::vector<double>? markhod Programming 4 10-12-2005 04:21 AM
C++, using custom function to sort an std::list R00ts Programming 3 01-03-2005 09:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration