C++ 算法 中的 unique_copy函数

首页 / C++入门教程 / C++ 算法 中的 unique_copy函数

C++算法 unique_copy()函数用于复制序列,例如每个重复的连续元素成为唯一元素。

  • 它不会改变原始参数并将结果复制到另一个集合中。
  • 第一个版本使用operator ==比较元素,第二个版本使用给定的二进制谓词pred。

unique_copy - 语法

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);

unique_copy - 参数

first:一个正向迭代器,指向要复制参数内第一个元素的位置。

last:前向迭代器,指向要复制的参数中最后一个元素之后的位置。

pred :用户定义的谓词函数对象,该对象定义了将参数中的两个元素视为等效时要满足的条件。二进制谓词返回两个参数,满足时返回true,不满足时返回false。

result:一个输出迭代器,它指向复制参数中第一个元素的位置,该元素正在接收已删除连续副本的副本。

unique_copy - 返回值

指向不包含连续重复项的复制参数的新结尾(第一个,最后一个)的迭代器。

unique_copy - 例子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  

在上面的示例中,来自向量v的所有连续重复元素的子组都被简化为一个元素,并被复制到新的向量v1中。

unique_copy - 例子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

unique_copy - 例子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

在上面的示例中,首先我们从向量v中删除重复的元素,并将结果元素存储到另一个向量v1中,然后将v1与v进行比较(如果两者相同)。 (意味着先前的向量v仅包含唯一元素,没有重复元素)。

unique_copy - 例子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!

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

Service Mesh实践指南 -〔周晶〕

摄影入门课 -〔小麥〕

Service Mesh实战 -〔马若飞〕

Vim 实用技巧必知必会 -〔吴咏炜〕

物联网开发实战 -〔郭朝斌〕

代码之丑 -〔郑晔〕

玩转Vue 3全家桶 -〔大圣〕

郭东白的架构课 -〔郭东白〕

超级访谈:对话道哥 -〔吴翰清(道哥)〕

好记忆不如烂笔头。留下您的足迹吧 :)