在 C++ 中是否有等价于列表切片 [1:]
的 vector ?我只想从 vector 中获取除第一个元素以外的所有元素。
Python 的列表切片操作符:
list1 = [1, 2, 3]
list2 = list1[1:]
print(list2) # [2, 3]
C++ 期望的结果:
std::vector<int> v1 = {1, 2, 3};
std::vector<int> v2;
v2 = v1[1:];
std::cout << v2 << std::endl; //{2, 3}
最佳答案
这可以使用 std::vector
的复制构造函数轻松完成:
v2 = std::vector<int>(v1.begin() + 1, v1.end());
关于python - 在 C++ 中切片 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50549611/