【黑马程序员】C++仿函数、STL常用算法

STL-函数对象

函数对象

函数对象概念

  • 重载函数调用操作符的类,其对象称为函数对象
  • 函数对象使用重载的()时,行为类似函数调用也叫仿函数
  • 本质:函数对象(仿函数)是一个类,不是一个函数

函数对象使用

  • 函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
class MyAdd {
public:
    // 本质:在类中创建一个函数,可以有参数,可以有返回值
    int operator()(int a, int b) {
        return a + b;
    }
};

// 使用时像调用普通函数那样调用,可以有参数,可以有返回值
void test1() {
    MyAdd myAdd;
    int sum = myAdd(10, 10);
    cout << sum << endl;
} 
  • 函数对象超出普通函数的概念,函数对象可以有自己的状态
// 函数对象超出普通函数的概念,函数对象可以有自己的状态
class MyPrint {
public:
    MyPrint() {
        count = 0;
    }

    void operator()(string test) {
        cout << test << endl;
        this->count++;
    }

    // 统计调用了多少次,不用使用全局变量,可以使用内部成员管理自己的状态
    int count;
};

void test2() {
    MyPrint myPrint;
    myPrint("hello c++");
    myPrint("hello c++");
    myPrint("hello c++");
    myPrint("hello c++");
    cout << "调用了" << myPrint.count << "次" << endl;
}
  • 函数对象可以作为参数传递
// 函数对象可以作为参数进行传递
void doPrint(MyPrint &mp, string msg) {
    mp(msg);
}

void test3() {
    MyPrint myPrint;
    // 传递函数对象作为参数
    doPrint(myPrint, "hello test3");
}

谓词

谓词概念

  • 返回bool类型的仿函数称为谓词

  • 如果operator()接收一个参数,称为一元谓词

  • 如果operator()接收两个参数,称为一元谓词

一元谓词

#include <iostream>
#include <vector>

using namespace std;

class GreaterFive {
public:
    // 一元谓词:返回值为bool,参数只有一个
    bool operator()(int v) {
        return v > 5;
    }
};

void test1() {
    vector<int> v;
    for (int i = 0; i < 10; i++) {
        v.push_back(i);
    }
    // GreaterFive() 传入一个匿名的函数对象
    vector<int>::iterator pos = find_if(v.begin(), v.end(), GreaterFive());
    if (pos != v.end()) {
        cout << "found data is: " << *pos << endl;
    } else {
        cout << "not found" << endl;
    }
}

二元谓词

#include <iostream>
#include <vector>

using namespace std;

class Sort {
public:
    // 二元谓词:返回值为bool,参数有两个
    bool operator()(int v1, int v2) {
        return v1 > v2;
    }
};

void test1() {
    vector<int> v;
    v.push_back(10);
    v.push_back(50);
    v.push_back(20);
    v.push_back(40);
    v.push_back(30);
    sort(v.begin(), v.end(), Sort());
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}

int main() {
    test1();
    return 0;
}

内建函数对象

内建函数对象意义

  • 这些仿函数所产生的对象,用法和一般函数完全相同

  • 使用内建函数对象,需要引入头文件#include<functional>

算术仿函数

  • 功能描述

    • 实现四则运算

    • 其中negate是一元运算,其它都是二元运算

  • 仿函数原型

在这里插入图片描述

  • 代码示例
#include <iostream>
#include <functional>

using namespace std;

// negate 一元仿函数,取反操作
void test1() {
    negate<int> n;
    cout << n(50) << endl;
}

void test2() {
    // plus 加法操作
    plus<int> p;
    cout << p(1, 2) << endl;
    // minus 减法操作
    minus<int> m;
    cout << m(3, 2) << endl;
    // multiplies 乘法操作
    multiplies<int> mul;
    cout << mul(3, 2) << endl;
    // divides 除法操作
    divides<int> d;
    cout << d(6, 2) << endl;
    // modulus 取模操作
    modulus<int> mod;
    cout << mod(6, 5) << endl;
}

int main() {
    test1();
    test2();
    return 0;
}

关系仿函数

  • 功能:实现关系运算

  • 函数原型

  • 代码示例

#include <iostream>
#include <functional>
#include <vector>

using namespace std;

