Here is the 2d vector [[1,3],[2,6],[8,10],[15,18]] I want to delete the 2nd row which is [2,6] I tried following to erase the 1st row
matrix[1].erase(intervals[1].begin(),intervals[1].end());
after erasing the row when I printed the matrix, I got [[1,3],[],[8,10],[15,18]] I wanted to remove the brackets also, how to do that?
To delete a "row" in a vector of vectors is easy.
For example
#include <vector>
#include <iterator>
//...
matrix.erase( std::next( std::begin( matrix ) ) );
Here is a demonstration program
#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';
}
The program output is
[1, 3]
[2, 6]
[8, 10]
[15, 18]
[1, 3]
[8, 10]
[15, 18]
From what you showed, I believe the correct code would be
matrix.erase( matrix.begin()+1 );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.