LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   std::list question: copy, sort and Merge.... (https://www.linuxquestions.org/questions/programming-9/std-list-question-copy-sort-and-merge-888190/)

ArthurHuang 06-24-2011 03:39 PM

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!

graemef 06-26-2011 10:18 AM

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).


All times are GMT -5. The time now is 01:17 PM.