Sorting Structure Data within Vector

Discussion in 'C++' started by Mike Copeland, Mar 27, 2013.

  1. 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
     
    Mike Copeland, Mar 27, 2013
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Mar 27, 2013
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cylin
    Replies:
    3
    Views:
    356
    Cy Edmunds
    Dec 19, 2003
  2. pmatos
    Replies:
    6
    Views:
    23,961
  3. Replies:
    8
    Views:
    1,970
    Csaba
    Feb 18, 2006
  4. Replies:
    2
    Views:
    1,464
    James Kanze
    Jul 6, 2010
  5. Replies:
    1
    Views:
    218
Loading...

Share This Page