0

I want to remove a specific values from vector when condition is true.size of every element stored in vector is different.I was trying to remove specific value from vector by index but it removes only 1st value.

for (std::vector<int>::iterator it = removeindices.begin(); 
     it != removeindices.end(); ++it)
    {
    differenceContainer.m_buffer.erase(differenceContainer.m_buffer.begin() + *it);

    }

here removeindices contains the indices from which i want to remove data,data size is differant for every element of vector.differance container contains m_buffer vector from which i want to remove values.

peterchen
  • 40,917
  • 20
  • 104
  • 186
Mitali Patil
  • 67
  • 1
  • 9
  • Please [take the tour](https://stackoverflow.com/tour) first then read the [How to Ask](https://stackoverflow.com/help/how-to-ask) page to form your question better. To allow others to help you, you'll need to provide a [sample of your code](https://stackoverflow.com/help/mcve) or whatever you have tried so far to solve the problem. – Gino Mempin Apr 03 '18 at 04:23
  • Please improve your problem description, an example with values for `differentContainer` and `removeIndices` would be helpful. – peterchen Apr 03 '18 at 06:20
  • removeidices contain index no from which i want to remove data – Mitali Patil Apr 03 '18 at 06:41
  • After you delete any element by index, your indices in removeindices vector has no meaning since elements gets shifted and position changed. So I would suggest collect all element in another vector that needs to be deleted and delete them by value. – DevMJ Apr 03 '18 at 07:04
  • Not clear. Do you just want to remove elements from `m_buffer` at index values that are stored in `removeindices`? If so, first sort `removeindices` in descending order before iterating and `erase`-ing so that indices remain accurate. – acraig5075 Apr 03 '18 at 10:23
  • I think this might help you: https://stackoverflow.com/questions/7958216/c-remove-if-on-a-vector-of-objects – Joseph Apr 05 '18 at 14:50

0 Answers0