How to use automatic lambda parameters in C ++ 11

I have code in C ++ 14. However, when I used it in C ++ 11, it has an error with const auto . How to use it in c ++ 11?

 vector<vector <int> > P; std::vector<double> f; vector< pair<double, vector<int> > > X; for (int i=0;i<N;i++) X.push_back(make_pair(f[i],P[i])); ////Sorting fitness descending order stable_sort(X.rbegin(), X.rend()); std::stable_sort(X.rbegin(), X.rend(), [](const auto&lhs, const auto& rhs) { return lhs.first < rhs.first; }); 
+26
c ++ c ++ 11 auto
May 6 '15 at 8:32
source share
5 answers

C ++ 11 does not support generic lambdas . What auto really means in the list of lambda parameters: a universal parameter comparable to the parameters in the function template. (Note that const is not a problem here.)

Note: C ++ 14 supports lambdas with auto , const auto , etc. You can read about it here .

You have basically two options:

  1. Enter the correct type instead of auto . Here it is the type of the element X , which is a pair<double, vector<int>> . If you find this unreadable, typedef may help.

     std::stable_sort(X.rbegin(), X.rend(), [](const pair<double, vector<int>> & lhs, const pair<double, vector<int>> & rhs) { return lhs.first < rhs.first; }); 
  2. Replace the lambda with a functor with a call statement pattern. This is how common lambdas are mostly implemented behind the scenes. Lambda is very versatile, so consider putting it in a global service header. (However, not using namespace std; enter std:: in case you put it in the header.)

     struct CompareFirst { template <class Fst, class Snd> bool operator()(const pair<Fst,Snd>& l, const pair<Fst,Snd>& r) const { return l.first < r.first; } }; 
     std::stable_sort(X.rbegin(), X.rend(), CompareFirst()); 
+29
May 6 '15 at 8:35
source share

I know there is an accepted answer, but you can also use decltype in C ++ 11 for this, it looks a little dirty ...

 stable_sort(X.rbegin(), X.rend(), [](decltype(*X.cbegin()) lhs, decltype(lhs) rhs) { return lhs.first < rhs.first; }); 

Use cbegin() here when you get the const const value_type in the container.

+10
May 6 '15 at 9:36
source share

Unfortunately, generic lambdas that accept auto (regardless of whether const or not) are only a C ++ 14 function.

See here https://isocpp.org/wiki/faq/cpp14-language#generic-lambdas for more details.

+9
May 6, '15 at 8:35
source share

const auto not supported in C ++ 11 as a lambda parameter (actually generated lambdas are not supported in C ++ 11).

To fix it:

 using pair_type = std::pair<double, std::vector<int>>; vector<pair_type> X; std::stable_sort(X.rbegin(), X.rend(), [](const pair_type&lhs, const pair_type& rhs) { return lhs.first < rhs.first; }); 
+3
May 6 '15 at 8:36
source share

Alternatively, you can directly use the value_type typedef of the container with decltype , for example

 std::stable_sort(X.rbegin(), X.rend(), [](const decltype(X)::value_type & lhs, const decltype(X)::value_type & rhs) {return lhs.first < rhs.first; } ); 
+2
May 6 '15 at 11:44
source share



All Articles