Quantcast

Maximum PC

It is currently Fri Jul 25, 2014 4:07 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: C++ Question: Writing my own sort predicate
PostPosted: Sun Mar 06, 2005 8:46 pm 
Little Foot
Little Foot

Joined: Thu Dec 23, 2004 12:54 pm
Posts: 150
Hi, I need to sort my data, but because of the complication of it, I need to use a sorting predicate. I have an IDEA of how predicates work, but not much more....

My scenarios is:

struct str1 {
vector<string> phrase;
int numpos;
};

struct str2 {
vector<str1> Index;
};

str2 groupA;

I want to sort all the elements in groupA, based on the element "phrase", alphabetically.

any IDEA of what to do would be awesome,
Thanks!


Top
  Profile  
 
 Post subject:
PostPosted: Sun Mar 06, 2005 9:59 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
You want to sort everything in groupA or all the elements in each one of groupA's member's phrase vector?


Top
  Profile  
 
 Post subject:
PostPosted: Mon Mar 07, 2005 2:09 pm 
Little Foot
Little Foot

Joined: Thu Dec 23, 2004 12:54 pm
Posts: 150
I want to sort GroupA.Index, but based on Phrase1 (alphabetically). Thanks!


Top
  Profile  
 
 Post subject:
PostPosted: Mon Mar 07, 2005 3:14 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
Well, you're trying to sort based on a vector. Do you want to sort by the first string in the vector or something?


Top
  Profile  
 
 Post subject:
PostPosted: Mon Mar 07, 2005 5:47 pm 
Little Foot
Little Foot

Joined: Thu Dec 23, 2004 12:54 pm
Posts: 150
good point. actually it's a vector of words, so I'd like to first check the first word, then the second, etc.

If it's too complicated, then checking just the first word is probably enough...

Or maybe I can just merge all the words into one string beforehand??

Still no idea what to do,
Thanks..


Top
  Profile  
 
 Post subject:
PostPosted: Mon Mar 07, 2005 8:23 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
Sure, you could merge all the words into one string. Wouldn't really do much though.

Can you explain the problem you are trying to solve instead of how to do this? It would probably be better if we knew the question.


Top
  Profile  
 
 Post subject:
PostPosted: Mon Mar 07, 2005 9:03 pm 
Little Foot
Little Foot

Joined: Thu Dec 23, 2004 12:54 pm
Posts: 150
...


Last edited by UTjunkie on Wed Mar 09, 2005 8:46 pm, edited 8 times in total.

Top
  Profile  
 
 Post subject:
PostPosted: Mon Mar 07, 2005 9:35 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
Repost it in code tags so I can see the formatting, please.


Top
  Profile  
 
 Post subject:
PostPosted: Mon Mar 07, 2005 10:27 pm 
Little Foot
Little Foot

Joined: Thu Dec 23, 2004 12:54 pm
Posts: 150
okay, i fixed it.

Thanks again!


Top
  Profile  
 
 Post subject:
PostPosted: Tue Mar 08, 2005 4:28 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
Well, first of all, you are most likely not going to get an EOF from stdin, so that input loop is pretty much infinite (if you move to a different stream it may change that, though).

Now, you since you are going to use std::sort for this, #include <algorithm>.
A predicate sort is simply a function that returns a bool and takes two parameters (the two things to compare). So, you want to make a function like:

Code:
bool rotation_pred_sort(const Rotation& T1, const Rotation& T2)

The actual comparison is simple enough:

Code:
bool rotation_pred_sort(const Rotation& T1, const Rotation& T2)
{
    return T1.phrase[0] <= T2.phrase[0] ? 1 : 0;
}



Now, you want to actually do the sorting. This is how we do it:
Code:
std::sort(Strings.begin(), Strings.end(), rotation_pred_sort);


That's all, folks.

If you have any questions, feel free to keep posting them here.


Top
  Profile  
 
 Post subject:
PostPosted: Thu Mar 10, 2005 6:31 am 
Little Foot
Little Foot

Joined: Thu Dec 23, 2004 12:54 pm
Posts: 150
worked great! thank you!


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group