C++复习笔记--STL的常见拷贝替换、算术生成和集合算法的使用

目录

1--常用拷贝替换算法

1-1--copy

1-2--replace

1-3--replace_if

1-4--swap

2--常用算术生成算法

2-1--accumulate

2-2--fill

3--常用集合算法

3-1--set_intersection

3-2--set_union

3-3--set_difference


1--常用拷贝替换算法

        STL 提供了部分常见的拷贝和替换算法,其作用如下:

copy // 将容器内指定范围的元素拷贝到另一个容器当中;

replace // 将容器内指定范围的旧元素替换为新元素

replace_if // 将容器指定范围内满足条件的旧元素替换为新元素

swap // 互换两个容器的元素

1-1--copy

        copy() 将容器内指定范围的元素拷贝到另一个容器当中,其函数原型如下:

copy(iterator beg, iterator end, iteratordest);

// beg 表示开始迭代器

// end 表示结束迭代器

// dest 表示目标起始迭代器

        代码实例:

#include "iostream"
#include "vector"
#include "algorithm"

void MyPrint(int val){
    std::cout << val << " ";    
}

int main(int argc, char* argv[]){

    std::vector<int> v1;
    v1.push_back(10);
    v1.push_back(30);
    v1.push_back(20);
    v1.push_back(40);

    std::vector<int> v2;
    v2.resize(v1.size());
    copy(v1.begin(), v1.end(), v2.begin());
    for_each(v2.begin(), v2.end(), MyPrint);
 
    return 0;
}

1-2--replace

        replace() 将容器内指定范围的旧元素替换为新元素,其函数原型如下:

replace(iterator beg, iterator end, oldvalue, newvalue);

// beg 表示开始迭代器

// end 表示结束迭代器

// oldvalue 表示旧元素

// newvalue 表示新元素

        代码实例:

#include "iostream"
#include "vector"
#include "algorithm"

void MyPrint(int val){
    std::cout << val << " ";    
}

int main(int argc, char* argv[]){

    std::vector<int> v1;
    v1.push_back(10);
    v1.push_back(30);
    v1.push_back(20);
    v1.push_back(40);
    v1.push_back(30);
    v1.push_back(30);
    v1.push_back(30);
    v1.push_back(30);

    std::cout << "before replace: " << std::endl;
    for_each(v1.begin(), v1.end(), MyPrint);
    std::cout << std::endl;

    std::cout << "after replace: " << std::endl;
    replace(v1.begin(), v1.end(), 30, 3000);
    for_each(v1.begin(), v1.end(), MyPrint);
    std::cout << std::endl;
 
    return 0;
}

1-3--replace_if

         replace_if() 将区间内满足条件的元素替换为指定元素,其函数原型如下:

replace_if(iterator beg, iterator end, _pred, newvalue);

// beg 表示开始迭代器

// end 表示结束迭代器

// _pred 表示谓词

// newvalue 表示替换的新元素

        代码实例:

#include "iostream"
#include "vector"
#include "algorithm"

void MyPrint(int val){
    std::cout << val << " ";    
}

class MyReplace{
public:
    bool operator()(int val){
        return val >= 30;
    }
};

int main(int argc, char* argv[]){

    std::vector<int> v1;
    v1.push_back(10);
    v1.push_back(30);
    v1.push_back(20);
    v1.push_back(40);
    v1.push_back(30);
    v1.push_back(40);
    v1.push_back(40);
    v1.push_back(30);

    std::cout << "before replace: " << std::endl;
    for_each(v1.begin(), v1.end(), MyPrint);
    std::cout << std::endl;

    std::cout << "after replace: " << std::endl;
    // 利用仿函数实现 >=30 的元素替换
    replace_if(v1.begin(), v1.end(), MyReplace(), 3000);
    for_each(v1.begin(), v1.end(), MyPrint);
    std::cout << std::endl;
 
    return 0;
}

1-4--swap

        swap() 可以实现两个容器元素的互换,其函数原型如下:

swap(container c1, container c2);

// c1表示容器1

// c2表示容器2

        实例代码:

#include "iostream"
#include "vector"
#include "algorithm"

void MyPrint(int val){
    std::cout << val << " ";    
}

int main(int argc, char* argv[]){

    std::vector<int> v1;
    v1.push_back(10);
    v1.push_back(30);
    v1.push_back(20);
    v1.push_back(40);
    
    std::vector<int> v2;
    v2.push_back(30);
    v2.push_back(40);
    v2.push_back(40);
    v2.push_back(30);

    std::cout << "before swap: " << std::endl;
    for_each(v1.begin(), v1.end(), MyPrint);
    std::cout << std::endl;
    for_each(v2.begin(), v2.end(), MyPrint);
    std::cout << std::endl;

    std::cout << "after swap: " << std::endl;
    swap(v1, v2);
    for_each(v1.begin(), v1.end(), MyPrint);
    std::cout << std::endl;
    for_each(v2.begin(), v2.end(), MyPrint);
    std::cout << std::endl;
 
    return 0;
}

2--常用算术生成算法

        STL 提供了部分常用的算术生成算法,使用时需要包含的头文件为 #include <numeric>;

2-1--accumulate

        accumulate() 用于计算区间内容器元素的累计总和,其函数原型如下:

accumulate(iterator beg, iterator end, value);

// beg 表示开始迭代器

// end 表示结束迭代器

// value 表示起始累计值

        代码实例:

#include "iostream"
#include "vector"
#include "numeric"
#include "algorithm"

void MyPrint(int val){
    std::cout << val << " ";    
}

