I've embedded a pair vector within a pair vector in order to be able to have 3 data types (all ints) within a pair. I am now having trouble sorting by the embedded pair vector. Can anyone tell me what I am doing wrong?
Here's my vector pair:
vector < pair<int, pair<int,int> > > obj2;
Here's my sort function:
bool sortby(const pair<int,int> &a,
const pair<int,int> &b)
{
return (a.first < b.first);
}
sort(vect.begin(), vect.end(), sortby)
There is no need to write a comparison function:
#include <cstddef>
#include <cstdlib>
#include <ctime>
#include <utility>
#include <vector>
#include <algorithm>
#include <iostream>
int main()
{
std::srand(static_cast<unsigned>(std::time(nullptr)));
std::vector<std::pair<int, std::pair<int, int> > > obj2;
for (std::size_t i{}; i < 10; ++i)
obj2.push_back(std::pair(rand() % 10, std::pair(rand() % 10, rand() % 10)));
for (auto const & i : obj2)
std::cout << i.first << ", " << i.second.first << ", " << i.second.second << '\n';
std::cout.put('\n');
std::sort(std::begin(obj2), std::end(obj2));
for (auto const & i : obj2)
std::cout << i.first << ", " << i.second.first << ", " << i.second.second << '\n';
std::cout.put('\n');
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.