Sorting Structure Data within Vector

M

Mike Copeland

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
 
V

Victor Bazarov

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top