如何在给定的位置下得到列表中的某个元素?

时间:2022-02-16 07:08:39

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);

?

吗?

3 个解决方案

#1


97  

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::vector or std::deque would likely be better.

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

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

也就是说,您可以使用std::advance:获得第n个元素的迭代器。

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::advance calls operator++ on the iterator N times. Alternatively, if your Standard Library implementation provides it, you may call std::next:

对于不提供随机访问的容器,比如在迭代器上N次调用std: list、std::advance调用操作符++ +。或者,如果您的标准库实现提供了它,您可以调用std::next:

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

std::next is effectively wraps a call to std::advance, making it easier to advance an iterator N times with fewer lines of code and fewer mutable variables. std::next was added in C++11.

next有效地封装了对std::advance的调用,使迭代器能够以更少的代码行和更少的可变变量进行N次的迭代。std::接下来在c++ 11中添加。

#2


25  

std::list doesn'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.

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 

#3


5  

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
}

#1


97  

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::vector or std::deque would likely be better.

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

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

也就是说,您可以使用std::advance:获得第n个元素的迭代器。

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::advance calls operator++ on the iterator N times. Alternatively, if your Standard Library implementation provides it, you may call std::next:

对于不提供随机访问的容器,比如在迭代器上N次调用std: list、std::advance调用操作符++ +。或者,如果您的标准库实现提供了它,您可以调用std::next:

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

std::next is effectively wraps a call to std::advance, making it easier to advance an iterator N times with fewer lines of code and fewer mutable variables. std::next was added in C++11.

next有效地封装了对std::advance的调用,使迭代器能够以更少的代码行和更少的可变变量进行N次的迭代。std::接下来在c++ 11中添加。

#2


25  

std::list doesn'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.

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 

#3


5  

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
}