While practicing the use of lambdas, I wrote this program which is supposed to sort a list
of pair
s by their second element (an int
).
#include <iostream> #include <algorithm> #include <list> using namespace std; int main() { list<pair <string, int>> s = {{"two", 2}, {"one", 1}, {"three", 3}}; sort(s.begin(), s.end(), [](pair<string,int> a, pair<string, int> b) -> bool { return (a.second) > (b.second); }); for_each(s.begin(), s.end(), [](pair<string, int> a) { cout << a.first << " " << a.second << endl; }); }
I get those errors, though:
c:\qt\qt5.2.0\tools\mingw48_32\lib\gcc\i686-w64-mingw32\4.8.0\include\c++\bits\stl_algo.h:5513: error: no match for 'operator-' (operand types are 'std::_List_iterator<std::pair<std::basic_string<char>, int> >' and 'std::_List_iterator<std::pair<std::basic_string<char>, int> >')
std::__lg(__last - __first) * 2, __comp);
^
c:\qt\qt5.2.0\tools\mingw48_32\lib\gcc\i686-w64-mingw32\4.8.0\include\c++\bits\stl_algo.h:2245: ошибка: 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = std::_List_iterator<std::pair<std::basic_string<char>, int> >; _Compare = main()::__lambda0]', declared using local type 'main()::__lambda0', is used but never defined [-fpermissive]
__final_insertion_sort(_RandomAccessIterator __first,
^
What is wrong with my code?