如何在std :: list中到达任意索引 [英] how to reach an arbitrary index in a std::list
问题描述
您好,我想在列表中找到指定的索引,因为我们可以通过[]来实现
std :: vector中的运算符。我无法找到一种方法来做它没有迭代器的b $ b,一个接一个地步进..我需要指向一个
指定的索引并检索该值然后删除该值
条目......
问候
< BLOCKQUOTE>" kkirtac" < ka ********** @ gmail.com写信息
新闻:11 ******************** **@y42g2000hsy.googlegr oups.com ...
您好,我想要达到列表中的指定索引,因为我们可以实现
这是[]的结果。 std :: vector中的运算符。我无法找到一种方法来做它没有迭代器的b $ b,一个接一个地步进..我需要指向一个
指定的索引并检索该值然后删除该值
条目...
那你为什么不用矢量?一般来说,如果你想随机访问一个集合中的
元素(通过索引)而不是顺序访问(从开始()
到end())你需要一个向量。或者你冒着增加的额外费用
遍历整个列表以获得你想要的特定索引。
-
Abdo Haji-Ali
程序员
In | Framez
kkirtacaécrit:
< blockquote class =post_quotes>
您好,我想要达到列表中的指定索引,因为我们可以通过[]来实现
std :: vector中的运算符。我无法找到一种方法来做它没有迭代器的b $ b,一个接一个地步进..我需要指向一个
指定的索引并检索该值然后删除该值
条目...
使用std :: advance算法:
list< T> :: iterator it = list.begin();
int n = 42;
std :: advance(it,n);
//它是第42个元素或list.end()
Michael
< blockquote> Michael DOUBEZaécrit:
kkirtacaécrit:
>你好,我想要到达列表中的指定索引,因为我们可以通过[]来实现这一点。 std :: vector中的运算符。我无法找到一种方法来做它没有迭代器,一个接一个地走。我需要指向一个
指定的索引并检索该值然后删除该条目的值。 ..
使用std :: advance算法:
list< T> :: iterator it = list.begin ();
int n = 42;
std :: advance(it,n);
//它是第42个元素或list.end()
oups它是第43个:)
>
Michael
Hello, i want to reach a specified index in a list, as we can achieve
this by the "[]" operator in a std::vector. I couldnt find a way to do
it without iterators, stepping one by one..i need to point to a
specified index and retrieve the value and then remove the value of
the entry...
Regards
"kkirtac" <ka**********@gmail.comwrote in message
news:11**********************@y42g2000hsy.googlegr oups.com...Hello, i want to reach a specified index in a list, as we can achieve
this by the "[]" operator in a std::vector. I couldnt find a way to do
it without iterators, stepping one by one..i need to point to a
specified index and retrieve the value and then remove the value of
the entry...Then why don''t you use a vector? Generally if you want to randomly access
elements in a collection (by index) rather than sequentially (from begin()
to end()) you''ll need a vector. Or you''ll risk the added overhead of
traversing the whole list to get to the specific index you want.
-
Abdo Haji-Ali
Programmer
In|Framez
kkirtac a écrit :Hello, i want to reach a specified index in a list, as we can achieve
this by the "[]" operator in a std::vector. I couldnt find a way to do
it without iterators, stepping one by one..i need to point to a
specified index and retrieve the value and then remove the value of
the entry...Use the std::advance algorithm:
list<T>::iterator it=list.begin();
int n=42;
std::advance(it,n);
//it is 42th element or list.end()
Michael
Michael DOUBEZ a écrit :kkirtac a écrit :>Hello, i want to reach a specified index in a list, as we can achieve
this by the "[]" operator in a std::vector. I couldnt find a way to do
it without iterators, stepping one by one..i need to point to a
specified index and retrieve the value and then remove the value of
the entry...
Use the std::advance algorithm:
list<T>::iterator it=list.begin();
int n=42;
std::advance(it,n);
//it is 42th element or list.end()oups it is the 43th :)
>
Michael
这篇关于如何在std :: list中到达任意索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!