python - 在 C++ 中切片 vector

标签 python c++ vector slice

在 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/

相关文章:

python - 从Azure blob读取excel数据并使用Python azure函数转换为csv

python - 构建一个大型 python 存储库,不导入所有内容

c++ - 相同的代码,不同的结果?从其他项目中包含有什么问题?

c++ - 线程安全 vector 实现

python - 基本 UDP 和 TCP 程序

python - 使用 sklearn 在 Python 中拟合的高斯混合模型太慢 - 还有其他选择吗?

c++ - 模板模板参数扣: three different compilers three different behaviors

c++ - YACC/LEX yyparse() 循环问题

c++ - 编译 vector 类型的类的私有(private)成员时出错 - C++

r - R:获取向量中每个项目的最小值/最大值(与单个值相比)