簡體   English   中英

std :: list.unique()應該使迭代器無效?

[英]std::list.unique() should invalidate iterators?

我有以下代碼:

#include <iostream>
#include <list>

int main()
{    
    typedef std::list<int> list;
    int i0t[5]={-1, 2, 3, 3, 5};
    list list_1(i0t, i0t+5);
    list::reverse_iterator ri0 = ++list_1.rbegin();    
    list_1.unique();
    list_1.remove(3);
    int val = *ri0; // why is this valid ?
    std::cout << "val = " << val << "\n";
    return 0;
}

我的直覺是ri0迭代器在
list_1.unique();
list_1.remove(3);
使用MS VS2005調試配置和_HAS_ITERATOR_DEBUGGING = 1
但是,我認為“迭代器調試”沒有抓住這一點。 對 ?

謝謝。

我的直覺是ri0迭代器將變為無效

是的,會的。 根據§23.3.5.5

無效刪除(常量T&值)

無效unique()

[...]僅使迭代器和對已刪除元素的引用無效。

因此,程序的行為是不確定的。

23.3.5.5/15指出,對列表執行刪除操作的效果只會使迭代器和對已刪除元素的引用無效。 它沒有指定該操作必須使那些迭代器無效。 迭代器保持有效是未指定的行為,並且不能保證在其他實現中發生。 但是,修改后訪問迭代器是未定義的行為。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM