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

c++ accumulate、find、count、fill、fill_n、copy、sort、unique 泛型算法

c++ accumulate、find、count、fill、fill_n、copy、sort、unique 泛型算法

        • `accumulate`
        • `find`
        • `count`
        • `fill`
        • `fill_n`
        • `copy`
        • `sort`
        • `unique`
        • 注意事项

在C++中,STL(标准模板库)提供了许多泛型算法,用于处理容器中的元素。以下是你提到的几种算法的函数声明及其简单使用示例。

accumulate
  • 函数声明
    template< class InputIt, class T >
    T accumulate( InputIt first, InputIt last, T init );
    
  • 功能:计算区间 [first, last) 内元素的累加和,初始值为 init
  • 示例
    #include <iostream>
    #include <vector>
    #include <numeric> // accumulate
    
    int main() {
        std::vector<int> v = {1, 2, 3, 4, 5};
        int sum = std::accumulate(v.begin(), v.end(), 0);
        std::cout << "Sum: " << sum << std::endl; // 输出: Sum: 15
        return 0;
    }
    
find
  • 函数声明
    template< class InputIt, class T >
    InputIt find( InputIt first, InputIt last, const T& value );
    
  • 功能:在区间 [first, last) 内查找值为 value 的元素,返回指向该元素的迭代器。如果未找到,返回 last
  • 示例
    #include <iostream>
    #include <vector>
    #include <algorithm> // find
    
    int main() {
        std::vector<int> v = {1, 2, 3, 4, 5};
        auto it = std::find(v.begin(), v.end(), 3);
        if (it != v.end()) {
            std::cout << "Found: " << *it << std::endl; // 输出: Found: 3
        } else {
            std::cout << "Not found" << std::endl;
        }
        return 0;
    }
    
count
  • 函数声明
    template< class InputIt, class T >
    typename iterator_traits<InputIt>::difference_type
    count( InputIt first, InputIt last, const T& value );
    
  • 功能:计算区间 [first, last) 内等于 value 的元素的个数。
  • 示例
    #include <iostream>
    #include <vector>
    #include <algorithm> // count
    
    int main() {
        std::vector<int> v = {1, 2, 3, 3, 4, 3};
        int cnt = std::count(v.begin(), v.end(), 3);
        std::cout << "Count: " << cnt << std::endl; // 输出: Count: 3
        return 0;
    }
    
fill
  • 函数声明
    template< class ForwardIt, class T >
    void fill( ForwardIt first, ForwardIt last, const T& value );
    
  • 功能:将区间 [first, last) 内的所有元素赋值为 value
  • 示例
    #include <iostream>
    #include <vector>
    #include <algorithm> // fill
    
    int main() {
        std::vector<int> v(5); // 创建一个包含5个元素的vector
        std::fill(v.begin(), v.end(), 10);
        for (int i : v) {
            std::cout << i << " "; // 输出: 10 10 10 10 10
        }
        return 0;
    }
    
fill_n
  • 函数声明
    template< class OutputIt, class Size, class T >
    OutputIt fill_n( OutputIt first, Size count, const T& value );
    
  • 功能:从 first 开始,将 count 个元素赋值为 value
  • 示例
    #include <iostream>
    #include <vector>
    #include <algorithm> // fill_n
    
    int main() {
        std::vector<int> v(5); // 创建一个包含5个元素的vector
        std::fill_n(v.begin(), 3, 10); // 将前3个元素赋值为10
        for (int i : v) {
            std::cout << i << " "; // 输出: 10 10 10 0 0
        }
        return 0;
    }
    
copy
  • 函数声明
    template< class InputIt, class OutputIt >
    OutputIt copy( InputIt first, InputIt last, OutputIt d_first );
    
  • 功能:将区间 [first, last) 内的元素复制到以 d_first 开始的目标区间。
  • 示例
    #include <iostream>
    #include <vector>
    #include <algorithm> // copy
    
    int main() {
        std::vector<int> src = {1, 2, 3, 4, 5};
        std::vector<int> dst(5);
        std::copy(src.begin(), src.end(), dst.begin());
        for (int i : dst) {
            std::cout << i << " "; // 输出: 1 2 3 4 5
        }
        return 0;
    }
    
sort
  • 函数声明
    template< class RandomIt >
    void sort( RandomIt first, RandomIt last );
    
  • 功能:对区间 [first, last) 内的元素进行升序排序。
  • 示例
    #include <iostream>
    #include <vector>
    #include <algorithm> // sort
    
    int main() {
        std::vector<int> v = {5, 3, 1, 4, 2};
        std::sort(v.begin(), v.end());
        for (int i : v) {
            std::cout << i << " "; // 输出: 1 2 3 4 5
        }
        return 0;
    }
    
unique
  • 函数声明
    template< class ForwardIt >
    ForwardIt unique( ForwardIt first, ForwardIt last );
    
  • 功能:移除区间 [first, last) 内相邻的重复元素,返回指向新逻辑结尾的迭代器。
  • 示例
    #include <iostream>
    #include <vector>
    #include <algorithm> // unique
    
    int main() {
        std::vector<int> v = {1, 2, 2, 3, 3, 3, 4};
        auto last = std::unique(v.begin(), v.end());
        v.erase(last, v.end()); // 删除多余的元素
        for (int i : v) {
            std::cout << i << " "; // 输出: 1 2 3 4
        }
        return 0;
    }
    
注意事项
  • 在使用写算法(如 fill, copy 等)时,必须确保目标区间足够大,否则会导致未定义行为。
  • sortunique 通常结合使用,unique 只能移除相邻的重复元素,因此在使用 unique 之前通常需要先对容器进行排序。

http://www.kler.cn/a/570102.html

相关文章:

  • 【实战篇】【深度解析DeepSeek:从机器学习到深度学习的全场景落地指南】
  • 算法-回溯篇02-组合总和 III
  • LeetCode hot 100—矩阵置零
  • Python:全方位赋能,开启科技前沿无限可能
  • Ubuntu20.04 ros-noetic下opencv多版本问题may conflict with libopencv_highgui.so.4.2
  • 鸿蒙NEXT开发-华为账号服务
  • MATLAB CVX 能处理的目标函数数量级极限是多少?
  • 【后端】Flask vs Django vs Node.js 对比分析
  • 数据结构——哈希表的实现
  • unity接入阿里云语音转文字,文字转语音功能
  • 知识库适配DeepSeek,企业微信支持自动登录,授权支持过期时间设置,zyplayer-doc 2.4.9 发布啦!
  • 一个开源且免费的 .NET CMS 和应用程序框架
  • 洛谷————P1634 禽兽的传染病
  • 实验室预约小程序
  • GreptimeDB v0.12 发布,开源 Rust 时序数据库
  • Thinkphp6 应用RdKafka插件封装工具类
  • unity pico开发二:连接头盔,配置手柄按键事件
  • vscode通过ssh远程连接(linux系统)不能跳转问题
  • R语言的基础命令及实例操作
  • C/C++ | 每日一练 (5)