2013-10-03 7 views
14

STL初心者の質問下げる:機能のstd ::マップ:: UPPER_BOUNDとstd ::マップについて上限で地図を検索し、バインド

を:: LOWER_BOUNDは、それが実際に存在しないキーを指定することが有効です地図?

std::map<int,int> intmap; 
std::map<int,int>::iterator it1,it2; 

intmap[1]=10; 
intmap[2]=20; 
intmap[4]=40; 
intmap[5]=50; 

it1=intmap.lower_bound (3); // Is this valid? 
it2=intmap.upper_bound (3); // Is this valid? 

おかげで...

+6

はいを​​使用することができます。 (15文字) – kennytm

+0

あなたがこれを投稿する場合、私は答えとして受け入れます。 – NeonGlow

+0

ビルズはこれについてより良い説明をしました。 – kennytm

答えて

21

はい、彼らは両方とも有効です。

map::lower_boundは、キー以上でない最初の要素を指すイテレータを返します。

map::upper_boundは、keyより大きい最初の要素を指すイテレータを返します。

intmap[1]=10; 
intmap[2]=20; 
intmap[4]=40; // <<---both lower_bound(3)/upper_bound(3) will points to here 
intmap[5]=50; 

lower_bound/upper_bound返り値が挿入さになるだろう位置。

注値キーがマップかではありません確認したい場合は、あなたはstd::map::find

関連する問題