简体   繁体   English

如何删除2d向量中的列,c ++

[英]How to delete column in 2d vector, c++

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, 我已经填充了2d向量现在我需要一种方法来删除该向量中的特定列,

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 // so什么是删除向量向量中特定列的算法或代码

for example if i have 例如,如果我有

V11, V12, V13
V21, V22, V23
V31, V32, V33

Then I would like to remove a specific column for instance column 1 which will erase V12, V22, V32 and shrink as below: 然后我想删除第1列的特定列,它将擦除V12,V22,V32并缩小如下:

V11, V13
V21, V23
V31, V33

Thank you for looking at this problem. 感谢您查看此问题。

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));
    });

Live demo 现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM