i have a class MyStrVec which contains a vector of class MyString.
i want to use the std::sort function on that vector and wanna sort all vector members by the int count variable in MyString class. how will i do all this..
i have written 2 parameters but whats third parameter

Code:

class MyString
{
private:

string inputString;
int count;

public:

MyString(string& , int& );
};

class MyStrVec
{

private:

vector <MyString> myStrVec;

public:

MyStrVec(){};

void sort()
{
std::sort( myStrVec.begin(), myStrVec.end() );
};
};

01-12-2004

Fordy

Theres a version of std::sort that allows you to pass a predicate function or functor to decide how each item is to be compared before sorting

Lookup std::sort in your docs for more info

[edit] Also you cant create a vector of objects unless that object has a default contructor

01-12-2004

justdoit22

can some one tell me how shall i impelement the predicate function which copares two classes before sorting????

The other option is to overload the < operator for MyString, however the semantics of < with a string like object is generally expected to be lexical comparison of the strings (inputString<rhs.inputString). You can also make cmp_count a nested class of MyString, it will still need to be a friend of MyString, unlike Java.

Note also that a class does not need a default constructor to be an element of a vector. In fact, this is one of the many ways vectors rule over arrays. You need a default ctor for your vectors element type only if you resize to a larger size, or construct with an initial number of elements and do not provide a default value. example : vec.resize(30,"Hi"); will add elements copy constructed from a temporary constructed from "Hi", no default construction required. vec.resize(30); will default construct the temporary to copy construct the elements.

01-12-2004

Codeplug

Quote:

Originally posted by grib ... but not required.

You don't even need a struct, a regular 'ole function pointer will work the same. Then all you need is a private, static member function to do the comparison.

gg

01-13-2004

CornedBee

A function pointer will be slower because it can't be inlined.

01-13-2004

justdoit22

Ok what i did at the end of the day was that i provided getter for MyString::count
and defined a Global comparison operator as this

Just for fun you might want to understand what the const does and why you should add it there. If you already understand and just forgot where to put it, then great. If not, then read up on making member functions const.

The gist of it is that if a function is a member of a class, and it does not change the state of that class, then you make the function const by adding the keyword const after the function declaration like my example showed you (if you don't implement the function inline within the class, you must also add the const when you define it elsewhere).

The point of making the function const is that now the compiler knows that you won't be changing the data inside the object, so it can do whatever optimizations it wants with that knowledge. You are also telling the user that they can call that function without fear of changing the data inside the class.

Later, when you write a function like the operator< above, you get increased efficiency by passing a reference instead of making a copy of the object. However, you don't want to pass a non-constant reference, because you don't want your operator< to change the objects that are passed to it - that wouldn't make sense and it goes against the accepted use of operator<. So the way to get the efficiency of a reference and still guarantee that the object won't be changed is to make it a const reference. And once you have a const reference you can only call const member functions.

Hope that makes sense and I hope you can see the difference between making the function const and making the return value const which is what your attempts were doing when you put the const before the function name.

01-14-2004

CornedBee

The essence of the const keyword is that it makes the implicit this parameter const, so a const object can be passed as well as a non-const.