can anyone please help me in the sort function ? i have read that there are 3 way to use sort().
1. use sort(v.begin(),v.end())
and second is sort(v.begin(),v.begin()+n, cmp)

then in cmp i can have 2 varaitions,

2.a : cmp as the function (it is okay with me)
2.b. here , can you tell me that how "operator()" is overloaded ?

like they have declared a struct and then they have overloaded "operator()" . what is this "operator()" ? can you please xplain me? searched it alot but didnt get anything. thanks.

Recommended Answers

All 9 Replies

This reference article, seems to have the info you're looking for.

yeah, all the things which i have written above are read from this link only. somewhere they have overloaded '<' and some where they have overloaded '()'.. why is it so ? thanks in advance.

What datatype is in your vector?

it can be anything.. like any structure or something like that. why does it depend on data type ?

If it had integers in it for example, this would not be a problem. But since you want to use custom object, you have two options.

Option 1: Overload the () function of a custom compare function:

/* Dummy structure with an integer key and a string */
struct MyStruct {
    int key;
    std::string value;
    MyStruct(int k, const std::string& s) : key(k), value(s) {} // Constructor assigns values to members.
};

/* Custom compare function */
struct is_less {
    inline bool operator() (const MyStruct& struct1, const MyStruct& struct2) {
        return (struct1.key < struct2.key);
    }
};

std::vector < MyStruct > vect;

vect.push_back(MyStruct(4, "daniweb"));
vect.push_back(MyStruct(3, "rocks"));
vect.push_back(MyStruct(2, "big"));
vect.push_back(MyStruct(1, "time"));

std::sort(vect.begin(), vect.end(), is_less());

You can see that the std::sort function takes a third parameter (is_less()) to tell it which function to use for the sort.

Option 2:
By default std::sort will use the operator < to sort the vector, so you could just overload that one.

struct MyStruct {
    int key;
    std::string value;

    MyStruct(int k, const std::string& s) : key(k), value(s) {}

    /* Overloading the compare operator */
    bool operator < (const MyStruct& str) const {
        return (key < str.key);
    }
};

std::vector < MyStruct > vect;

vect.push_back(MyStruct(4, "daniweb"));
vect.push_back(MyStruct(3, "rocks"));
vect.push_back(MyStruct(2, "big"));
vect.push_back(MyStruct(1, "time"));

std::sort(vect.begin(), vect.end());

As you can see, sort no-longer needs the third parameter.

Disclaimer:
I don't have a compiler at this moment, so there may be some syntax errors in provided examples.

two questions in my mind now,
Firstly, is_less is a structure, how have you used it as function ? and i know that () is used for function calling and how structure and () are interlinked here ? seconldy,

In the statement , key< str.key , "key" is which key here ? i mean in the one object of structure, you are comparing it with which key ?

P.S May be these questions are stupid, but as i have started to learn C++, these things puzzling my mind. thanks in advance to you.

In

return (key < str.key);

key belongs to the object being used and str.key is from the object passed to the function. You could also write it like this if it makes more sense to you:

return (this->key < str.key);

and my first question ? is_less is a structure, how have you used it as function ? and i know that () is used for function calling and how structure and () are interlinked here ? thanks in advance.

Well in the example Nick posted sort you use the functor(object with function operator ( operator () ) overloaded)

//inside sort
if(isless(first,second)) // first and second would be iterators
    // sort accordingly

You should also read this wiki article.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.