[英]How to get the address of the element which is pushed back in the list, In constant time,in c++ stl?
list<int> l; list<int>::iterator lastElement; lastElement=l.end() l.push_back(10); cout<<"Address of the element 10 in the list is"<<&lastElement<<endl;
Is the above way the correct way to get the address of the last element of a list in constant time.上述方法是在恒定时间内获取列表最后一个元素的地址的正确方法吗? I know there are many other methods but I want a method that gives the address in constant time.我知道还有很多其他方法,但我想要一种在恒定时间内给出地址的方法。
How to get the address of the element which is pushed back in the list, In constant time,in c++ stl?如何获取列表中被推回的元素的地址,在恒定时间内,在 c++ stl 中?
After pushing the element, use std::list::back
to get reference to the element.推送元素后,使用std::list::back
获取对元素的引用。 Then use the operator & to get the address of the element (or use std::addressof
if the operator is overloaded, which is unusual).然后使用运算符 & 获取元素的地址(如果运算符重载,则使用std::addressof
,这是不寻常的)。
Is the above way the correct way to get the address of the last element of a list以上方法是获取列表最后一个元素地址的正确方法吗
No. There are several problems:不,有几个问题:
std::list:end
returns an iterator to the element past the end of the list; std::list:end
将迭代器返回到列表末尾之后的元素; not an iterator to the last element.不是最后一个元素的迭代器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.