Why this :
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; vector<pair<int, string>> list; int main() { int one = 1, two = 2, three =3, five =5, six = 6; string bla = "bla"; list.push_back( pair<int, string>(two, bla)); list.push_back( pair<int, string>(one, bla)); list.push_back( pair<int, string>(two, bla)); list.push_back( pair<int, string>(six, bla)); list.push_back( pair<int, string>(five, bla)); sort(list.begin(), list.end()); for(auto item : list) { cout << item.first << endl; } }
working as intended? output:
1 2 2 5 6
How does std::sort get a way to sort my int-string pairs? How to do this for my class as a first pair? Is there a way to sort by second using std::sort ?
source share