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

std::unique_copy

Defined in header <algorithm>

?

?

template< class InputIt, class OutputIt > OutputIt unique_copy( InputIt first, InputIt last, OutputIt d_first );

(1)

?

template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > ForwardIt2 unique_copy( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first );

(2)

(since C++17)

template< class InputIt, class OutputIt, class BinaryPredicate > OutputIt unique_copy( InputIt first, InputIt last, OutputIt d_first, BinaryPredicate p );

(3)

?

template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPredicate > ForwardIt2 unique_copy( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first, BinaryPredicate p );

(4)

(since C++17)

从范围复制元素[first, last),到另一个范围,从d_first以这样的方式不存在连续的平等元素。只复制每组相等元素的第一个元素。

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

d_first

-

the beginning of the destination range

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 InputIt can be dereferenced and then implicitly converted to both of them. ?

类型要求

-输入必须符合输入器的要求。

-输出必须符合输出器的要求。

-前进1,前进2必须符合先行者的要求。

-取消引用投资的类型必须符合CopyAssignable的要求。如果InputIt不满足ForwardIterator

-取消引用投资的类型必须符合CopyConstrucable的要求。如果InputIt和OutputIt都不满足ForwardIterator,或者如果InputIt不满足ForwardIterator,则InputIt的值类型与OutputIt的值类型不同

返回值

将迭代器输出到上一次写入元素之后的元素。

复杂性

对于非空范围,准确地说std::distance(first, last)-1相应比较器的应用。

例外

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

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

注记

如果InputIt满足ForwardIterator,此函数重新读取输入,以检测重复项。

否则,如果OutputIt满足ForwardIterator的值类型InputItOutputIt,此功能比较*d_first*first...

否则,此函数将进行比较。*first到本地元素副本。

对于执行策略的重载,如果值类型为ForwardIterator1不是两者都是CopyConstructibleCopyAssignable...

二次

代码语言:javascript
复制
#include <string>
#include <iostream>
#include <algorithm>
#include <iterator>
 
int main()
{
    std::string s1 = "The      string    with many       spaces!";
    std::cout << "before: " << s1 << '\n';
 
    std::string s2;
    std::unique_copy(s1.begin(), s1.end(), std::back_inserter(s2),
                     [](char c1, char c2){ return c1 == ' ' && c2 == ' '; });
 
    std::cout << "after:  " << s2 << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
before: The      string    with many       spaces!
after:  The string with many spaces!

二次

另见

adjacent_find

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

unique

removes consecutive duplicate elements in a range (function template)

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

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com