[英]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;
上述方法是在恒定时间内获取列表最后一个元素的地址的正确方法吗? 我知道还有很多其他方法,但我想要一种在恒定时间内给出地址的方法。
如何获取列表中被推回的元素的地址,在恒定时间内,在 c++ stl 中?
推送元素后,使用std::list::back
获取对元素的引用。 然后使用运算符 & 获取元素的地址(如果运算符重载,则使用std::addressof
,这是不寻常的)。
以上方法是获取列表最后一个元素地址的正确方法吗
不,有几个问题:
std::list:end
将迭代器返回到列表末尾之后的元素; 不是最后一个元素的迭代器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.