我能保证std::unique将保留第一个元素吗?
Do I have the guarantee that std::unique will keep the first element?
考虑基于pair的第一个元素的比较排序的std::vector<std::pair<int, int>>
。
现在假设我应用:
std::unique(std::begin(v),
std::end(v),
[](const std::pair<int, int>& x, const std::pair<int, int>& y)
{return x.first == y.first;});
我能保证std::unique
将保留每个相等范围的第一个元素吗?
是。
从范围[first, last)中每一组连续相等的元素中除去除第一个元素外的所有元素,并为范围的新逻辑末端返回一个过尾迭代器。
。
您所给出的BinaryPredicate
只是意味着y
等于前一个元素x
的任何元素将被删除。
是。c++ 14标准草案在第25.3.9节中说。(强调我的):
效果:对于非空范围,从迭代器i所指向的[first + 1,last]范围内的每个连续等效元素组中除去第一个元素之外的所有元素,满足以下条件:*(i - 1) == i或pred((i - 1), *i) != false。
标准的引用在这里很重要,因为通常的互联网来源在这一点上给出了两个不同的答案:en.cppreference.com不提供保证,但cplusplus.com(上面引用的Ben有保证)
相关文章:
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- std::find,返回所有找到的值的替代方法,而不仅仅是存在重复的向量的第一个值
- 如何仅读取文本文件中的第一个值
- 在C++中,如何在第一个"system()"结束后执行第二个"system()"?
- 查找不在标准中的第一个值::设置<int>最小-最大值
- C++:忽略第一个 cin.ignore 之后的输入
- 在C++中打印多个矢量的第一个值
- C++去除前x个元素的有效方法,在不改变向量大小的情况下将第x+1个元素推到第一个
- C++第一个cout将不会打印
- 我们可以在第一个else-if条件结束后使用另一个else-if条件吗
- OpenGL:第二个VBO破坏了第一个VBO
- 为什么第一个Dynamic_cast没有投射到基类?
- 保留从一个多集复制到另一个多集的元素的顺序
- OpenGL 2D游戏只绘制第二个精灵纹理而不是第一个
- C++ 为什么程序只读取第一个值
- 在我的第一个C++程序中需要一些帮助(简单)
- 为什么我的代码在第一个 if 语句处中断?
- 是否可以从另一个类对象调用一个类函数而不继承第一个类
- 无法使我的第一个Windows OpenGL窗口抬起并运行
- 将参数初始化为构造函数,而不是第一个