2D vector erase() ?

i have a problem with 2D vector.
i have n*m massiv and want to delete r-th n.
ex:
k[5][6] = {1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4
5 5 5 5 5 5
}
and after maybe choose to delete 2nd row, it will be
k[4][6] = {1 1 1 1 1 1
3 3 3 3 3 3
4 4 4 4 4 4
5 5 5 5 5 5
}

must have to delete given index number n-th of row.
I used erase() but it's not work true

Last edited on
Post your code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 if(ch == 1)
   {  
     int num = 0;
     for(int j = 0; j < NUM; j++)
     {
       for(int i = 0; i = jj.size(); i++)      
       {
         if(j == jj[i])      
         {
           num = j;  
           x.erase(x.begin()+num);
           y.erase(y.begin()+num);         
           NUM--;      
          }
        }
     }
   }
 
Last edited on
Could it just be this line?

for(int i = 0; i = jj.size(); i++)

Presumably you mean

for(int i = 0; i < jj.size(); i++)
yes it's

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 if(ch == 1)
   {  
     int num = 0;
     for(int j = 0; j < NUM; j++)
     {
       for(int i = 0; i < jj.size(); i++)      
       {
         if(j == jj[i])      
         {
           num = j;  
           x.erase(x.begin()+num);
           y.erase(y.begin()+num);         
          }
          NUM--;      
        }
     }
   }
Last edited on
It's tough to tell exactly what you are trying to do from your posts. Your original post talks about deleting columns, but your example actually deletes the second row. The code you posted doesn't look like it'll do either, but looks more like you actually are trying to delete a given column. So, here's some sample code for both:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//Deleting a row:

void print2dVec(vector<vector<int> > k){

	for(unsigned int i = 0; i < k.size(); ++i){
		for(unsigned int j = 0; j < k[i].size(); ++j)
			cout << k[i][j] << " ";

		cout << endl;
	}
	cout << endl << endl;

}

int main()
{

	vector<vector<int> > k;

	for(int i = 1; i <=5; ++i)
		k.push_back(vector<int>(6, i));

	print2dVec(k);

	int num = 2; //row to delete

	vector<vector<int> >::iterator row = k.begin();
	while(row != k.end()){
		if((*row)[0] == num)
			break;
		row++;
	}

	if(row != k.end()){
		row->clear();// Clear out that row
		k.erase(row);// Remove that row
	}


	print2dVec(k);

	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//Deleting a column

void print2dVec(vector<vector<int> > k){

	for(unsigned int i = 0; i < k.size(); ++i){
		for(unsigned int j = 0; j < k[i].size(); ++j)
			cout << k[i][j] << " ";

		cout << endl;
	}
	cout << endl << endl;

}

int main()
{

	vector<vector<int> > k;

	for(unsigned int i = 0; i <=4; ++i){
		k.push_back(vector<int>());
		for(int j = 1; j <=6; j++)
			k[i].push_back(j);
	}

	print2dVec(k);

	int num = 2; //Column to delete

         //Walk through each row and delete the appropriate column
	for(unsigned int i = 0; i < k.size(); ++i){
                	vector<int>::iterator col = k[i].begin();
		while(col != k[i].end()){
			if((*col) == num)
				break;
			col++;
		}

		if(col != k[i].end()){
			k[i].erase(col);// Remove that column entry
		}
	}


	print2dVec(k);

	return 0;
}
thanks jRaskell,
deleting a row was working great but how to delete some random number of rows it means not delete just one of rows.
delete given index numbers of rows?
Last edited on
http://www.cplusplus.com/reference/stl/vector/erase/

Erase will work with a contiguous range of entries (rows).

Erase also returns an iterator to the next element in the vector after the element that was just erased. So if you have some random criteria defined, you could just put that criteria in your while loop something like:

1
2
3
4
5
6
7
8
9
10
	vector<vector<int> >::iterator row = k.begin();
	while(row != k.end()){
		switch((*row)[0]){
			case 2:
			case 4:
				row = k.erase(row);
			default:
				row++;
		}
	}


The switch is for illustration only. Just note that when you call erase, you assign the return to your row iterator, and you only increment the iterator when you don't erase (because erase is incrementing the iterator for you).
Topic archived. No new replies allowed.