Let's say I have a vector declared as follows:
struct MYSTRUCT { float a; float b; }; std::vector<MYSTRUCT> v;
Now I want to find all elements of v that have the same a, and average them b, i.e.
Say v contains these five elements {a, b}: {1, 1}, {1, 2}, {2, 1}, {1, 3}, {2, 2}
I want to get v [0], v [1], v [3] (where a is 1) and the average value of b: (1 + 2 + 3) / 3 = 2, v [2] and v [4] ( where a is 2) and the average value of b: (1 + 2) / 2 = 1.5
Then v will look like this: {1, 2}, {1, 2}, {2, 1.5}, {1, 2}, {2, 1.5}
I am not very familiar with STL or Boost, so I can only figure out how to do this with "brute force" in C ++, but I assume that the STL (for_each?) And Boost (lambda?) Libraries can solve this more elegantly .
EDIT For reference only, here is my (working) way to iterate over:
for(int j = 0; j < tempV.size(); j++) { MYSTRUCT v = tempV.at(j); int matchesFound = 0; for(int k = 0; k < tempV.size(); k++) { if(k != j && va == tempV.at(k).a) { vb += tempV.at(k).b; matchesFound++; } } if(matchesFound > 0) { vb = vb/matchesFound; } finalV.push_back(v); }