2017-03-08 46 views
0

當你指的是cppreference,你會發現這一點:什麼將從std :: set.end()完全在C++中返回?

Returns an iterator to the element following the last element of the container. This element acts as a placeholder; attempting to access it results in undefined behavior.

然後我運行下面的代碼:

std::set<int> s {1, 2, 3}; 
cout << *s.end() << endl; 

的出來說就是:,爲什麼呢?

+2

未定義的行爲意味着任何事情都可能發生。包括三個。 – aschepler

+0

看起來像「有效」的未定義行爲是最陰險的一種。 – user4581301

+0

與visualC++我得到以下錯誤:「地圖/設置迭代器不可忽略」:) – r3bel

回答

4

What will be returned from std::set.end() exactly in c++?

它返回:

an iterator to the element following the last element of the container.

需要注意的是:

This element acts as a placeholder; attempting to access it results in undefined behavior.

 

The out put is: 3, why?

因爲:

attempting to access it results in undefined behavior.

所以它可以做任何事你的編譯器感覺就像在做什麼,返回3是一種什麼,你的編譯器顯然覺得這樣做。

+0

非常感謝... – cong

+0

@cong我的意思是,你*確實*擁有大部分的信息就在你面前... – immibis

相關問題