C++のmapの最大のキーをとる
C++のstd::mapのキーで最小のキーはbegin()を使用し,最大のキーはrbegin()を使用すればよい.
間違えて最大のキーを取得する際にend()を使ってしまったのでメモ.
endは最終要素の次にアクセスしてしまうので値が不定になる(多分).
未定義動作になるらしいです
map::end - C++ Reference
実際に" It does not point to any element, and thus shall not be dereferenced."とある.
#include <iostream> #include <map> int main(){ std::map<int,int> mp = { {2,20}, {5,50}, {4,40}, }; int minv = mp.begin()->first; int maxv = mp.rbegin()->first; std::cout << minv << std::endl; // 2 std::cout << maxv << std::endl; // 5 int e = mp.end()->first; std::cout << e << std::endl; // 3 (最後の要素の次を指してるので何が入ってるか不明) }
修正
C++のmapの最大のキーをとる - ちゃっくのメモ帳 https://t.co/nEV7G0LB4H
— らりお・ザ・㉅㊛の🈗然㊌㋞㋰㋷㋓ (@lo48576) 2017年6月11日
値が不定というか、未定義動作になる
n3797 (C++14) だと 24.2.1/5 にある: "Results of most expressions are undefined for singular values; 〜"
— らりお・ザ・㉅㊛の🈗然㊌㋞㋰㋷㋓ (@lo48576) 2017年6月11日
上の赤字で示した部分修正受けました.ありがとうございます
ピーー☆━━━━━…‥‥(・_・ヽ) アリガタビーム!!