### Deleting columns and rows in 2D vector

Hello,

I am struggling to delete one column and one row from a 2D vector, where the column and row to be deleted depend on the position of the element, not its contents.

Without using an iterator I can delete a row, but not a column:
 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````#include #include #include using namespace std; int main () { int i, j, p; int num =2; //Row and column number to be deleted vector > k; for(i = 0; i <=5; ++i) //Fill vector k { k.push_back(vector()); for(j = 1; j <=6; j++) k[i].push_back(j); } for (i=0; i< k.size(); ++i) //Erases row number 2 { if (i == num) { k.erase(k.begin()+i); } } for (int x=0; x< k.size(); ++x) //print vector { for (int y=0; y< k[x].size(); ++y) { cout << k[x][y] <<"\t"; } cout << endl; } ``````

My attempt to delete a column isn't working:
 ``12345678910`` ``````for (int r=0; r< k.size(); ++r) { for (int s=0; s< k[r].size(); ++s) { if (k[r][s]==k[r][num]) { k.erase(k.begin() +s); } } } ``````
Your logic is wrong. `k.erase(X)` will aways delete a row. Assuming `k[Row][Col]`. Your use of single letters for naming objects is hideous.

You're also using way too much iteration in your code.

To delete a row:
 ``12345`` ``````unsigned rowToDelete = 2; if (myVector.size() > rowToDelete) { myVector.erase( myVector.begin() + rowToDelete ); }``````

To delete a column:
 ``123456789`` ``````unsigned columnToDelete = 2; for (unsigned i = 0; i < myVector.size(); ++i) { if (myVector[i].size() > columnToDelete) { myVector[i].erase(myVector[i].begin() + columnToDelete); } }``````

Last edited on
Thank you very much! Sorry about the single letter names. I used them for simplicity, but they obviously don't read well.
Simplicity is having names that correspond nicely to the functionality of the object. When you put your code down for 6mnths and come back too it; how easy will it be to read and follow?

All good (or even average) coding standards will emphasize the naming of variables to be applicable to their purpose.
Fun:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869`` ``````#include #include #include #include #include template struct delete_column { void operator () ( ContainerType& container ) { typename ContainerType::iterator iter = container.begin(); std::advance( iter, N ); container.erase( iter ); } }; const char* DATA[ 4 ][ 4 ] = { { "0,0", "0,1", "0,2", "0,3" }, { "1,0", "1,1", "1,2", "1,3" }, { "2,0", "2,1", "2,2", "2,3" }, { "3,0", "3,1", "3,2", "3,3" }, }; std::ostream& operator << ( std::ostream& outs, const std::vector > & v ) { for (unsigned r = 0; r < v.size(); r++) { outs << "Row " << r << ": "; for (unsigned c = 0; c < v[ r ].size(); c++) outs << v[ r ][ c ] << " "; outs << std::endl; } return outs; } int main( int argc, char** argv ) { using namespace std; vector > data; for (unsigned n = 0; n < 4; n++) data.push_back( vector ( DATA[ n ], DATA[ n ] + 4 ) ); cout << data << endl; cout << "Row 2 deleted:\n"; data.erase( data.begin() + 2 ); cout << data << endl; cout << "Column 3 deleted:\n"; for_each( data.begin(), data.end(), delete_column , 3> () ); cout << data << endl; cout << "Column 1 deleted:\n"; for_each( data.begin(), data.end(), delete_column , 1> () ); cout << data << endl; cout << "Column 0 deleted:\n"; for_each( data.begin(), data.end(), delete_column , 0> () ); cout << data << endl; cout << "Column 2 deleted:\n"; for_each( data.begin(), data.end(), delete_column , 0> () ); // it's really column 0 now... cout << data << endl; return 0; }``````
Topic archived. No new replies allowed.