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

c++ 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 之前通常需要先对容器进行排序。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值