A
A
arteskin2021-04-08 20:12:13
css
arteskin, 2021-04-08 20:12:13

How to erase an element using vector.erase()?

How to remove an element that exceeds a certain value from a vector? (In this case, more than 9)

vector<int> myVector = { 1, 3, 10, 20, 13, 15, 4, 7, 9 , 18, 19, 1, 3 };

  for (int i = 0; i < myVector.size(); i++) {
    if (myVector[i] > 9) {
      myVector.erase(...........) // как тут прописать?
    }
  }

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vadim Kot, 2018-06-01
@z_u_q

The simplest example

P
profesor08, 2018-06-01
@profesor08

css clip path
svg mask

N
NIKITF, 2021-08-05
@NIKITF

#include<iostream>
#include<vector>
using namespace std;
int main()
{
    vector<short> myVector = { 1, 3, 10, 20, 13, 15, 4, 7, 9 , 18, 19, 1, 3 };
    for (auto b = myVector.begin(); b!=myVector.end();) 
    {
        if ((*b) > 9)
        {
           b = myVector.erase(b);
        }
        else b++;
    }
    for (const auto s : myVector)
    {
        cout << s << " ";
    }
    return 0;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question