当前位置: 首页 > article >正文

Android常用C++特性之std::unique

声明:本文内容生成自ChatGPT,目的是为方便大家了解学习作为引用到作者的其他文章中。

std::unique 是 C++ 标准库中的一个算法函数,用于移除相邻的重复元素,使每个相邻的元素在容器中保持唯一。它不会真正移除元素,而是通过重新排列元素来消除相邻的重复项,并返回一个指向新结尾的迭代器。此算法通常和 std::erase 或者 std::erase-remove idiom 结合使用,以实际移除不需要的元素。

语法

#include <algorithm>

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

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

参数

  • first, last:表示要处理的范围的迭代器。
  • p(可选):自定义的二元谓词,用于定义 "相等" 的标准。

返回值

std::unique 返回一个迭代器,指向删除相邻重复元素后的新结尾位置。容器中从该位置到 last 之间的元素仍然存在,但它们的值是未定义的。

特点

  • std::unique 只移除相邻的重复元素。如果需要移除所有重复的元素,通常需要先使用 std::sort 对容器排序。
  • 它通过重新排列容器中的元素,将相邻重复的元素放置在一起并返回新范围的末尾迭代器。容器的大小不会实际改变。

示例

1. 基本使用(移除相邻重复项)
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> vec = {1, 1, 2, 3, 3, 4, 5, 5, 6};

    // 使用 std::unique 移除相邻的重复项
    auto new_end = std::unique(vec.begin(), vec.end());

    // 打印结果
    std::cout << "Vector after unique: ";
    for (auto it = vec.begin(); it != new_end; ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    return 0;
}

输出:

Vector after unique: 1 2 3 4 5 6 
2. 实际移除重复项(结合 erase
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> vec = {1, 1, 2, 3, 3, 4, 5, 5, 6};

    // 使用 std::unique 移除相邻的重复项
    auto new_end = std::unique(vec.begin(), vec.end());

    // 使用 erase 减少容器大小
    vec.erase(new_end, vec.end());

    // 打印结果
    std::cout << "Vector after erase: ";
    for (const auto& num : vec) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}

输出:

Vector after erase: 1 2 3 4 5 6 
3. 自定义谓词

你可以通过提供自定义的谓词来定义 "相等" 的标准。例如,比较绝对值相等的元素:

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

int main() {
    std::vector<int> vec = {1, -1, 2, 3, -3, 4, 5, 5, 6};

    // 使用自定义谓词来移除相邻绝对值相等的元素
    auto new_end = std::unique(vec.begin(), vec.end(), [](int a, int b) {
        return std::abs(a) == std::abs(b);
    });

    // 使用 erase 减少容器大小
    vec.erase(new_end, vec.end());

    // 打印结果
    std::cout << "Vector after unique with custom predicate: ";
    for (const auto& num : vec) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}

输出:

Vector after unique with custom predicate: 1 2 3 4 5 6 
4. 非相邻重复元素的移除(排序+unique)

std::unique 只移除相邻的重复元素。如果容器中存在非相邻的重复元素,通常需要先排序。

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> vec = {1, 5, 2, 3, 3, 4, 5, 1, 6};

    // 对容器进行排序
    std::sort(vec.begin(), vec.end());

    // 使用 std::unique 移除所有重复的元素
    auto new_end = std::unique(vec.begin(), vec.end());

    // 使用 erase 减少容器大小
    vec.erase(new_end, vec.end());

    // 打印结果
    std::cout << "Vector after sort and unique: ";
    for (const auto& num : vec) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}

输出:

Vector after sort and unique: 1 2 3 4 5 6 

总结

  • std::unique 是一个用于移除相邻重复元素的算法。
  • 它通过重新排列容器中的元素,将不需要的重复元素移动到末尾,并返回新的末尾位置。
  • 如果想要实际移除元素,需要结合 erase 函数。
  • 如果容器中存在非相邻的重复元素,通常需要先使用 std::sort 对容器进行排序。

http://www.kler.cn/news/323446.html

相关文章:

  • 接口报错500InvalidPropertyException: Invalid property ‘xxx[256]‘,@InitBinder的使用
  • Python编程:08- pycharm使用技巧
  • OpenSource - 开源WAF_SamWaf
  • 详解Ajax与axios的区别
  • 增强现实中的物体识别与跟踪
  • rocketmq 学习文档
  • Vue3(五) 组件通信大汇总
  • 学习记录:js算法(四十八):另一棵树的子树
  • 【C++】Eclipse技巧汇总
  • Codeforces Round 975 (Div. 2) A. Max Plus Size
  • 使用JLINK合并boot和app两个hex文件,使用Keil烧写到单片机
  • AI Agent如何落地?来看看在教育行业大厂的落地实践
  • 使用 lstm + crf 实现NER
  • 防伪溯源查询系统V1.0.5
  • 虚幻引擎UE5如何云渲染,教程来了
  • 【安装教程】Windows环境下Neo4j的安装与配置
  • php中打印函数
  • 牛犇啊!LSTM+Transformer炸裂创新,精准度高至95.65%!
  • RabbitMQ的高级特性-TTL
  • 【算法】KMP算法
  • Linux C高级 day4
  • Winform—常用控件、属性、事件详情介绍
  • 宠物智能听诊器在多渠道上的健康管理
  • SpringBoot教程(安装篇) | Docker Desktop的安装(Windows下的Docker环境)
  • 开源链动 2+1 模式、AI 智能名片与 S2B2C 商城小程序:以问题解决为导向的盈利新模式
  • ArcGIS与ArcGIS Pro去除在线地图服务名单
  • Android常用C++特性之std::chrono
  • jupyter安装与使用——Ubuntu服务器
  • java网络编程知识点,以及面试常被问的知识点
  • Spring Boot 入门操作指南