On 8/8/07, chun ping wang
use bind. try using bind. namespace bll = boost::lambda;
std::sort(countries1.begin(), countries1.end(), bll::bind(&countries_t::second, _1) < bll::bind(&countries_t::second, _2));
Does that compile for you? It doesn't for me. Note that second is not a function. ------ Build started: Project: Lambda Test, Configuration: Debug Win32 ------ Compiling... Lambda Test.cpp c:\vc\temp\lambda test\lambda test.cpp(26) : error C2039: 'second' : is not a member of 'std::map<_Kty,_Ty>' with [ _Kty=int, _Ty=int ] c:\vc\temp\lambda test\lambda test.cpp(26) : error C2065: 'second' : undeclared identifier c:\vc\temp\lambda test\lambda test.cpp(26) : error C2039: 'second' : is not a member of 'std::map<_Kty,_Ty>' with [ _Kty=int, _Ty=int ] c:\vc\temp\lambda test\lambda test.cpp(26) : error C2780: 'void std::sort(_RanIt,_RanIt)' : expects 2 arguments - 3 provided c:\program files\microsoft visual studio 8\vc\include\algorithm(3109) : see declaration of 'std::sort' Lambda Test - 4 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
On 8/8/07, Olaf van der Spek
wrote: Hi,
I'm trying to sort a map based on second, but I get all kinds of compiler errors and I have no idea what's wrong. A simple _1 > _2 works fine, but that compares the maps themselves.
Also, is there a better way to write the copy from map to vector or to sort the map in general?
Note that I'm using a map because I'm counting how often an integer or string occurs.
#include
#include #include <algorithm> #include <map> #include <vector> using namespace std; using namespace boost::lambda;
int _tmain(int argc, _TCHAR* argv[]) { typedef std::map
countries_t; countries_t countries; countries[0] = 5; countries[1] = 3; countries[2] = 1; typedef vector countries1_t;
countries1_t countries1; for (countries_t::const_iterator i = countries.begin(); i != countries.end(); i++) countries1.push_back(i); sort(countries1.begin(), countries1.end(), _1->second >
_2->second);
return 0; }
------ Build started: Project: Lambda Test, Configuration: Debug Win32
------
Compiling... Lambda Test.cpp c:\vc\temp\lambda test\lambda test.cpp(23) : error C2819: type 'boost::lambda::lambda_functor<T>' does not have an overloaded member 'operator ->' with [ T=boost::lambda::placeholder<1> ] did you intend to use '.' instead? c:\vc\temp\lambda test\lambda test.cpp(23) : error C2039: 'second' : is not a member of 'boost::lambda::lambda_functor<T>' with [ T=boost::lambda::placeholder<1> ] c:\vc\temp\lambda test\lambda test.cpp(23) : error C2819: type 'boost::lambda::lambda_functor<T>' does not have an overloaded member 'operator ->' with [ T=boost::lambda::placeholder<2> ] did you intend to use '.' instead? c:\vc\temp\lambda test\lambda test.cpp(23) : error C2039: 'second' : is not a member of 'boost::lambda::lambda_functor<T>' with [ T=boost::lambda::placeholder<2> ] c:\vc\temp\lambda test\lambda test.cpp(23) : error C2780: 'void std::sort(_RanIt,_RanIt)' : expects 2 arguments - 3 provided c:\program files\microsoft visual studio 8\vc\include\algorithm(3109) : see declaration of 'std::sort' Lambda Test - 5 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users