Boost logo

Boost Users :

From: Surya Kiran Gullapalli (skg_at_[hidden])
Date: 2005-02-07 02:53:42


Hi all,

I'm having this particular class

class File
{
    public:
    int get_size();
    std::string get_name();

    private:
    int size,
    std::string name;
} ;

and a vector of objects of this class.
vector <File> files ;

and if want to sort the elements of the container i use
stable_sort (files.begin(), files.end()) // Invokes operator < if its
defined. otherwise shows up an error.
there's another form of stable_sort, in which we can call a sorting
function of our own.

Now my problem is the sorting criteria i use is dependent on runtime.
say at times i want to sort by size and at other times i want to sort
them by name of the file.

The simple solution could be write 2 functions, sort_on_size and
sort_on_filename which does the work.

Now, the sorting order is also decided at runtime. So i write 2 more
functions for that.

The problem is the number of functions i use increase like any thing,
which does not look good.

The best way should be to use bind (or am i wrong here ?) from Boost
library.

But i'm not getting the syntax correctly here.

the crude way could be

stable_sort (files.begin(), files.end(), (bind (&File::get_size, _1) >
bind (&File::get_size, _2))) ;
 
that is call get_size() on first argument, and call get_size() on second
argument, and compare those values, and return a boolean value.

Can anybody please help me out with the syntax here. ??

Thanks in advance,
Surya



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net