I am trying to perform averages on some values in a vector of pairs that have the same first element, and then storing it inside of a map. For example:
std::vector<std::pair<int, double>> test;
test.push_back(std::make_pair(1,100.0));
test.push_back(std::make_pair(1,200.0));
test.push_back(std::make_pair(1,400.0));
test.push_back(std::make_pair(2,200.0));
test.push_back(std::make_pair(2,300.0));
test.push_back(std::make_pair(3,100.0));
std::map<int, double> test2;
test2 would then contain: {{1, 233.33}, {2, 250}, {3, 100}};
The issue I am having is finding all the duplicate first elements then performing the average.
Note: the test vector is sorted using the code below:
std::sort(test.begin(), test.end(),
[](auto a, auto b)
{
return a.first < b.first;
});
Can someone explain how I should go about doing this?