I've a 2d vector say
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
Now I want to copy only the last two columns into another 2d vector like
vector < vector < int > > test;
test = 4 5
9 0
1 1
2 2
How can I do this efficiently ?
Maybe like this?
#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());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With