如何在2D向量中删除列,C ++ [英] How to delete column in 2d vector, c++
问题描述
如果我在创建矩阵的向量中有一个向量,如何删除该矩阵中的特定列。我已经填充了2d向量,现在我需要一种方法来删除该向量中的特定列,
If i have a vector inside a vector creating a matrix, how do I delete a specific column in that matrix. I have already populated the 2d vector now i need a method to delete a specific column in that vector,
例如,我的向量将如下所示:
for example my vector would look like:
vector<vector<float> > vec;
仅出于了解,我知道如何擦除向量中的行,如下所示:
Just for knowledge i know how to erase the rows in the vector as below:
vec.erase(vec.begin()+row);
//那么删除向量中特定列的算法或代码是什么
// so What is the algorithm or code to delete a specific column in a vector of vector
例如,如果我有
V11, V12, V13
V21, V22, V23
V31, V32, V33
然后我想删除实例列1的特定列将擦除V12,V22,V32并缩小,如下所示:
Then I would like to remove a specific column for instance column 1 which will erase V12, V22, V32 and shrink as below:
V11, V13
V21, V23
V31, V33
感谢您解决此问题。
推荐答案
您需要遍历所有行,并删除每一行中该列中的元素。
You'll need to loop through all the rows, and delete the element in that column in each row.
int columnIndex = 1;
std::for_each(vec.begin(), vec.end(),
[&](std::vector<float>& row) {
row.erase(std::next(row.begin(), columnIndex));
});
这篇关于如何在2D向量中删除列,C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!