C++ 列表随机访问

标签 c++ list data-structures random-access

<分区>

Possible Duplicate:
How to get a certain element in a list, given the position?

所以在 python 中,您可以以随机访问方式获取列表中的元素....

list = [1,12,3]

print(list[1]) 

它打印出 12....

你能用 C++ 列表做同样的事情吗?

我说的是这个:http://www.cplusplus.com/reference/stl/list/list/

最佳答案

在 C++ 中,最接近您想要的是 vector :

std::vector<int> v;
v.push_back(1);
v.push_back(12);
v.push_back(3);
std::cout << v[1] << std::endl; // prints 12

您也可以使用提供的迭代器来遍历 vector 。但是一旦你修改了 vector (插入或删除),它就失效了。

实际提供的 List 类(这是一个双向链表)不提供这种功能。

关于C++ 列表随机访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5767416/

相关文章:

python - 为什么在使用过滤器时 "return s and s.strip()"有效?

c++ - 用Qt3D画三角形

c++ - 二维数组错误c++

c++ - 我无法将元素添加到列表中 - C++

java - List.of 给出编译错误 "The method of(char, char, char, char, char, char) is undefined for the type List"

c++ - 二叉树 getParent 函数

C# SWIG vector <string> 到 string[]

c++ - C++如何从数组中获取最长的素数序列

algorithm - 在特殊条件下获取集合中的最大数量

你能改变 C 非指针类型的内存地址吗?