Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Sorting Structure Data within Vector

Reply
Thread Tools

Sorting Structure Data within Vector

 
 
Mike Copeland
Guest
Posts: n/a
 
      03-27-2013
I am trying to convert an array-based application to a vector-based
implementation, mostly because I need to expand the data being processed
beyond a fixed array I've declared (CTSMAX), but also to make use of
higher level structures and techniques.
I've declared a vector (cszVect) that contains sub-structure (CSTYPE)
which contains the data key to be sorted.
When I coded a sort call (sort(cszVect.begin(), cszVect.end()) I
get many compiler errors informing me I don't have a comparison operator
for the data key (csKey) that's in one of the substructures.
I've tried placing the comparison operator code in both the structures,
one at a time - the compiler doesn't like either.
Can I sort a structure on data that exists within an inner
structure? If so, how? TIA

struct CSTYPE
{
string csKey; // City/State "Key"
string csString // City & State data
// bool operator <(const CSTYPE &rhs) const
// {
// return csKey < rhs.csKey;
// }
} ;
struct ZIPTYPE
{ // ZIP codes for City
long loZIP, hiZIP; // ZIP Code ranges
int zipCount; // Usage Count of City/State
} ;
struct CSZTYPE
{ // combined City, State, ZIP info for Entry
CSTYPE csData;
// bool operator <(const CSTYPE &rhs) const
// {
// return csData.csKey < rhs.csKey;
// }
ZIPTYPE zipData;
} ;
typedef vector<CSZTYPE> CSZINFO;
CSZINFO cszVect; // City, State, ZIP data storage
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-27-2013
On 3/27/2013 3:20 PM, Mike Copeland wrote:
> I am trying to convert an array-based application to a vector-based
> implementation, mostly because I need to expand the data being processed
> beyond a fixed array I've declared (CTSMAX), but also to make use of
> higher level structures and techniques.
> I've declared a vector (cszVect) that contains sub-structure (CSTYPE)
> which contains the data key to be sorted.
> When I coded a sort call (sort(cszVect.begin(), cszVect.end()) I
> get many compiler errors informing me I don't have a comparison operator
> for the data key (csKey) that's in one of the substructures.
> I've tried placing the comparison operator code in both the structures,
> one at a time - the compiler doesn't like either.
> Can I sort a structure on data that exists within an inner
> structure? If so, how? TIA


Consider providing your own comparison functor. It can be a function
that takes two arguments, it could be a class that has operator()
defined (and it would take two arguments), it can be a lambda expression
right where you call 'sort' (if your compiler supports it).

There are many examples of this on the net. Find one and try to
understand how it works. If you fail to do that, post more questions.

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting list vs sorting vector boltar2003@boltar.world C++ 2 07-06-2010 09:40 AM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
selection structure nested within another selection structure 4Ankit@gmail.com Javascript 1 12-07-2006 11:47 AM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
After sorting a vector, how to get max in this vector? cylin C++ 3 12-19-2003 05:58 PM



Advertisments