我有一个二维 vector 说

vector < vector < int > > sample;

sample = 1 2 3 4 5
         6 7 8 9 0
         1 1 1 1 1
         2 2 2 2 2

现在我只想将最后两列复制到另一个二维 vector 中
vector < vector < int > > test;

test = 4 5
       9 0
       1 1
       2 2

我怎样才能有效地做到这一点?

最佳答案

也许像这样?

#include <algorithm>

vector<vector<int> > new_vector;
new_vector.resize(sample.size());

for (size_t i = 0; i < new_vector.size(); ++i) {
    new_vector[i].resize(2);
    copy(sample[i].end() - 2, sample[i].end(), new_vector[i].begin());
}

关于c++ - 如何按列复制 vector ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4599643/

10-11 04:22
查看更多