C++ 给定位置,如何获取列表中的某个元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5733842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:45:12  来源:igfitidea点击:

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

c++liststl

提问by Test

So I've got a list:

所以我有一个清单:

list<Object> myList;
myList.push_back(Object myObject);

I'm not sure but I'm confident that this would be the "0th" element in the array. Is there any function I can use that will return "myObject"?

我不确定,但我相信这将是数组中的“第 0”个元素。是否有任何我可以使用的函数可以返回“myObject”?

Object copy = myList.find_element(0);

?

?

回答by James McNellis

If you frequently need to access the Nth element of a sequence, std::list, which is implemented as a doubly linked list, is probably not the right choice. std::vectoror std::dequewould likely be better.

如果您经常需要访问序列的第 N 个元素,那么std::list实现为双向链表的 可能不是正确的选择。 std::vector或者std::deque可能会更好。

That said, you can get an iterator to the Nth element using std::advance:

也就是说,您可以使用以下方法获取第 N 个元素的迭代器std::advance

std::list<Object> l;
// add elements to list 'l'...

unsigned N = /* index of the element you want to retrieve */;
if (l.size() > N)
{
    std::list<Object>::iterator it = l.begin();
    std::advance(it, N);
    // 'it' points to the element at index 'N'
}

For a container that doesn't provide random access, like std::list, std::advancecalls operator++on the iterator Ntimes. Alternatively, if your Standard Library implementation provides it, you may call std::next:

对于不提供随机访问的容器,例如std::liststd::advance调用operator++迭代器N时间。或者,如果您的标准库实现提供了它,您可以调用std::next

if (l.size() > N)
{
    std::list<Object>::iterator it = std::next(l.begin(), N);
}

std::nextis effectively wraps a call to std::advance, making it easier to advance an iterator Ntimes with fewer lines of code and fewer mutable variables. std::nextwas added in C++11.

std::next有效地包装了对 的调用std::advance,从而可以更轻松地N使用更少的代码行和更少的可变变量来推进迭代器次数。 std::next是在 C++11 中添加的。

回答by Nawaz

std::listdoesn't provide any function to get element given an index. You may try to get it by writing some code, which I wouldn't recommend, because that would be inefficient if you frequently need to do so.

std::list不提供任何函数来获取给定索引的元素。您可以尝试通过编写一些代码来获得它,我不建议这样做,因为如果您经常需要这样做,效率会很低。

What you need is : std::vector. Use it as:

你需要的是:std::vector。将其用作:

std::vector<Object> objects;
objects.push_back(myObject);

Object const & x = objects[0];    //index isn't checked
Object const & y = objects.at(0); //index is checked 

回答by furas

std::list<Object> l; 
std::list<Object>::iterator ptr;
int i;

for( i = 0 , ptr = l.begin() ; i < N && ptr != l.end() ; i++ , ptr++ );

if( ptr == l.end() ) {
    // list too short  
} else {
    // 'ptr' points to N-th element of list
}

回答by Bill Moore

Maybe not the most efficient way. But you could convert the list into a vector.

也许不是最有效的方式。但是您可以将列表转换为向量。

#include <list>
#include <vector>

list<Object> myList;

vector<Object> myVector(myList.begin(), myList.end());

Then access the vector using the [x] operator.

然后使用 [x] 运算符访问向量。

auto x = MyVector[0];

You could put that in a helper function:

你可以把它放在一个辅助函数中:

#include <memory>
#include <vector>
#include <list>

template<class T>
shared_ptr<vector<T>> 
ListToVector(list<T> List) {
shared_ptr<vector<T>> Vector {
        new vector<string>(List.begin(), List.end()) }
return Vector;
}

Then use the helper funciton like this:

然后像这样使用辅助函数:

auto MyVector = ListToVector(Object);
auto x = MyVector[0];