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
At Monday 2005-02-07 00:53, you wrote:
Hi all,
I'm having this particular class
class File { public: int get_size(); std::string get_name();
private: int size, std::string name; } ;
kinda difficult to make name anything other than ""
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.
sort(files.begin(), files.end(), binary_predicate) exists
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 mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Victor A. Wagner Jr. http://rudbek.com The five most dangerous words in the English language: "There oughta be a law"
Surya Kiran Gullapalli wrote:
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. ??
Right syntax, wrong library. You can do this with Boost.Lamba. http://www.boost.org/doc/html/lambda.html You might also want to look at phoenix. http://www.boost.org/libs/spirit/phoenix/index.html By the way, the accessor functions in your File class need to be const for this to work. Daniel
Daniel James wrote:
Surya Kiran Gullapalli wrote:
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. ??
Right syntax, wrong library. You can do this with Boost.Lamba. [snip]
Or if you really want to use Boost.Bind, something like: using namespace boost; stable_sort(files.begin(), files.end(), (bind(std::greater<int>(), bind(&File::get_size, _1), bind(&File::get_size, _2)))) ; Remembering to include <functional> to get std::greater. Daniel
participants (3)
-
Daniel James
-
Surya Kiran Gullapalli
-
Victor A. Wagner Jr.