problem related to the sort function provided by STL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rachel Forder

    problem related to the sort function provided by STL

    Hi All,

    I have a problem related to the sort function provided by STL.

    class A{
    A(string, string, int);
    string itemA;
    string itemB;
    int itemC;
    };

    class B{
    vector<A*> vecData;
    vector<int> itemASorted;//keeps vecData indexes into sorted form
    for itemA
    vector<int> itemBSorted;//keeps vecData indexes into sorted form
    for temA
    vector<int> itemCSorted;//keeps vecData indexes into sorted form
    for itemA
    };

    In my program, i fill my vecData with all the data avalible.
    then i need to create the 3 sorted vectors where the indexes into
    vecData is kept in sorted form according to the item.

    i am having problem in defining compare function for the 3rd argument
    of STL sort function

    sort(itemASorte d.begin(), itemASorted.end (), cmpFunc);

    approach 1:
    make cmpFunc as static member of class B,
    but then it won't be able to access vecData variable which is
    not static.
    approach 2:
    overload operator(),
    but it can be done only for one item...whereas i need a vector
    sorted for all the 3 items

    any guesses how can i perform sorting of my items?

    thanks for any help!!
  • Ivan Vecerina

    #2
    Re: problem related to the sort function provided by STL

    "Rachel Forder" <mickyuser00@ya hoo.com> wrote in message
    news:191927d3.0 310120821.43649 362@posting.goo gle.com...
    ....[color=blue]
    > class A{
    > A(string, string, int);
    > string itemA;
    > string itemB;
    > int itemC;
    > };
    >
    > class B{
    > vector<A*> vecData;
    > vector<int> itemASorted;//keeps vecData indexes into sorted form
    > for itemA
    > vector<int> itemBSorted;//keeps vecData indexes into sorted form
    > for temA
    > vector<int> itemCSorted;//keeps vecData indexes into sorted form
    > for itemA
    > };
    >
    > In my program, i fill my vecData with all the data avalible.
    > then i need to create the 3 sorted vectors where the indexes into
    > vecData is kept in sorted form according to the item.
    >
    > i am having problem in defining compare function for the 3rd argument
    > of STL sort function
    >
    > sort(itemASorte d.begin(), itemASorted.end (), cmpFunc);[/color]
    ....[color=blue]
    > any guesses how can i perform sorting of my items?[/color]

    You can use an auxillary class, one for each ordering criterion:

    // class for sorting by itemA member of class A
    class OrderClassAItem A
    {
    vector<A*>* data_;
    public:
    OrderClassAItem A(vector<A*>& data) : data_(&data) {}
    void operator()(int a, int b)
    {
    return data_->at(a)->itemA < data_->at(b)->itemA;
    }
    };

    Which is then used as follows from a method of class B:
    sort( itemASorted.beg in(), itemASorted.end ()
    , OrderClassAItem A(vecData) );

    One such class can be written for each sort order...


    I hope this helps,
    Ivan
    --
    Ivan Vecerina - expert in medical devices, software - info, links, contact information, code snippets

    http://www.brainbench.com <> Brainbench MVP for C++


    Comment

    • Rachel Forder

      #3
      Re: problem related to the sort function provided by STL

      thanks Ivan,
      it worked.

      Rachel
      [color=blue]
      > You can use an auxillary class, one for each ordering criterion:
      >
      > // class for sorting by itemA member of class A
      > class OrderClassAItem A
      > {
      > vector<A*>* data_;
      > public:
      > OrderClassAItem A(vector<A*>& data) : data_(&data) {}
      > void operator()(int a, int b)
      > {
      > return data_->at(a)->itemA < data_->at(b)->itemA;
      > }
      > };
      >
      > Which is then used as follows from a method of class B:
      > sort( itemASorted.beg in(), itemASorted.end ()
      > , OrderClassAItem A(vecData) );
      >
      > One such class can be written for each sort order...
      >
      >
      > I hope this helps,
      > Ivan[/color]

      Comment

      Working...