首页 > 其他 > 详细

multiset的find函数详解

时间:2020-04-20 19:18:12      阅读:154      评论:0      收藏:0      [点我收藏+]

std::multiset 是含有 Key 类型对象有序集的容器。不同于 set ,它允许多个关键拥有等价的值。用关键比较函数 Compare 进行排序。搜索、插入和移除操作拥有对数复杂度。
与set一样地,它有一个 find 函数,但是当容器内有多个重复元素时,它返回的到底是哪一个元素呢?cppreference.com 上对于 find 函数的解释是“返回值:指向键等于 key 的元素的迭代器。”,并没有说明到底是哪个元素。
使用 Visual Studio 打开头文件 set,找到对应的 find 函数,代码如下(部分):

iterator find(const key_type& _Keyval)
{	// find an element in mutable sequence that matches _Keyval
    iterator _Where = lower_bound(_Keyval);
    return (_Where == end()
        || _DEBUG_LT_PRED(this->_Getcomp(),
            _Keyval, this->_Key(_Where._Ptr))
                ? end() : _Where);
}

这里其实还看不出来,那只能实践出真知了

技术分享图片
经过多次实验,find 函数返回的是等价的几个元素中最先插入进容器的那个。这个结论同样适用于G++编译器。

multiset的find函数详解

原文:https://www.cnblogs.com/melodicule/p/12739712.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!