实现对vector的截段操作。
C++版本
#include <iostream>
#include <vector>
int main() {
vector<int> vector{1,2,3,4,5,6,7,8,9};
cout << "vectoor: ";
for(auto el : vector) {
cout << el << " ";
}
//截取前4个数
vector<int>first1 = vector.begin();
vector<int>last1 = vector.begin() + 4;
vector<int> cut1_vector(first1, last1);
cout << "
cut1_vector: ";
for(auto el : cut1_vector) {
cout << el << " ";
}
//截取后4个数
vector<int>first2 = vector.end() - 4;
vector<int>last2 = vector.end();
vector<int> cut2_vector(first2, last2);
cout << "
cut2_vector: ";
for(auto el : cut2_vector) {
cout << el << " ";
}
cout << "
";
}