Home > Software design >  Delete Row in 2D Vector C
Delete Row in 2D Vector C

Time:12-30

So I have the example vector initialized as so:

vector<vector<int> > v = {{1, 2, 3},{4, 5, 6},{7, 8, 9}}

I want to totally delete the row, {4, 5, 6} such that v[1][0] references 7 and the final vector is v = {{1, 2, 3},{7, 8, 9}}

v[1].clear() does not accomplish this, and v[1].erase(v[1].begin(),v[1].begin() 3) doesnt seem to either unless I am just an idiot.

Thank you in advance!

CodePudding user response:

You should erase whole nested vector, not just items inside.

If you writing v[1].erase(v[1].begin(),v[1].begin() 3), you got {{1,2,3},{},{7,8,9}}. Just write what Albin Paul said: v.erase(v.begin() 1) and then your vector will be v = {{1,2,3},{7,8,9}}.

  • Related