void test() {
    // greater 大于
    greater<int> g;
    cout << g(3, 2) << endl;
    // less 小于
    less<int> l;
    cout << l(3, 2) << endl;
    // greater_equal 大于等于
    greater_equal<int> ge;
    cout << ge(3, 2) << endl;
    // less_equal 小于等于
    less_equal<int> le;
    cout << le(3, 2) << endl;
    // equal_to 等于
    equal_to<int> equal;
    cout << equal(2, 2) << endl;
    // not_equal_to 不等于
    not_equal_to<int> neq;
    cout << neq(2, 2) << endl;
}


void test1() {
    vector<int> v;
    v.push_back(10);
    v.push_back(50);
    v.push_back(20);
    v.push_back(40);
    v.push_back(30);
    // 直接使用内建的大于仿函数进行排序
    sort(v.begin(), v.end(), greater<int>());
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}

int main() {
    test();
    test1();
    return 0;
}

逻辑仿函数

  • 功能:实现逻辑运算

  • 函数原型

在这里插入图片描述

  • 代码示例
#include <iostream>
#include <functional>
#include <vector>

using namespace std;

void test() {
    // logical_and 逻辑与
    logical_and<int> la;
    cout << la(1, 0) << endl;
    // logical_or 逻辑或
    logical_or<int> lo;
    cout << lo(1, 0) << endl;
    // logical_not 逻辑非
    logical_not<int> ln;
    cout << ln(1) << endl;
}

void test1() {
    vector<bool> v;
    v.push_back(true);
    v.push_back(false);
    v.push_back(true);
    v.push_back(false);
    cout << "取反前容器中的数:";
    for (vector<bool>::iterator it = v.begin(); it != v.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
    // 利用逻辑非,将v搬运到容器v2中,并执行取反操作
    vector<bool> v2;
    v2.resize(v.size());
    // transform 搬运之前目标容器必须存在
    transform(v.begin(), v.end(), v2.begin(), logical_not<bool>());
    cout << "取反后容器中的数:";
    for (vector<bool>::iterator it = v2.begin(); it != v2.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}

int main() {
    test();
    test1();
    return 0;
}

stl常用算法

概述

  • 算法主要是由头文件#include <algorithm>、#include <functional>、#include <numeric>组成

  • algorithm是所有STL头文件中最大的一个,范围涉及到比较、交换、查找、遍历操作、复制、修改等

  • functional定义了一些模板类,用以声明函数对象

  • numeric体积很小,只包含几个在序列上进行简单数学运算的模版函数

常用遍历算法

for_each

  • 功能描述:遍历容器

  • 函数原型:for_each(iterator beg, iterator end, _func)

    • beg开始迭代器

    • end结束迭代器

    • _func函数或者函数对象

  • 代码示例

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

using namespace std;

// 1. 利用普通全局函数实现vector遍历
void print01(int val) {
    cout << val << " ";
}

// 2.利用仿函数实现遍历操作
class MyPrint {
public:
    void operator()(int val) {
        cout << val << " ";
    }
};

void test() {
    vector<int> v;
    for (int i = 0; i < 10; i++) {
        v.push_back(i);
    }
    // for_each遍历时需要先给定区间
    // 普通函数直接传函数名
    for_each(v.begin(), v.end(), print01);
    cout << endl;
    // 仿函数需要传仿函数并加() 
    for_each(v.begin(), v.end(), MyPrint());
    cout << endl;
}

int main() {
    test();
    return 0;
}

transform

  • 功能描述:搬运容器到另一个容器中

  • 函数原型:transform(iterator beg1, iterator end1,iterator beg2, _func)

    • beg1源容器开始迭代器

    • end1源容器结束迭代器

    • beg2目标容器开始迭代器

    • _func函数或者函数对象

  • 代码示例

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

using namespace std;

class Transform {
public:
    int operator()(int value) {
        return value;
    }
};

class Print {
public:
    void operator()(int val) {
        cout << val << " ";
    }
};

void test() {
    vector<int> v;
    for (int i = 0; i < 10; i++) {
        v.push_back(i);
    }
    vector<int> v1;
    v1.resize(v.size());
    transform(v.begin(), v.end(), v1.begin(), Transform());
    for_each(v1.begin(), v1.end(), Print());
    cout << endl;
}

int main() {
    test();
    return 0;
}

常用查找算法

算法简介

在这里插入图片描述

find

  • 功能描述:查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器end()

  • 函数原型:find(iterator beg, iterator end, value)

    • beg迭代器开始区间

    • end迭代器结束区间

    • value要查找的值

  • 代码示例

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

using namespace std;

// 查找内置数据类型
void test() {
    vector<int> v;
    for (int i = 0; i < 10; i++) {
        v.push_back(i);
    }
    vector<int>::iterator pos = find(v.begin(), v.end(), 10);
    if (pos == v.end()) {
        cout << "not found" << endl;
    } else {
        cout << "found val is " << *pos << endl;
    }
}

class Person {
public:
    Person(string name, int age) {
        this->name = name;
        this->age = age;
    }

    // 操作自定义数据类型去查找的时候需要重载==,让底层find知道如何比较Person数据类型
    bool operator==(const Person &p) {
        if (this->name == p.name && this->age == p.age) {
            return true;
        }
        return false;
    }

    string name;
    int age;
};

void test1() {
    vector <Person> v;
    Person p1("zs1", 1);
    Person p2("zs2", 2);
    Person p3("zs3", 3);
    Person p4("zs4", 4);
    Person p5("zs5", 5);
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);
    vector<Person>::iterator pos = find(v.begin(), v.end(), p2);
    if (pos != v.end()) {
        cout << "find " << pos->name << " " << pos->age << endl;
    } else {
        cout << "not found" << endl;
    }
}

find_if

  • 功能描述:按条件查找元素

  • 函数原型:find_if(iterator beg, iterator end, _Pred)

    • beg起始迭代器

    • end终止迭代器

    • _Pred函数或谓词,返回bool类型的仿函数

  • 代码示例

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

using namespace std;

class GreaterEight {
public:
    bool operator()(int val) {
        return val > 8;
    }
};

// 查找内置数据类型
void test() {
    vector<int> v;
    for (int i = 0; i < 10; i++) {
        v.push_back(i);
    }
    vector<int>::iterator pos = find_if(v.begin(), v.end(), GreaterEight());
    if (pos == v.end()) {
        cout << "not found" << endl;
    } else {
        cout << "found val is " << *pos << endl;
    }
}


class Person {
public:
    Person(string name, int age) {
        this->name = name;
        this->age = age;
    }

    // 操作自定义数据类型去查找的时候需要重载==,让底层find知道如何比较Person数据类型
    bool operator==(const Person &p) {
        if (this->name == p.name && this->age == p.age) {
            return true;
        }
        return false;
    }

    string name;
    int age;
};

class Greater2 {
public:
    bool operator()(Person &p) {
        return p.age > 2;
    }
};

void test1() {
    vector <Person> v;
    Person p1("zs1", 1);
    Person p2("zs2", 2);
    Person p3("zs3", 3);
    Person p4("zs4", 4);
    Person p5("zs5", 5);
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);
    vector<Person>::iterator pos = find_if(v.begin(), v.end(), Greater2());
    if (pos != v.end()) {
        cout << "find " << pos->name << " " << pos->age << endl;
    } else {
        cout << "not found" << endl;
    }
}

