map中的lower_bound和upper_bound的意思其实很简单,就两句话:
map::lower_bound(key):返回map中第一个大于或等于key的迭代器指针
map::upper_bound(key):返回map中第一个大于key的迭代器指针
所以,理解这两个函数请不要按照字面意义思考太复杂,因为仅仅是不小于(lower_bound)和大于(upper_bound)这么简单。
看两个msdn里的例子
1 // map_upper_bound.cpp 2 // compile with: /EHsc 3 #include
The first element of map m1 with a key greater than 2 is: 30.The map m1 doesn't have an element with a key greater than 4.The 1st element of m1 with a key greater thanthat of the initial element of m1 is: 20.
1 // map_lower_bound.cpp 2 // compile with: /EHsc 3 #include
The first element of map m1 with a key of 2 is: 20.
The map m1 doesn't have an element with a key of 4.
The element of m1 with a key matching that of the last element is: 30.