首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::unique

Defined in header <algorithm>

?

?

template< class ForwardIt > ForwardIt unique( ForwardIt first, ForwardIt last );

(1)

?

template< class ExecutionPolicy, class ForwardIt > ForwardIt unique( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last );

(2)

(since C++17)

template< class ForwardIt, class BinaryPredicate > ForwardIt unique( ForwardIt first, ForwardIt last, BinaryPredicate p );

(3)

?

template< class ExecutionPolicy, class ForwardIt, class BinaryPredicate > ForwardIt unique( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, BinaryPredicate p );

(4)

(since C++17)

从范围内的每个连续的等效元素组中删除除第一个元素之外的所有元素。[first, last)并返回一个新的过去结束迭代器。逻辑范围的尽头。

移除是通过移动范围中的元素,从而使要删除的元素被覆盖。保留元素的相对顺序,并且物理容器的大小不变。迭代器指向新元素之间的元素。逻辑结束和物理范围的结束仍然是不可引用的,但是元素本身有未指定的值。打电话给unique通常后面是对容器%27的调用。erase方法,它删除未指定的值并减少物理容器的大小以匹配其新的逻辑尺寸。

1%29个元素的比较operator==如果行为不是等价关系...

使用给定的二进制谓词对3%29个元素进行比较。p如果行为不是等价关系,则行为是未定义的。

2,4%29与%281,3%29相同,但根据policy这些过载不参与过载解决,除非std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>是真的。

参数

first, last

-

the range of elements to process

policy

-

the execution policy to use. See execution policy for details.

p

-

binary predicate which returns ?true if the elements should be treated as equal. The signature of the predicate function should be equivalent to the following: bool pred(const Type1 &a, const Type2 &b); The signature does not need to have const &, but the function must not modify the objects passed to it. The types Type1 and Type2 must be such that an object of type ForwardIt can be dereferenced and then implicitly converted to both of them. ?

类型要求

---。

-取消引用前的类型必须符合可移动分配的要求。

返回值

将迭代器转发到范围的新末端。

复杂性

对于非空范围,准确地说std::distance(first,last)-1相应谓词的应用程序。

例外

带有名为ExecutionPolicy报告错误如下:

  • 如果执行作为算法一部分调用的函数,则引发异常ExecutionPolicy是其中之一标准政策,,,std::terminate叫做。对于任何其他人ExecutionPolicy,行为是由实现定义的。
  • 如果算法不能分配内存,std::bad_alloc被扔了。

可能的实施

第一版

*。

模板<class ForwardIt>前进唯一%28前进第一,前进最后%29{如果%28First==最后%29返回最后;前进结果=第一;而%28++第21%=最后%29{如果%28%21%28%2A结果==%2A结果%21=前%29{%2A结果=STD::移动%28%2A第一%29;}返回++结果;}

第二版

模板<类前进,类预测>前进--唯一%28前进--第一,前进--最后,[如果%28--=最后%---29返回最后一次;前进---结果=第一,%28++---第21%=最后%29---{if%28%21 p%28%2A结果,%2A结果%21=前%29{%2A结果=STD::移动%28%2A第一%29;}返回++结果;}

二次

代码语言:javascript
复制
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cctype>
 
int main() 
{
    // remove duplicate elements (normal use)
    std::vector<int> v{1,2,3,1,2,3,3,4,5,4,5,6,7};
    std::sort(v.begin(), v.end()); // 1 1 2 2 3 3 3 4 4 5 5 6 7 
    auto last = std::unique(v.begin(), v.end());
    // v now holds {1 2 3 4 5 6 7 x x x x x x}, where 'x' is indeterminate
    v.erase(last, v.end()); 
    for (int i : v)
      std::cout << i << " ";
    std::cout << "\n";
 
    // remove consecutive spaces
    std::string s = "wanna go    to      space?";
    auto end = std::unique(s.begin(), s.end(), [](char l, char r){
        return std::isspace(l) && std::isspace(r) && l == r;
    });
    // s now holds "wanna go to space?xxxxxxxx", where 'x' is indeterminate
    std::cout << std::string(s.begin(), end) << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
1 2 3 4 5 6 7
wanna go to space?

二次

另见

adjacent_find

finds the first two adjacent items that are equal (or satisfy a given predicate) (function template)

unique_copy

creates a copy of some range of elements that contains no consecutive duplicates (function template)

removeremove_if

removes elements satisfying specific criteria (function template)

unique

removes consecutive duplicate elements (public member function of std::list)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com