int main() {
    test();
    test1();
    return 0;
}

adjacent_find

  • 功能描述:查找相邻重复元素

  • 函数原型:adjacent_find(iterator beg, iterator end)

  • 代码示例

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

using namespace std;

void test() {
    vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(2);
    v.push_back(2);
    v.push_back(3);
    v.push_back(4);
    vector<int>::iterator it = adjacent_find(v.begin(), v.end());
    if (it == v.end()) {
        cout << "not found" << endl;
        return;
    }
    cout << "found " << *it << endl;
}

int main() {
    test();
    return 0;
}

binary_search

  • 查找指定元素是否存在,仅适用于有序序列

  • 函数原型:bool binary_search(iterator beg, iterator end, val)

    • beg 起始迭代器

    • end 终止迭代器

    • val 要查找的数

  • 代码示例

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

using namespace std;

void test() {
    vector<int> v;
    for (int i = 0; i < 10; i++) {
        v.push_back(i);
    }
    bool isFind = binary_search(v.begin(), v.end(), 3);
    if (isFind) {
        cout << "found" << endl;
        return;
    }
    cout << "not found " << endl;
}

int main() {
    test();
    return 0;
}

count

  • 功能描述:统计元素个数

  • 函数原型:bool count(iterator beg, iterator end, val)

    • beg 起始迭代器

    • end 终止迭代器

    • val 统计的元素

  • 代码示例

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

