加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 为什么[std :: unique]不能适用于[std :: multiset]?

发布时间:2020-12-16 10:13:38 所属栏目:百科 来源:网络整理
导读:#include set#include algorithmusing namespace std;int main(){ multisetint coll{ 1,1,2 }; unique(coll.begin(),coll.end()); // error} 为什么std :: unique不能应用于std :: multiset? 解决方法 因为 std::unique改变(通过移动)移动赋值传入的[first,
#include <set>
#include <algorithm>

using namespace std;

int main()
{
        multiset<int> coll{ 1,1,2 };
        unique(coll.begin(),coll.end()); // error
}

为什么std :: unique不能应用于std :: multiset?

解决方法

因为 std::unique改变(通过移动)移动赋值传入的[first,last]范围内的元素.这意味着它需要取消引用迭代器的类型必须满足MoveAssignable的要求.

Type requirements

  • ForwardIt must meet the requirements of 07001.
  • The type of dereferenced ForwardIt must meet the requirements of 07002.

但是std::multiset的迭代器是const迭代器(自C 11起),它不符合要求.不能通过它们移动分配引用的元素.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读