简体   繁体   中英

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,

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

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:

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

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.

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