c++ day 4

今天来复习标准模板库

当谈到C++的标准库时,STL(Standard Template Library,标准模板库)是一个重要的组成部分。STL提供了一组通用的模板类和函数,用于实现常见的数据结构和算法。它包括容器(Containers)、迭代器(Iterators)、算法(Algorithms)和函数对象(Function Objects)等。

第一个STL算法:

std::accumulate 是 C++ 标准库中的一个算法函数,它用于计算指定范围内元素的累加值。std::accumulate 函数位于 <numeric> 头文件中。

函数原型如下: 

template<class InputIt, class T> T accumulate(InputIt first, InputIt last, T init); 

这里的 firstlast 是迭代器,指定了范围的起始和结束位置(左闭右开区间),init 是初始值,用于指定累加的起始值。

std::accumulate 函数会遍历指定范围内的元素,并将它们累加起来。它使用加法操作符将每个元素依次加到累加器上,并返回最终的累加结果。

以下是一个使用 std::accumulate 函数的示例:

#include <iostream>
#include <numeric>
#include <vector>

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

    int sum = std::accumulate(numbers.begin(), numbers.end(), 0);

    std::cout << "Sum: " << sum << std::endl;

    return 0;
}

 

在上面的示例中,我们定义了一个整数向量 numbers,其中包含了一些整数。然后,我们使用 std::accumulate 函数计算了向量中所有元素的累加和,并将结果存储在变量 sum 中。最后,我们将结果打印到标准输出流中。

在这个示例中,我们将初始值 0 传递给 std::accumulate 函数作为累加的起始值。函数会将初始值与向量中的每个元素相加,最终得到累加和。

你还可以根据需要自定义二元操作符来改变累加的行为。例如,你可以传递一个乘法操作符来计算累乘值。

下面是一个示例,演示如何使用自定义的乘法操作符来计算一组数字的累乘值:

#include <iostream>
#include <numeric>
#include <vector>

// 自定义乘法操作符
struct Multiply {
    template <typename T>
    T operator()(const T& x, const T& y) const {
        return x * y;
    }
};

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

    // 使用自定义的乘法操作符计算累乘值
    int product = std::accumulate(numbers.begin(), numbers.end(), 1, Multiply());

    std::cout << "Product: " << product << std::endl;

    return 0;
}

在上述示例中,我们定义了一个名为 Multiply 的函数对象结构体,它重载了圆括号操作符(即函数调用操作符)。在 operator() 函数中,我们执行了乘法操作,并返回结果。

然后,我们创建了一个整数向量 numbers,其中包含了一组数字。接下来,在调用 std::accumulate 函数时,将自定义的乘法操作符 Multiply 作为第四个参数传递给函数。这样,std::accumulate 将使用乘法操作符来执行累乘运算,将向量中的元素依次相乘,并返回最终的累乘结果。

最后,我们将累乘结果打印到标准输出流中。

通过自定义二元操作符,你可以根据需要改变 std::accumulate 的行为,使其执行不同的累加、累乘或其他操作。你只需定义一个函数对象或函数,然后将其作为参数传递给 std::accumulate 函数即可。

 

下面来学习std::copystd::next_permutation

这两位都是 C++ 标准库中的算法函数,用于不同的操作。

  1. std::copystd::copy 用于将一个容器(或数组)中的元素复制到另一个容器(或数组)中。它位于 <algorithm> 头文件中。

函数原型如下:

template<class InputIt, class OutputIt>
OutputIt copy(InputIt first, InputIt last, OutputIt d_first);

firstlast 是输入范围的迭代器,指定了要复制的元素范围。d_first 是输出范围的迭代器,指定了复制元素的目标位置。

以下是一个使用 std::copy 的示例:

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <vector>
 4 
 5 int main() {
 6     std::vector<int> source = {1, 2, 3, 4, 5};
 7     std::vector<int> destination(source.size());
 8 
 9     std::copy(source.begin(), source.end(), destination.begin());
10 
11     for (int num : destination) {
12         std::cout << num << " ";
13     }
14     std::cout << std::endl;
15 
16     return 0;
17 }

在上面的示例中,我们有一个源向量 source,并创建了一个目标向量 destination,它的大小与源向量相同。然后,我们使用 std::copy 函数将源向量中的元素复制到目标向量中。最后,我们打印目标向量的元素,以验证复制的结果。

  1. std::next_permutationstd::next_permutation 用于生成给定范围内元素的下一个排列。它位于 <algorithm> 头文件中。

函数原型如下:

template<class BidirIt>
bool next_permutation(BidirIt first, BidirIt last);

firstlast 是双向迭代器,指定了要生成排列的范围。

以下是一个使用 std::next_permutation 的示例:

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

int main() {
    std::vector<int> nums = {1, 2, 3};

    do {
        for (int num : nums) {
            std::cout << num << " ";
        }
        std::cout << std::endl;
    } while (std::next_permutation(nums.begin(), nums.end()));

    return 0;
}

在上面的示例中,我们有一个初始的整数向量 nums,包含了一些整数。然后,我们使用 std::next_permutation 函数生成了 nums 中所有元素的排列,并在每次生成一个排列后打印它们。std::next_permutation 会按字典序生成下一个排列,直到所有的排列都被生成完毕。

std::next_permutation 一起使用的函数对象或谓词。这样的函数对象或谓词可以用于自定义生成排列的规则或条件。

以下是一个示例,展示了如何使用自定义的比较函数对象来生成排列:

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

struct Compare {
    bool operator()(int a, int b) {
        // 自定义比较函数,按照降序排列
        return a > b;
    }
};

int main() {
    std::vector<int> nums = {3, 1, 2};

    // 使用自定义的比较函数对象生成降序排列
    std::sort(nums.begin(), nums.end(), Compare());

    do {
        for (int num : nums) {
            std::cout << num << " ";
        }
        std::cout << std::endl;
    } while (std::next_permutation(nums.begin(), nums.end()));

    return 0;
}

在上述示例中,我们定义了一个名为 Compare 的函数对象结构体,它重载了圆括号操作符,用于自定义比较规则。我们使用 std::sort 函数和自定义的比较函数对象将向量 nums 进行降序排序。

然后,我们使用 std::next_permutation 函数生成降序排列的所有排列。std::next_permutation 函数会依次生成下一个排列,直到所有排列都被生成完毕。

 

posted @ 2023-07-08 21:55  芜湖大厨师  阅读(17)  评论(0编辑  收藏  举报