using namespace std;

// 统计内置类型数据
void test() {
    vector<int> v;
    v.push_back(1);
    v.push_back(1);
    v.push_back(2);
    v.push_back(4);
    v.push_back(1);
    int num = count(v.begin(), v.end(), 1);
    cout << "个数为:" << num << endl;
}

class Person {
public:
    Person(string name, int age) {
        this->name = name;
        this->age = age;
    }

    // 操作自定义数据类型去统计的时候需要重载==,让底层count知道如何比较Person数据类型
    bool operator==(const Person &p) {
        if (this->name == p.name && this->age == p.age) {
            return true;
        }
        return false;
    }

    string name;
    int age;
};

void test1() {
    vector <Person> v;
    Person p1("zs1", 1);
    Person p2("zs2", 2);
    Person p3("zs1", 1);
    Person p4("zs4", 4);
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    int num = count(v.begin(), v.end(), p1);
    cout << "个数为:" << num << endl;
}

int main() {
    test();
    test1();
    return 0;
}

count_if

  • 功能描述:按条件统计元素个数

  • 函数原型:count_if(iterator beg, iterator end, _Pred)

    • beg 起始迭代器

    • end 终止迭代器

    • val 谓词

  • 代码示例

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

using namespace std;

class Greate2 {
public:
    bool operator()(int val) {
        return val > 2;
    }
};

// 统计内置类型数据
void test() {
    vector<int> v;
    v.push_back(1);
    v.push_back(3);
    v.push_back(2);
    v.push_back(4);
    v.push_back(3);
    int num = count_if(v.begin(), v.end(), Greate2());
    cout << "大于2的个数为:" << num << endl;
}

class Person {
public:
    Person(string name, int age) {
        this->name = name;
        this->age = age;
    }

    string name;
    int age;
};

class AgeGreater3 {
public:
    bool operator()(const Person &p) const {
        return p.age > 3;
    }
};

void test1() {
    vector <Person> v;
    Person p1("zs1", 1);
    Person p2("zs2", 2);
    Person p3("zs1", 1);
    Person p4("zs4", 4);
    Person p5("zs5", 4);
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);
    int num = count_if(v.begin(), v.end(), AgeGreater3());
    cout << "年龄大于3的个数:" << num << endl;
}

int main() {
    test();
    test1();
    return 0;
}

常用排序算法

sort

  • 功能描述:对容器内元素进行排序
  • 函数原型:sort(iterator beg, iterator end, _Pred)
  • 代码示例
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void myPrint(int val) {
    cout << val << " ";
}

void test1() {
    vector<int> v;
    v.push_back(5);
    v.push_back(2);
    v.push_back(1);
    v.push_back(4);
    v.push_back(3);
    cout << "默认升序排序:";
    sort(v.begin(), v.end());
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;
}

void test2() {
    vector<int> v;
    v.push_back(5);
    v.push_back(2);
    v.push_back(1);
    v.push_back(4);
    v.push_back(3);
    cout << "降序排序:";
    sort(v.begin(), v.end(), greater<int>());
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;
}

int main() {
    test1();
    test2();
    return 0;
}

random_shuffle

  • 功能描述:洗牌,指定范围内的元素随机调整次序

  • 函数原型:random_shuffle(iterator beg, iterator end)

  • 注意:使用时需要使用随机种子函数srand((unsigned int) time(NULL));

  • 代码示例

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

using namespace std;

void myPrint(int val) {
    cout << val << " ";
}

void test1() {
    srand((unsigned int) time(NULL));
    vector<int> v;
    for (int i = 0; i < 10; i++) {
        v.push_back(i);
    }
    cout << "打乱前:";
    for_each(v.begin(), v.end(), myPrint);
    random_shuffle(v.begin(), v.end());
    cout << endl;
    cout << "打乱后:";
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;
}

int main() {
    test1();
    return 0;
}

