C++ unique_copy()
C++算法unique_copy()
C++算法 unique_copy()函数用于复制序列,例如每个重复的连续元素成为唯一元素。
它不会改变原始范围并将结果复制到另一个容器中。
第一个版本使用operator ==比较元素,第二个版本使用给定的二进制谓词pred。
语法
equality (1) template <class InputIterator, class OutputIterator>
OutputIterator unique_copy (InputIterator first, InputIterator last,
OutputIterator result);
predicate (2) template <class InputIterator, class OutputIterator, class BinaryPredicate>
OutputIterator unique_copy (InputIterator first, InputIterator last,
OutputIterator result, BinaryPredicate pred);
参数
first: 前向迭代器,指向要复制范围中第一个元素的位置。
last: 前向迭代器,指向要复制的范围中最后一个元素之后的位置。
pred : 用户定义的谓词函数对象定义了将一个范围内的两个元素等效时要满足的条件。二进制谓词返回两个参数,满足时返回true,不满足时返回false。
result: 一个输出迭代器,指向复制范围中第一个元素的位置,该元素正在接收删除了连续重复项的副本。
返回值
一个迭代器,它指向不包含连续重复项的复制范围的新结尾(第一个,最后一个)。
复杂度
复杂度在[first,last)范围内是线性的: 比较每对连续元素,并执行赋值操作
数据竞争
访问范围为[first,last)的对象,并且结果和返回值之间的范围内的对象被修改。
异常安全
如果pred,元素比较,元素赋值或迭代器上的操作中的任何一个抛出异常,则此函数将引发异常。
请注意,无效参数会导致未定义的行为。
示例1
让我们看一个简单的示例来演示unique_copy( ),其中元素将通过operator ==进行比较:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v = { 100, 100, 300, 300, 300, 500, 100,
300, 300, 600, 600, 700 };
// vector to store the copied value
vector<int> v1(10);
vector<int>::iterator ip;
// Using unique_copy
ip = unique_copy(v.begin(), v.begin() + 12, v1.begin());
// Resizing vector v1
v1.resize(distance(v1.begin(), ip));
cout << "Before: ";
for (ip = v.begin(); ip != v.end(); ++ip)
{
cout << *ip << " ";
}
// Displaying vector after applying unique_copy
cout << "\n\nAfter: ";
for (ip = v1.begin(); ip != v1.end(); ++ip)
{
cout << *ip << " ";
}
return 0;
}
输出:
Before: 100 100 300 300 300 500 100 300 300 600 600 700
After: 100 300 500 100 300 600 700
在上面的示例中,来自 Vector v的所有连续重复元素的所有子组都被简化为一个元素,并被复制到新的 Vector v1、
示例2
让我们看另一个简单的示例来说明unique_copy()的用法,其中将通过预定义函数比较元素:
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
// declaring a BinaryFunction
bool Pred(char a, char b)
{
// It checks if the both the arguments are same and equal
// to 'v' then only they are considered same and duplicates are removed
if (a == b && a == 'v')
{
return 1;
}
else
{
return 0;
}
}
int main()
{
// Declaring a string
string s = "You arre vvvisiting vvvogie bbogie", s1;
// Using std::unique_copy to remove the consecutive
// v in the word and copy it to s1
auto ip = unique_copy(s.begin(), s.end(), back_inserter(s1), Pred);
cout << "Before: " << s;
// Displaying the corrected string
cout << "\nAfter: " << s1;
return 0;
}
输出:
Before: You arre vvvisiting vvvogie bbogie
After: You arre visiting vogie bbogie
示例3
让我们看另一个简单的示例来检查容器是否包含重复元素:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v = { 1, 4, 3, 5, 2, 7, 6 };
vector<int>::iterator ip;
// Sorting the array to make duplicate elements
// consecutive
sort(v.begin(), v.end());
// Now v becomes 1 2 3 4 5 6 7
// Declaring a container to store the unique elements
vector<int> v1(7);
// Using unique_copy
ip = unique_copy(v.begin(), v.end(), v1.begin());
// Now v1 becomes {1 2 3 4 5 6 7 }
if (v == v1)
{
cout << "v1 contains only unique elements";
}
else
{
cout << "v1 contains duplicate elements";
}
return 0;
}
输出:
v1 contains only unique elements
在上面的示例中,我们首先从 Vector v中删除重复的元素,然后将所得元素存储到另一个 Vector v1中,然后将v1与v比较(如果两者相同)。(意味着先前的 Vector v仅包含唯一元素,没有重复元素)。
示例4
让我们看另一个简单的示例,以删除给定语句中的所有空格:
#include <string>
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
string s1 = "The string with many spaces!";
cout << "before: " << s1 << '\n';
string s2;
unique_copy(s1.begin(), s1.end(), std::back_inserter(s2),
[](char c1, char c2){ return c1 == ' ' && c2 == ' '; });
cout << "after: " << s2 << '\n';
}
输出:
before: The string with many spaces!
after: The string with many spaces!