Vector Erase Confusion

Hi everyone,

I'm trying to convert some C++ code to another language but I'm struggling to understand what a couple lines of code are doing. Here are the two codes:

1
2
3
4
5
  vector<double> values; //Definition
  values.erase(std::remove_if(values.begin(), values.end(), [&](const double& x) { return x < chart_options.start || x > chart_options.end; }), values.end());

  vector<double> values; //Definition
  rtn.erase(std::unique(rtn.begin(), rtn.end()), rtn.end());


My understanding is that vector.erase erases any value from vector.start to vector.end - vector.erase(startpoint,endpoint)

But in the startpoint bit of the first code we have an if statement that seems to be doing the same thing? it removes values x from the vector if they satisfy the conditions, but then that somehow becomes the start point of the erase?

As for the second one, the beginning point is a vector? You remove non-unique contiguous values and then make that the startpoint?

Any help would be appreciated!
Last edited on
remove_if and unique don't actually resize the vector.
They leave garbage elements at the end of the container.
So erase is called to remove those.

https://en.wikipedia.org/wiki/Erase%E2%80%93remove_idiom

I don't see what applying unique to a map is supposed to accomplish, unless it's being applied to the mapped type and not the key.
Thanks for the reply. Turns out I used the wrong definition in the OP.

Correct definition for the unique is: vector<double> values; //Definition

Topic archived. No new replies allowed.