merge

  • 功能描述:两个容器元素合并,并存储到另一个容器中

  • 函数原型:merge(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>
#include <ctime>

using namespace std;

void myPrint(int val) {
    cout << val << " ";
}

void test1() {
    vector<int> v1;
    for (int i = 0; i < 5; i++) {
        v1.push_back(i);
    }
    cout << "容器1内容: ";
    for_each(v1.begin(), v1.end(), myPrint);
    cout << endl;
    vector<int> v2;
    for (int i = 7; i < 10; i++) {
        v2.push_back(i);
    }
    cout << "容器2内容: ";
    for_each(v2.begin(), v2.end(), myPrint);
    cout << endl;
    vector<int> dest;
    dest.resize(v1.size() + v2.size());
    merge(v1.begin(), v1.end(), v2.begin(), v2.end(), dest.begin());
    cout << "merge后目标容器内容: ";
    for_each(dest.begin(), dest.end(), myPrint);
    cout << endl;
}

int main() {
    test1();
    return 0;
}

reverse

  • 功能描述:将容器内元素进行反转

  • 函数原型:reverse(iterator beg, iterator end)

    • beg容器开始迭代器

    • end容器终止迭代器

  • 代码示例

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

using namespace std;

void myPrint(int val) {
    cout << val << " ";
}

void test1() {
    vector<int> v1;
    for (int i = 0; i < 10; i++) {
        v1.push_back(i);
    }
    cout << "容器内容: ";
    for_each(v1.begin(), v1.end(), myPrint);
    cout << endl;

    cout << "反转后容器内容: ";
    reverse(v1.begin(), v1.end());
    for_each(v1.begin(), v1.end(), myPrint);
    cout << endl;
}

int main() {
    test1();
    return 0;
}

常用拷贝和替换算法

copy

  • 功能描述:容器内指定范围的元素拷贝到另一个容器中

  • 函数原型:copy(iterator beg, iterator end, iterator dest)

    • beg容器开始迭代器

    • end容器终止迭代器

    • dest目标容器起始迭代器

  • 代码示例

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

using namespace std;

void myPrint(int val) {
    cout << val << " ";
}

void test1() {
    vector<int> v1;
    for (int i = 0; i < 10; i++) {
        v1.push_back(i);
    }
    cout << "容器1内容: ";
    for_each(v1.begin(), v1.end(), myPrint);
    cout << endl;
    vector<int> v2;
    v2.resize(v1.size());
    copy(v1.begin(), v1.end(), v2.begin());
    cout << "容器2内容: ";
    for_each(v2.begin(), v2.end(), myPrint);
    cout << endl;
}

int main() {
    test1();
    return 0;
}

replace

  • 功能描述:将容器内指定范围的旧元素改为新元素

  • 函数原型:replace(iterator beg, iterator beg, oldvalue, newvalue)

    • beg 容器开始迭代器

    • end 容器终止迭代器

    • oldvalue 旧元素

    • newvalue 新元素

  • 代码示例

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

using namespace std;

void myPrint(int val) {
    cout << val << " ";
}

void test1() {
    vector<int> v1;
    v1.push_back(1);
    v1.push_back(3);
    v1.push_back(5);
    v1.push_back(3);
    v1.push_back(2);
    v1.push_back(3);
    cout << "替换前容器内容: ";
    for_each(v1.begin(), v1.end(), myPrint);
    cout << endl;
    cout << "替换算法执行将3替换成4" << endl;
    replace(v1.begin(), v1.end(), 3, 4);
    cout << "替换后容器内容: ";
    for_each(v1.begin(), v1.end(), myPrint);
    cout << endl;
}

int main() {
    test1();
    return 0;
}

replace_if

  • 功能描述:将区间内满足条件的元素,替换成指定元素

  • 函数原型:replace(iterator beg, iterator beg, _pred, newvalue)

    • beg 容器开始迭代器

    • end 容器终止迭代器

    • _pred 谓词

    • newvalue 新元素

  • 代码示例

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

using namespace std;

void myPrint(int val) {
    cout << val << " ";
}

class Greate5 {
public:
    bool operator()(int val) {
        return val > 5;
    }
};

void test1() {
    vector<int> v1;
    for (int i = 0; i < 10; i++) {
        v1.push_back(i);
    }
    cout << "替换前容器内容: ";
    for_each(v1.begin(), v1.end(), myPrint);
    cout << endl;
    cout << "替换算法执行将大于5的元素都替换成1" << endl;
    replace_if(v1.begin(), v1.end(), Greate5(), 1);
    cout << "替换后容器内容: ";
    for_each(v1.begin(), v1.end(), myPrint);
    cout << endl;
}

int main() {
    test1();
    return 0;
}

swap

  • 功能描述:互换两个容器元素

  • 函数原型:swap(container c1, container c2)

  • 代码示例

```cpp
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void myPrint(int val) {
    cout << val << " ";
}

void test1() {
    vector<int> v1;
    for (int i = 0; i < 10; i++) {
        v1.push_back(i);
    }
    vector<int> v2;
    for (int i = 10; i > 0; i--) {
        v2.push_back(i);
    }
    cout << "交换前v1内容: ";
    for_each(v1.begin(), v1.end(), myPrint);
    cout << endl;
    cout << "交换前v2内容: ";
    for_each(v2.begin(), v2.end(), myPrint);
    cout << endl;
    cout << "执行交换v1容器和v2容器操作" << endl;
    swap(v1, v2);
    cout << "交换后v1内容: ";
    for_each(v1.begin(), v1.end(), myPrint);
    cout << endl;
    cout << "交换后v2内容: ";
    for_each(v2.begin(), v2.end(), myPrint);
    cout << endl;
}

int main() {
    test1();
    return 0;
}

常用算术生成算法

accumulate

  • 计算容器元素累计总和

  • 函数原型:accumulate(iterator beg, iterator end, val)

    • beg 容器开始迭代器

    • end 容器终止迭代器

    • val 起始值

  • 代码示例

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

using namespace std;

void test() {
    vector<int> v;
    for (int i = 0; i < 10; i++) {
        v.push_back(i);
    }
    int count = accumulate(v.begin(), v.end(), 0);
    cout << count << endl;
}

int main() {
    test();
    return 0;
}

fill

  • 功能描述:向指定区间填充元素

  • 函数原型:fill(iterator beg, iterator end, val)

    • beg 容器开始迭代器

    • end 容器终止迭代器

    • val 填充元素

  • 代码实例

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

using namespace std;

void myPrint(int val) {
    cout << val << " ";
}

void test() {
    vector<int> v;
    v.resize(10);
    cout << "未填充前:";
    for_each(v.begin(), v.end(), myPrint);
    fill(v.begin(), v.end(), 10);
    cout << endl;
    cout << "填充后:";
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;
}

int main() {
    test();
    return 0;
}

常用集合算法

set_intersection

  • 功能描述:求两个容器的交集

  • 函数原型:set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest)

    • beg1容器1开始迭代器

    • end1容器1终止迭代器

    • beg2容器2开始迭代器

    • end2容器2终止迭代器

    • dest目标容器开始迭代器

set_union

  • 功能描述:求两个容器的并集

  • 函数原型:set_union(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest)

    • beg1容器1开始迭代器

    • end1容器1终止迭代器

    • beg2容器2开始迭代器

    • end2容器2终止迭代器

    • dest目标容器开始迭代器

set_difference

  • 功能描述:求两个容器的差集
  • 函数原型:set_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest)
    • beg1容器1开始迭代器

    • end1容器1终止迭代器

    • beg2容器2开始迭代器

    • end2容器2终止迭代器

    • dest目标容器开始迭代器

代码示例

#include <iostream>
#include <vector>

using namespace std;

void myPrint(int val) {
    cout << val << " ";
}

void test() {
    vector<int> v1;
    for (int i = 0; i < 7; i++) {
        v1.push_back(i);
    }
    cout << "v1容器:";
    for_each(v1.begin(), v1.end(), myPrint);
    cout << endl;
    vector<int> v2;
    for (int i = 4; i < 10; i++) {
        v2.push_back(i);
    }
    cout << "v2容器:";
    for_each(v2.begin(), v2.end(), myPrint);
    cout << endl;
    vector<int> dest_inter;
    dest_inter.resize(min(v1.size(), v2.size()));
    set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), dest_inter.begin());
    cout << "dest_inter求完交集后的容器:";
    for_each(dest_inter.begin(), dest_inter.end(), myPrint);
    cout << endl;
    vector<int> dest_union;
    dest_union.resize(v1.size() + v2.size());
    set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), dest_union.begin());
    cout << "dest_union求完并集后的容器:";
    for_each(dest_union.begin(), dest_union.end(), myPrint);
    cout << endl;
    vector<int> dest_diff;
    dest_diff.resize(v1.size());
    set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), dest_diff.begin());
    cout << "dest_diff求完差集后的容器:";
    for_each(dest_diff.begin(), dest_diff.end(), myPrint);
    cout << endl;
}

int main() {
    test();
    return 0;
}
  • 15
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

double_happiness

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值