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

xml*_*lmx 3 c++ algorithm standards stl

#include <set>
#include <algorithm>

using namespace std;

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

为什么不能std::unique申请std::multiset

son*_*yao 6

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

类型要求

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