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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
template <typename ContainerType, typename ContainerType::size_type N>
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 <std::vector <std::string> > & 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 <vector <string> > data;
for (unsigned n = 0; n < 4; n++)
data.push_back( vector <string> ( 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 <vector <string>, 3> () );
cout << data << endl;
cout << "Column 1 deleted:\n";
for_each( data.begin(), data.end(), delete_column <vector <string>, 1> () );
cout << data << endl;
cout << "Column 0 deleted:\n";
for_each( data.begin(), data.end(), delete_column <vector <string>, 0> () );
cout << data << endl;
cout << "Column 2 deleted:\n";
for_each( data.begin(), data.end(), delete_column <vector <string>, 0> () ); // it's really column 0 now...
cout << data << endl;
return 0;
}
|