[英]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.