删除向量中的“行”很容易。
例如
#include <vector>
#include <iterator>
//...
matrix.erase( std::next( std::begin( matrix ) ) );
这是一个演示程序
#include <iostream>
#include <vector>
#include <iterator>
int main()
{
std::vector<std::vector<int>> matrix =
{
{ 1, 3 }, { 2, 6 }, { 8, 10 }, { 15, 18 }
};
for (const auto &row : matrix)
{
bool first = true;
std::cout << '[';
for (const auto &item : row)
{
if (!first)
{
std::cout << ", ";
}
else
{
first = false;
}
std::cout << item;
}
std::cout << "]\n";
}
std::cout << '\n';
matrix.erase( std::next( std::begin( matrix ) ) );
for (const auto &row : matrix)
{
bool first = true;
std::cout << '[';
for (const auto &item : row)
{
if (!first)
{
std::cout << ", ";
}
else
{
first = false;
}
std::cout << item;
}
std::cout << "]\n";
}
std::cout << '\n';
}
程序输出是
[1, 3]
[2, 6]
[8, 10]
[15, 18]
[1, 3]
[8, 10]
[15, 18]
要删除第 i 个“行”,您可以使用下面语句中显示的表达式 std::next( std::begin( matrix ), i )
matrix.erase( std::next( std::begin( matrix ), i ) );