remove elements from 2D vector?

somebody help me knows 2D vector.
i have a problem that to delete some elements from given number of row.
example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
vector< vector<int> > x;
vector<int > x1;  vector< int> x2;  vector< int> x3;  vector< int> x4;  vector< int> x5;
int a = 1, b = 2, c = 3, d = 5, e = 30;
for(int i = 0; i < 5; i++)
{    
  x1.push_back(a);  
  x2.push-back(b)
  x3.push_back(c);   
  x4.push_back(d);
  x5.push_back(e);  
  a++;  b++;  c++;  d++;  e--;
}
x.push_back(x1);
x.push_back(x2);
x.push_back(x3);
x.push_back(x4);            
x.push_back(x5);


1 2 3 4 5,
2 3 4 5 6,
3 4 5 6 7,
5 6 7 8 9,
30 29 28 27 26

and may delete last 3 elements of 2nd row.
it will be

1 2 3 4 5,
2 3,
3 4 5 6 7,
5 6 7 8 9,
30 29 28 27 26

I tried erase() and delete() many times but can't find the true work
thanks in advencd

Last edited on
x[1].erase(x.[1].back()-1, x.[1].back());
|
1
2
x[1].pop_back();
x[1].pop_back();


http://www.cplusplus.com/reference/stl/vector/
Huh? Your first line is totally wrong.

The second block works, except it doesn't remove the right number of elements.

Most efficient for removing n entries from the back of a vector is

x.resize( std::max( 0, x.size() - 3 ) );

(which is just being extra careful not to try to erase more elements than are in the vector
to begin with.)

Topic archived. No new replies allowed.