c++列出随机访问
可能的重复:
如何获取 a 中的某个元素列表,给定位置?
所以在Python中你可以以随机访问方式获取列表中的一个元素....
list = [1,12,3]
print(list[1])
并且它打印12....
你能用C++列表做同样的事情吗?
Possible Duplicate:
How to get a certain element in a list, given the position?
so in python you can get an element in a list in a random access fashion....
list = [1,12,3]
print(list[1])
and it prints 12....
can you do the same thing with c++ list?
I'm talking about this: http://www.cplusplus.com/reference/stl/list/list/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 C++ 中,最接近您想要的的是向量:
您也可以使用提供的迭代器来遍历向量。但是一旦修改了向量(插入或擦除),它就变得无效。
提供的实际 List 类(这是一个双向链接列表)不提供此类功能。
In C++, the nearest along to what you want would be a vector:
You can use the iterators provided to traverse the vector, too. But once you modify the vector (insert or erase), it becomes invalid.
The actual List class provided (which is a doubly-linked list), doesn't provide this sort of feature.
不。如果使用 std::list ,则必须遍历列表才能找到特定元素,因为列表是双链表,无法使用随机访问运算符访问元素。
这是因为对于列表,在列表中的任何点插入或删除都是快速且高效的,因此开头的第一个元素可能是修改列表后的第三个元素。
no. if you use std::list you have to iterate through the list to find a specific element, because list is a double-linked list, elements cannot be accessed with random access operator.
and that's because with lists, it's fast and efficient to insert or delete at any point in the list, thus what was the first element at the beginning could be the third element after modifying the list.
如果我理解你的问题,那么你问的是 arrays
int list[3(size)] = {1,12,3};
计算<<列表[1];
If I understand your question right, you're asking about arrays
int list[3(size)] = {1,12,3};
cout << list[1];
如果您谈论的是 C++ STL 列表,则不,这是列表的主要问题。
If you are talking about a C++ STL list, no, that is the primary problem with the list.