Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

I want to use STL sort with a functor

Posted on 2000-05-10
8
Medium Priority
?
222 Views
Last Modified: 2010-05-18
//
//  This compiles and runs if I comment out the
//  call to sort().
//  I would like to sort on key.  But, I have no clue
//  where to start on the syntax.
//
#include <vector>
#include <algorithm>
#include <iostream>
class keyPLUS {
  public:
     int key;
     int something_else;
} ;

int main() {

  vector<keyPLUS> KeyContainer(2);

  KeyContainer[0].key = 5;
  KeyContainer[0].something_else = 13;

  KeyContainer[1].key = 4;
  KeyContainer[1].something_else = 17;
 
  sort( &KeyContainer[0], &KeyContainer[2],
      what_do_I_put_here );
 
  cout << " [0].key =" << KeyContainer[0].key
       << " [0].something_else =" << KeyContainer[0].something_else << endl ;
  cout << " [1].key =" << KeyContainer[1].key
       << " [1].something_else =" << KeyContainer[1].something_else << endl ;
}

0
Comment
Question by:klopter
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 

Expert Comment

by:fjfp
ID: 2798522
If you look at the prototype for the template function sort in <algorithm>, you'll see that it takes 2 iterators.  It expects the beginning and end of the vector (in this case).

Try:
sort(KeyContainer.begin(), KeyContainer.end());

Hope it helps.
Frank
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 2799367
> sort(KeyContainer.begin(), KeyContainer.end());

No, that will only work if you have a less than operator defined on the object, which you don't in this case.  You have a couple of options:

i) define a < operator

class keyPLUS {
  public:
     int key;
     int something_else;
     bool operator<(const keyPlus& rhs)
     { return key < rhs.key; // or whatever it should be }
} ;

and then the sort call given above will work (you could also define a non-member function less than operator).

or

ii) define a functor, as you asked originally, the best way is to use a function object:

class keyPlusLessThan
{
   bool operator()(const keyPlus& lhs, const keyPlus& rhs)
   { return lhs.key < rhs.key;  // ? }
};

then the sort call becomes:

sort(keyContainer.begin(), keyContainer.end(), keyPlusLessThan());

you can also do it with a pointer to a function if you want,  but the function object is probably better.  Let me know if you want to see an example with a function pointer.
0
 

Author Comment

by:klopter
ID: 2800157
Jason is right.  Sort( begin, end ) only works if you have defined operator <.

Ken
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:jasonclarke
ID: 2800247
so, is my answer good enough?

or do you need more/different help?
0
 
LVL 9

Accepted Solution

by:
jasonclarke earned 600 total points
ID: 2803669
assuming my comment above is sufficient.
0
 

Author Comment

by:klopter
ID: 2804675
Jason,
  I meant to accept your answer.  My excuse is that there was a bit of a delay in getting my rejection accepted (busy network I guess) and then I forgot.

Thanks,
  Ken
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 2804688
No problem, It's sometimes difficult to know what to do around here.  Sometimes completely inappropriate answers get accepted, just because they are answers  and somebody else has added the answer in a comment.

So it seems better to get an answer in if you think it is appropriate...
0
 

Author Comment

by:klopter
ID: 2805530
Makes sense to me.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question