GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Jul-2006, 12:22
jondean jondean is offline
Awaiting Email Confirmation
 
Join Date: Jun 2006
Posts: 19
jondean is on a distinguished road

Sorting a vector of structures within a structure


Hello,

I successfully sorted a vector of structures but am having trouble doing the same for a vector of structures in a structure...

Here is the code..

CPP / C++ / C Code:

struct Codes{
unsigned char mem1;
unsigned int mem2;
};

struct HT{
Codes htcodes;
vector<Codes> vcodes;     // Vector within a structure
} HTi;

// Sorting predicate for Huffman codes
bool JPG::MyDataSortPredicate(const Codes& lhs, const Codes& rhs)
{
  return lhs.symbol < rhs.symbol;
}


// assume vcodes has been initialized with values....

void JPG::funsort(HT* HTs)
{
std::sort(HTs->vcodes.begin(), HTs->vcodes.end(), MyDataSortPredicate);

}

I get a compile error saying...

"JPG.cpp argument of type `bool (JPG:(const JPG::Codes&, const JPG::Codes&)' does not match `bool (JPG::*)(const JPG::Codes&, const JPG::Codes&)' "

Please help... Jon
  #2  
Old 03-Jul-2006, 14:35
jondean jondean is offline
Awaiting Email Confirmation
 
Join Date: Jun 2006
Posts: 19
jondean is on a distinguished road

Re: Sorting a vector of structures within a structure


Sorry, predicate function should read
CPP / C++ / C Code:
// Sorting predicate for Huffman codes
bool JPG::MyDataSortPredicate(const Codes& lhs, const Codes& rhs)
{
  return lhs.symbol < rhs.symbol;
}
  #3  
Old 03-Jul-2006, 14:37
jondean jondean is offline
Awaiting Email Confirmation
 
Join Date: Jun 2006
Posts: 19
jondean is on a distinguished road

Re: Sorting a vector of structures within a structure


I'm an idiot... sorting predicate function should read...

CPP / C++ / C Code:
// Sorting predicate for Huffman codes
bool JPG::MyDataSortPredicate(const Codes& lhs, const Codes& rhs)
{
  return lhs.mem1 < rhs.mem1;
}

still need help though...
  #4  
Old 03-Jul-2006, 16:15
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 991
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: Sorting a vector of structures within a structure


I wouldn't go the way you have about this problem. However, you can solve the issue as it is now by simply making the MyDataSortPredicate member function static, and thus a valid functor.

Let me know if that doesn't work, I don't have a compiler available.

Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #5  
Old 03-Jul-2006, 16:33
jondean jondean is offline
Awaiting Email Confirmation
 
Join Date: Jun 2006
Posts: 19
jondean is on a distinguished road

Re: Sorting a vector of structures within a structure


Wow. That seemed to work but I don't know why.

And you hinted you would go about my program another way. What way is that?

Thanks for your helpl, Jon
 
 

Recent GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Mapping C Structures: IP_ADAPTER_ADDRESSES to VB Doug Thomas C Programming Language 15 22-Dec-2006 04:55
Trying to make a vector of structures, need help CT++ C++ Forum 4 10-May-2005 17:00
Delete and diplaying probrlem with structures. glulu76 C++ Forum 2 09-May-2005 18:23
[GIM] gim.h dsmith C Programming Language 0 18-Jan-2005 09:48
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 16:13

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:43.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.