int main(int argc, char* argv[]){

    std::vector<int> v1;
    for(int i = 0; i < 10; i++){
        v1.push_back(i);
    }
    for_each(v1.begin(), v1.end(), MyPrint);
    std::cout << std::endl;

    int sum1 = accumulate(v1.begin(), v1.end(), 0);
    int sum2 = accumulate(v1.begin(), v1.end(), 1000);
    std::cout << "Sum1: " << sum1 << std::endl;
    std::cout << "Sum2: " << sum2 << std::endl;
 
    return 0;
}

2-2--fill

        fill()用于在容器中填充指定的元素,其函数原型如下:

fill(iterator beg, iterator end, value);

// beg 表示开始迭代器

// end 表示结束迭代器

// value 表示填充的值

        代码实例:

#include "iostream"
#include "vector"
#include "numeric"
#include "algorithm"

void MyPrint(int val){
    std::cout << val << " ";    
}

int main(int argc, char* argv[]){

    std::vector<int> v1;
    v1.resize(10);
    fill(v1.begin(), v1.end(), 100);
    for_each(v1.begin(), v1.end(), MyPrint);
 
    return 0;
}

3--常用集合算法

        STL提供了部分常用集合算法,如求容器的交集、并集和差集等;

3-1--set_intersection

        set_intersection() 用于求两个容器的交集,其函数原型如下:

set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

//beg1 表示容器1的开始迭代器;

//end1 表示容器1的结束迭代器;

//beg2 表示容器2的开始迭代器;

//end2 表示容器2的结束迭代器;

//dest 表示目标容器的开始迭代器;

//注:两个容器集合必须是有序序列!!

        代码实例:

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

void MyPrint(int val){
    std::cout << val << " ";
}

int main(int argc, char* argv[]){
    std::vector<int> v1;
    for(int i = 0; i < 10; i++){
        v1.push_back(i);
    }

    std::vector<int> v2;
    for(int i = 5; i < 15; i++){
        v2.push_back(i);
    }

    std::vector<int> targetV;
    targetV.resize(std::min(v1.size(), v2.size())); // 用两个容器大小的最小值进行初始化
    // 返回交集最后一个元素的迭代器
    std::vector<int>::iterator pos = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), targetV.begin());

    for_each(v1.begin(), v1.end(), MyPrint);
    std::cout << std::endl;
    for_each(v2.begin(), v2.end(), MyPrint);
    std::cout << std::endl;
    for_each(targetV.begin(), pos, MyPrint); // 打印交集元素
    std::cout << std::endl;

    return 0;
}

3-2--set_union

        set_union() 用于求两个容器的并集,其函数原型如下:

set_union(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

//beg1 表示容器1的开始迭代器;

//end1 表示容器1的结束迭代器;

//beg2 表示容器2的开始迭代器;

//end2 表示容器2的结束迭代器;

//dest 表示目标容器的开始迭代器;

//注:两个容器集合必须是有序序列!!

        代码实例:

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

void MyPrint(int val){
    std::cout << val << " ";
}

int main(int argc, char* argv[]){
    std::vector<int> v1;
    for(int i = 0; i < 10; i++){
        v1.push_back(i);
    }

    std::vector<int> v2;
    for(int i = 5; i < 15; i++){
        v2.push_back(i);
    }

    std::vector<int> targetV;
    targetV.resize(v1.size() + v2.size()); // 用两个容器的 容量之和 来初始化容器的大小
    // 返回并集最后一个元素的迭代器
    std::vector<int>::iterator pos = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), targetV.begin());

    for_each(v1.begin(), v1.end(), MyPrint);
    std::cout << std::endl;
    for_each(v2.begin(), v2.end(), MyPrint);
    std::cout << std::endl;
    for_each(targetV.begin(), pos, MyPrint); // 打印并集元素
    std::cout << std::endl;

    return 0;
}

3-3--set_difference

        set_difference() 用于求两个容器的差集,其函数原型如下:

set_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

//beg1 表示容器1的开始迭代器;

//end1 表示容器1的结束迭代器;

//beg2 表示容器2的开始迭代器;

//end2 表示容器2的结束迭代器;

//dest 表示目标容器的开始迭代器;

//注:两个容器集合必须是有序序列!!

两个容器的差值不相同:A与B的差集 = V1 - V1∩V2;B与A的差集 = V2 - V1∩V2;

        代码实例:

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

void MyPrint(int val){
    std::cout << val << " ";
}

int main(int argc, char* argv[]){
    std::vector<int> v1;
    for(int i = 0; i < 10; i++){
        v1.push_back(i);
    }
    std::vector<int> v2;
    for(int i = 5; i < 15; i++){
        v2.push_back(i);
    }

    // V1和V2的差集
    std::vector<int> targetV1;
    targetV1.resize(std::max(v1.size(), v2.size())); // 用两个容器的最大值来初始化容器的大小
    // 返回差集最后一个元素的迭代器
    std::vector<int>::iterator pos1 = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), targetV1.begin());

    // V2和V1的差集
    std::vector<int> targetV2;
    targetV2.resize(std::max(v1.size(), v2.size()));
    std::vector<int>::iterator pos2 = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), targetV2.begin());

    for_each(v1.begin(), v1.end(), MyPrint);
    std::cout << std::endl;
    for_each(v2.begin(), v2.end(), MyPrint);
    std::cout << std::endl;
    for_each(targetV1.begin(), pos1, MyPrint); // 打印差集元素
    std::cout << std::endl;
    for_each(targetV2.begin(), pos2, MyPrint); // 打印差集元素
    std::cout << std::endl;

    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43863869/article/details/129813981