STL中的equal_range算法返回一个pair类型的值range,
vector<int> vec;
...//vec initialize
pair<vector<int>::iterator,vector<int>::iterator> range;
range = equal_range(vec.begin(),vec.end(),value);
其中range.first是可以在不改变原来排序顺序的情况下的可以插入value的最小迭代器位置,range.second是不改变原来排序顺序的情况下的可以插入value的最大迭代器位置.
实际情况是:如果vec中存在value,那么range.first就是vec中的指向第一个value位置的迭代器,而range.second则是vec中指向第一个大于value的值的位置的迭代器.如果搜索值在容器中是最后一个值那么range.second就是container.end().当vec中没有value时,range返回一个0区间,也就是range.first=range.second=指向vec中第一个值大于value的位置的迭代器(可能为vec.end,如果vec中所有值均小于value)。
在sgi上的描述。
Note that equal_range may return an empty range; that is, it may return a pair both of whose elements are the same iterator.
Equal_range returns an empty range if and only if the range [first, last) contains no elements equivalent to value.
In this case it follows that there is only one position where value could be inserted without violating the range's
ordering, so the return value is a pair both of whose elements are iterators that point to that position.
在http://www.cplusplus.com/reference/algorithm/equal_range/ 上的描述
If value is not equivalent to any value in the range, the subrange returned has a length of zero, with both iterators
pointing to the nearest value greater than value, if any, or to last, if value compares greater than all the elements
in the range.
(但是在《c++标准程序库自修教程与参考手册》上没有找到相关的说明,按理说应该有,可能是自己浏览的太快没看到。)
从上面可以看出,不论是vec中存在不存在value,如果要将value插入vec中,都可以从range.second位置插入(无论range.second是否指向), 但是用于其他用途时要考虑到容器中不存在value时的range.first = range.second的情况。