C++之STL常用算法(遍历、查找、排序、拷贝、替换、算数生成、集合)

简介: C++之STL常用算法(遍历、查找、排序、拷贝、替换、算数生成、集合)

一、常用遍历算法

 
//1、常用遍历算法
// for_each遍历
void print01(int val) {
    cout << val << " ";
}
 
class print02 {
public:
    void operator()(int val) {
        cout << val << " ";
    }
};
 
void test01() {
    vector<int> v;
    for (int i = 0; i < 10; ++i) {
        v.push_back(i);
    }
    // 普通函数
    for_each(v.begin(), v.end(), print01);
    cout << endl;
    // 仿函数
    for_each(v.begin(), v.end(), print02());
    cout << endl;
}
 
// transform
int change(int val) {
    return val * 10;
}
 
class Transform {
public:
    int operator()(int val) {
        return val;
    }
};
 
void test02() {
    vector<int> v;
    for (int i = 0; i < 10; ++i) {
        v.push_back(i);
    }
    for_each(v.begin(), v.end(), print01);
    cout << endl;
 
    vector<int> v2;
    // 开辟空间
    v2.resize(v.size());
    //源容器开始迭代器 源容器结束迭代器 目标容器开始迭代器 函数或者函数对象
    // transform(v.begin(), v.end(), v2.begin(), Transform());
    transform(v.begin(), v.end(), v2.begin(), change);
    for_each(v2.begin(), v2.end(), print01);
    cout << endl;
}

二、常用查找算法

 
//2、常用查找算法
// 常用查找算法 find
// 查找内置
void test03() {
    vector<int> v;
    for (int i = 0; i < 10; ++i) {
        v.push_back(i);
    }
    vector<int>::iterator it = find(v.begin(), v.end(), 56);
    if (it == v.begin()) {
        cout << "没有找到元素" << endl;
    } else {
        cout << "找到了元素:" << *it << endl;
    }
}
 
// 查找自定义
class Person {
public:
    Person(string name, int age) {
        this->m_Name = name;
        this->m_Age = age;
    }
 
    // 重载== 底层find知道如何对比person数据类型
    bool operator==(const Person &p) {
        if (this->m_Name == p.m_Name && this->m_Age == p.m_Age) {
            return true;
        } else {
            return false;
        }
    }
 
    string m_Name;
    int m_Age;
};
 
void printPerson(Person p) {
    cout << "姓名:" << p.m_Name << " 年龄:" << p.m_Age << endl;
}
 
void test04() {
    vector<Person> v;
    Person p1("aa", 11);
    Person p2("bb", 12);
    v.push_back(p1);
    v.push_back(p2);
    for_each(v.begin(), v.end(), printPerson);
 
    vector<Person>::iterator it = find(v.begin(), v.end(), p2);
    if (it != v.begin()) {
        cout << "找到了" << endl;
        printPerson(*it);
    } else {
        cout << "没有找到" << endl;
    }
}
 
// 查找find_if 内置数据类型
class GreaterFive {
public:
    bool operator()(int val) {
        return val > 5;
    }
};
 
void test05() {
    vector<int> v1;
    for (int i = 0; i < 10; ++i) {
        v1.push_back(i);
    }
    // 开始迭代器 结束迭代器 _Pred函数或者谓词(返回bool类型的仿函数)
    vector<int>::iterator pos = find_if(v1.begin(), v1.end(), GreaterFive());
    if (pos == v1.end()) {
        cout << "没有找到" << endl;
    } else {
        cout << "找到了" << *pos << endl;
    }
}
 
// 查找find_if 自定义数据类型
class Person2 {
public:
    Person2(string name, int age) {
        this->m_Name = name;
        this->m_Age = age;
    }
 
    string m_Name;
    int m_Age;
};
 
void printPerson2(Person2 p) {
    cout << "姓名:" << p.m_Name << " 年龄:" << p.m_Age << endl;
}
 
class Greater20 {
public:
    bool operator()(Person2 &p) {
        return p.m_Age > 20;
    }
};
 
void test06() {
    vector<Person2> v;
    Person2 p1("aa", 10);
    Person2 p2("bb", 20);
    Person2 p3("cc", 30);
    Person2 p4("dd", 40);
    Person2 p5("ee", 50);
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);
    for_each(v.begin(), v.end(), printPerson2);
 
    vector<Person2>::iterator it = find_if(v.begin(), v.end(), Greater20());
    if (it != v.begin()) {
        cout << "找到了" << endl;
        printPerson2(*it);
    } else {
        cout << "没有找到" << endl;
    }
}
 
// 常用查找算法 adjacent_find
void pirntInt(int val) {
    cout << val << " ";
}
 
void test07() {
    vector<int> v;
    v.push_back(0);
    v.push_back(2);
    v.push_back(0);
    v.push_back(3);
    v.push_back(1);
    v.push_back(4);
    v.push_back(3);
    v.push_back(3);
    vector<int>::iterator pos = adjacent_find(v.begin(), v.end());
    if (pos != v.end()) {
        cout << "找到了" << endl;
        for_each(pos, v.end(), pirntInt);
        cout << endl;
    } else {
        cout << "位找到" << endl;
    }
}
 
//binary_search 二分查找
void test08() {
    vector<int> v;
    for (int i = 0; i < 10; i++) {
        v.push_back(i);
    }
    v.push_back(2);
 
    // 注意:容器必须是有序的序列,如果是无序序列,结果未知
    int ret = binary_search(v.begin(), v.end(), 9);
    if (ret) {
        cout << "找到了" << endl;
    } else {
        cout << "没有找到" << endl;
    }
}
 
//常用查找算法 count 统计内置数据类型
void test09() {
    vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(1);
    v.push_back(3);
    // 开始迭代器 结束迭代器 统计的元素
    cout << count(v.begin(), v.end(), 1) << endl;
}
 
//常用查找算法 count 统计自定义数据类型
class Person3 {
public:
    Person3(string name, int age) {
        this->m_Name = name;
        this->m_Age = age;
    }
 
    // 重载== 底层find知道如何对比Person3数据类型
    bool operator==(const Person3 &p) {
        if (this->m_Name == p.m_Name && this->m_Age == p.m_Age) {
            return true;
        } else {
            return false;
        }
    }
 
    string m_Name;
    int m_Age;
};
 
void test10() {
// 创建一个vector容器,数组
    vector<Person3> v;
 
    //向容器中插入数据
    Person3 p1("aa", 1);
    Person3 p2("a2", 2);
    Person3 p3("a3", 3);
    Person3 p4("aa", 1);
    Person3 p5("a5", 5);
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);
    Person3 p("aa", 1);
    cout << count(v.begin(), v.end(), p) << endl;
}
 
//常用的查找泛 count_if 统计内置数据类型
class Greater19 {
public:
    bool operator()(int val) {
        return val > 19;
    }
};
 
void test11() {
    vector<int> v;
    v.push_back(10);
    v.push_back(40);
    v.push_back(30);
    v.push_back(20);
    v.push_back(40);
    v.push_back(20);
    cout << count_if(v.begin(), v.end(), Greater19()) << endl;
}
 
//常用的查找泛 count_if 统计自定义数据类型
class Person4 {
public:
    Person4(string name, int age) {
        this->m_Name = name;
        this->m_Age = age;
    }
 
 
    string m_Name;
    int m_Age;
};
 
class AgeGreater20 {
public:
    bool operator()(const Person4 &p) {
        return p.m_Age > 20;
    }
};
 
void test12() {
// 创建一个vector容器,数组
    vector<Person4> v;
 
    //向容器中插入数据
    Person4 p1("aa", 1);
    Person4 p2("a2", 20);
    Person4 p3("a3", 30);
    Person4 p4("aa", 10);
    Person4 p5("a5", 50);
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);
    cout << count_if(v.begin(), v.end(), AgeGreater20()) << endl;
}

三、常用排序算法

//3、常用排序算法
//常用排序算法sort
void myPrint(int val) {
    cout << val << " ";
}
 
void test13() {
    vector<int> v;
    v.push_back(10);
    v.push_back(20);
    v.push_back(50);
    v.push_back(20);
    v.push_back(40);
    v.push_back(60);
    // 从小到大
    sort(v.begin(), v.end());
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;
    // 从大到小
    sort(v.begin(), v.end(), greater<int>());
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;
 
}
 
//常用排序算法 random_shuffle 指定范围内元素的顺序打乱
void test14() {
    vector<int> v;
    v.push_back(10);
    v.push_back(20);
    v.push_back(50);
    v.push_back(20);
    v.push_back(40);
    v.push_back(60);
    // 打乱次序
    random_shuffle(v.begin(), v.end());
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;
}
 
//常用排序算法 merge
void test15() {
    vector<int> v1;
    vector<int> v2;
    for (int i = 0; i < 10; ++i) {
        v1.push_back(i);
        v2.push_back(i + 1);
    }
    for_each(v1.begin(), v1.end(), myPrint);
    cout << endl;
    for_each(v2.begin(), v2.end(), myPrint);
    cout << endl;
    vector<int> vTarget;
    // 提前准备空间
    vTarget.resize(v1.size() + v2.size());
    // 合并两个有序序列
    merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
    for_each(vTarget.begin(), vTarget.end(), myPrint);
}
 
//常用排序算法reverse
void test16() {
    vector<int> v;
    v.push_back(10);
    v.push_back(20);
    v.push_back(50);
    v.push_back(20);
    v.push_back(40);
    v.push_back(60);
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;
    // 反转元素
    reverse(v.begin(), v.end());
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;
}

四、常用拷贝和替换算法

//4、常用拷贝和替换算法
// 常用拷贝和替换算法 copy
void test17() {
    vector<int> v1;
    for (int i = 0; i < 10; ++i) {
        v1.push_back(i);
    }
    vector<int> v2;
    v2.resize(v1.size());
    // 拷贝元素
    copy(v1.begin(), v1.end(), v2.begin());
    for_each(v2.begin(), v2.end(), myPrint);
    cout << endl;
}
 
//常用拷贝和替换算法 replace
void test18() {
    vector<int> v;
    v.push_back(20);
    v.push_back(30);
    v.push_back(50);
    v.push_back(40);
    v.push_back(20);
    v.push_back(10);
    v.push_back(20);
 
    replace(v.begin(), v.end(), 20, 200);
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;
}
 
//常用拷贝和替换算法 replace_if
class GreaterTen {
public:
    bool operator()(int val) {
        return val > 10;
    }
};
 
void test19() {
    vector<int> v;
    v.push_back(20);
    v.push_back(30);
    v.push_back(5);
    v.push_back(40);
    v.push_back(20);
    v.push_back(10);
    v.push_back(20);
 
    replace_if(v.begin(), v.end(), GreaterTen(), 200);
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;
}
 
//常用拷贝和替换算法 swap
void test20() {
    vector<int> v1;
    vector<int> v2;
    for (int i = 0; i < 10; ++i) {
        v1.push_back(i);
        v2.push_back(i + 100);
    }
    cout << "交换前:" << endl;
    for_each(v1.begin(), v1.end(), myPrint);
    cout << endl;
    for_each(v2.begin(), v2.end(), myPrint);
    cout << endl;
    swap(v1, v2);
    cout << "交换后:" << endl;
    for_each(v1.begin(), v1.end(), myPrint);
    cout << endl;
    for_each(v2.begin(), v2.end(), myPrint);
    cout << endl;
 
}

五、常用算数生成算法

//5、常用算术生成算法
// 常用算术生成算法 accumulate
void test21() {
    vector<int> v1;
    for (int i = 0; i <= 100; ++i) {
        v1.push_back(i);
    }
    // 计算容器累加总和
    cout << "total=" << accumulate(v1.begin(), v1.end(), 0) << endl;
}
 
//常用算术生成算法 fill
void test22() {
    vector<int> v1;
    v1.resize(10);
    // 填充
    fill(v1.begin(), v1.end(), 100);
    // 计算容器累加总和
    for_each(v1.begin(), v1.end(), myPrint);
}

六、常用集合算法

//6、常用集合算法
void test23() {
    // 有序序列
    vector<int> v1;
    vector<int> v2;
    for (int i = 0; i < 10; ++i) {
        v1.push_back(i);
        v2.push_back(i + 5);
    }
    vector<int> vTarget;
    vTarget.resize(min(v1.size(), v2.size()));
    // 两个容器的交集
    vector<int>::iterator itEnd = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
    for_each(vTarget.begin(), itEnd, myPrint);
    cout << endl;
 
    vector<int> v2Target;
    v2Target.resize(plus<int>()(v1.size(), v2.size()));
    // 两个容器的并集
    vector<int>::iterator itEnd2 = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v2Target.begin());
    for_each(v2Target.begin(), itEnd2, myPrint);
    cout << endl;
 
    vector<int> v3Target;
    v3Target.resize(max(v1.size(), v2.size()));
    // 两个容器的差集
    vector<int>::iterator itEnd3 = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v3Target.begin());
    for_each(v3Target.begin(), itEnd3, myPrint);
    cout << endl;
 
    vector<int> v4Target;
    v4Target.resize(max(v1.size(), v2.size()));
    // 两个容器的差集
    vector<int>::iterator itEnd4 = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), v4Target.begin());
    for_each(v4Target.begin(), itEnd4, myPrint);
    cout << endl;
}

七、全部代码

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <numeric>
#include <string>
#include <ctime>
 
using namespace std;
 
//1、常用遍历算法
// for_each遍历
void print01(int val) {
    cout << val << " ";
}
 
class print02 {
public:
    void operator()(int val) {
        cout << val << " ";
    }
};
 
void test01() {
    vector<int> v;
    for (int i = 0; i < 10; ++i) {
        v.push_back(i);
    }
    // 普通函数
    for_each(v.begin(), v.end(), print01);
    cout << endl;
    // 仿函数
    for_each(v.begin(), v.end(), print02());
    cout << endl;
}
 
// transform
int change(int val) {
    return val * 10;
}
 
class Transform {
public:
    int operator()(int val) {
        return val;
    }
};
 
void test02() {
    vector<int> v;
    for (int i = 0; i < 10; ++i) {
        v.push_back(i);
    }
    for_each(v.begin(), v.end(), print01);
    cout << endl;
 
    vector<int> v2;
    // 开辟空间
    v2.resize(v.size());
    //源容器开始迭代器 源容器结束迭代器 目标容器开始迭代器 函数或者函数对象
    // transform(v.begin(), v.end(), v2.begin(), Transform());
    transform(v.begin(), v.end(), v2.begin(), change);
    for_each(v2.begin(), v2.end(), print01);
    cout << endl;
}
 
//2、常用查找算法
// 常用查找算法 find
// 查找内置
void test03() {
    vector<int> v;
    for (int i = 0; i < 10; ++i) {
        v.push_back(i);
    }
    vector<int>::iterator it = find(v.begin(), v.end(), 56);
    if (it == v.begin()) {
        cout << "没有找到元素" << endl;
    } else {
        cout << "找到了元素:" << *it << endl;
    }
}
 
// 查找自定义
class Person {
public:
    Person(string name, int age) {
        this->m_Name = name;
        this->m_Age = age;
    }
 
    // 重载== 底层find知道如何对比person数据类型
    bool operator==(const Person &p) {
        if (this->m_Name == p.m_Name && this->m_Age == p.m_Age) {
            return true;
        } else {
            return false;
        }
    }
 
    string m_Name;
    int m_Age;
};
 
void printPerson(Person p) {
    cout << "姓名:" << p.m_Name << " 年龄:" << p.m_Age << endl;
}
 
void test04() {
    vector<Person> v;
    Person p1("aa", 11);
    Person p2("bb", 12);
    v.push_back(p1);
    v.push_back(p2);
    for_each(v.begin(), v.end(), printPerson);
 
    vector<Person>::iterator it = find(v.begin(), v.end(), p2);
    if (it != v.begin()) {
        cout << "找到了" << endl;
        printPerson(*it);
    } else {
        cout << "没有找到" << endl;
    }
}
 
// 查找find_if 内置数据类型
class GreaterFive {
public:
    bool operator()(int val) {
        return val > 5;
    }
};
 
void test05() {
    vector<int> v1;
    for (int i = 0; i < 10; ++i) {
        v1.push_back(i);
    }
    // 开始迭代器 结束迭代器 _Pred函数或者谓词(返回bool类型的仿函数)
    vector<int>::iterator pos = find_if(v1.begin(), v1.end(), GreaterFive());
    if (pos == v1.end()) {
        cout << "没有找到" << endl;
    } else {
        cout << "找到了" << *pos << endl;
    }
}
 
// 查找find_if 自定义数据类型
class Person2 {
public:
    Person2(string name, int age) {
        this->m_Name = name;
        this->m_Age = age;
    }
 
    string m_Name;
    int m_Age;
};
 
void printPerson2(Person2 p) {
    cout << "姓名:" << p.m_Name << " 年龄:" << p.m_Age << endl;
}
 
class Greater20 {
public:
    bool operator()(Person2 &p) {
        return p.m_Age > 20;
    }
};
 
void test06() {
    vector<Person2> v;
    Person2 p1("aa", 10);
    Person2 p2("bb", 20);
    Person2 p3("cc", 30);
    Person2 p4("dd", 40);
    Person2 p5("ee", 50);
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);
    for_each(v.begin(), v.end(), printPerson2);
 
    vector<Person2>::iterator it = find_if(v.begin(), v.end(), Greater20());
    if (it != v.begin()) {
        cout << "找到了" << endl;
        printPerson2(*it);
    } else {
        cout << "没有找到" << endl;
    }
}
 
// 常用查找算法 adjacent_find
void pirntInt(int val) {
    cout << val << " ";
}
 
void test07() {
    vector<int> v;
    v.push_back(0);
    v.push_back(2);
    v.push_back(0);
    v.push_back(3);
    v.push_back(1);
    v.push_back(4);
    v.push_back(3);
    v.push_back(3);
    vector<int>::iterator pos = adjacent_find(v.begin(), v.end());
    if (pos != v.end()) {
        cout << "找到了" << endl;
        for_each(pos, v.end(), pirntInt);
        cout << endl;
    } else {
        cout << "位找到" << endl;
    }
}
 
//binary_search 二分查找
void test08() {
    vector<int> v;
    for (int i = 0; i < 10; i++) {
        v.push_back(i);
    }
    v.push_back(2);
 
    // 注意:容器必须是有序的序列,如果是无序序列,结果未知
    int ret = binary_search(v.begin(), v.end(), 9);
    if (ret) {
        cout << "找到了" << endl;
    } else {
        cout << "没有找到" << endl;
    }
}
 
//常用查找算法 count 统计内置数据类型
void test09() {
    vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(1);
    v.push_back(3);
    // 开始迭代器 结束迭代器 统计的元素
    cout << count(v.begin(), v.end(), 1) << endl;
}
 
//常用查找算法 count 统计自定义数据类型
class Person3 {
public:
    Person3(string name, int age) {
        this->m_Name = name;
        this->m_Age = age;
    }
 
    // 重载== 底层find知道如何对比Person3数据类型
    bool operator==(const Person3 &p) {
        if (this->m_Name == p.m_Name && this->m_Age == p.m_Age) {
            return true;
        } else {
            return false;
        }
    }
 
    string m_Name;
    int m_Age;
};
 
void test10() {
// 创建一个vector容器,数组
    vector<Person3> v;
 
    //向容器中插入数据
    Person3 p1("aa", 1);
    Person3 p2("a2", 2);
    Person3 p3("a3", 3);
    Person3 p4("aa", 1);
    Person3 p5("a5", 5);
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);
    Person3 p("aa", 1);
    cout << count(v.begin(), v.end(), p) << endl;
}
 
//常用的查找泛 count_if 统计内置数据类型
class Greater19 {
public:
    bool operator()(int val) {
        return val > 19;
    }
};
 
void test11() {
    vector<int> v;
    v.push_back(10);
    v.push_back(40);
    v.push_back(30);
    v.push_back(20);
    v.push_back(40);
    v.push_back(20);
    cout << count_if(v.begin(), v.end(), Greater19()) << endl;
}
 
//常用的查找泛 count_if 统计自定义数据类型
class Person4 {
public:
    Person4(string name, int age) {
        this->m_Name = name;
        this->m_Age = age;
    }
 
 
    string m_Name;
    int m_Age;
};
 
class AgeGreater20 {
public:
    bool operator()(const Person4 &p) {
        return p.m_Age > 20;
    }
};
 
void test12() {
// 创建一个vector容器,数组
    vector<Person4> v;
 
    //向容器中插入数据
    Person4 p1("aa", 1);
    Person4 p2("a2", 20);
    Person4 p3("a3", 30);
    Person4 p4("aa", 10);
    Person4 p5("a5", 50);
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);
    cout << count_if(v.begin(), v.end(), AgeGreater20()) << endl;
}
 
//3、常用排序算法
//常用排序算法sort
void myPrint(int val) {
    cout << val << " ";
}
 
void test13() {
    vector<int> v;
    v.push_back(10);
    v.push_back(20);
    v.push_back(50);
    v.push_back(20);
    v.push_back(40);
    v.push_back(60);
    // 从小到大
    sort(v.begin(), v.end());
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;
    // 从大到小
    sort(v.begin(), v.end(), greater<int>());
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;
 
}
 
//常用排序算法 random_shuffle 指定范围内元素的顺序打乱
void test14() {
    vector<int> v;
    v.push_back(10);
    v.push_back(20);
    v.push_back(50);
    v.push_back(20);
    v.push_back(40);
    v.push_back(60);
    // 打乱次序
    random_shuffle(v.begin(), v.end());
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;
}
 
//常用排序算法 merge
void test15() {
    vector<int> v1;
    vector<int> v2;
    for (int i = 0; i < 10; ++i) {
        v1.push_back(i);
        v2.push_back(i + 1);
    }
    for_each(v1.begin(), v1.end(), myPrint);
    cout << endl;
    for_each(v2.begin(), v2.end(), myPrint);
    cout << endl;
    vector<int> vTarget;
    // 提前准备空间
    vTarget.resize(v1.size() + v2.size());
    // 合并两个有序序列
    merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
    for_each(vTarget.begin(), vTarget.end(), myPrint);
}
 
//常用排序算法reverse
void test16() {
    vector<int> v;
    v.push_back(10);
    v.push_back(20);
    v.push_back(50);
    v.push_back(20);
    v.push_back(40);
    v.push_back(60);
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;
    // 反转元素
    reverse(v.begin(), v.end());
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;
}
 
//4、常用拷贝和替换算法
// 常用拷贝和替换算法 copy
void test17() {
    vector<int> v1;
    for (int i = 0; i < 10; ++i) {
        v1.push_back(i);
    }
    vector<int> v2;
    v2.resize(v1.size());
    // 拷贝元素
    copy(v1.begin(), v1.end(), v2.begin());
    for_each(v2.begin(), v2.end(), myPrint);
    cout << endl;
}
 
//常用拷贝和替换算法 replace
void test18() {
    vector<int> v;
    v.push_back(20);
    v.push_back(30);
    v.push_back(50);
    v.push_back(40);
    v.push_back(20);
    v.push_back(10);
    v.push_back(20);
 
    replace(v.begin(), v.end(), 20, 200);
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;
}
 
//常用拷贝和替换算法 replace_if
class GreaterTen {
public:
    bool operator()(int val) {
        return val > 10;
    }
};
 
void test19() {
    vector<int> v;
    v.push_back(20);
    v.push_back(30);
    v.push_back(5);
    v.push_back(40);
    v.push_back(20);
    v.push_back(10);
    v.push_back(20);
 
    replace_if(v.begin(), v.end(), GreaterTen(), 200);
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;
}
 
//常用拷贝和替换算法 swap
void test20() {
    vector<int> v1;
    vector<int> v2;
    for (int i = 0; i < 10; ++i) {
        v1.push_back(i);
        v2.push_back(i + 100);
    }
    cout << "交换前:" << endl;
    for_each(v1.begin(), v1.end(), myPrint);
    cout << endl;
    for_each(v2.begin(), v2.end(), myPrint);
    cout << endl;
    swap(v1, v2);
    cout << "交换后:" << endl;
    for_each(v1.begin(), v1.end(), myPrint);
    cout << endl;
    for_each(v2.begin(), v2.end(), myPrint);
    cout << endl;
 
}
 
//5、常用算术生成算法
// 常用算术生成算法 accumulate
void test21() {
    vector<int> v1;
    for (int i = 0; i <= 100; ++i) {
        v1.push_back(i);
    }
    // 计算容器累加总和
    cout << "total=" << accumulate(v1.begin(), v1.end(), 0) << endl;
}
 
//常用算术生成算法 fill
void test22() {
    vector<int> v1;
    v1.resize(10);
    // 填充
    fill(v1.begin(), v1.end(), 100);
    // 计算容器累加总和
    for_each(v1.begin(), v1.end(), myPrint);
}
 
//6、常用集合算法
void test23() {
    // 有序序列
    vector<int> v1;
    vector<int> v2;
    for (int i = 0; i < 10; ++i) {
        v1.push_back(i);
        v2.push_back(i + 5);
    }
    vector<int> vTarget;
    vTarget.resize(min(v1.size(), v2.size()));
    // 两个容器的交集
    vector<int>::iterator itEnd = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
    for_each(vTarget.begin(), itEnd, myPrint);
    cout << endl;
 
    vector<int> v2Target;
    v2Target.resize(plus<int>()(v1.size(), v2.size()));
    // 两个容器的并集
    vector<int>::iterator itEnd2 = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v2Target.begin());
    for_each(v2Target.begin(), itEnd2, myPrint);
    cout << endl;
 
    vector<int> v3Target;
    v3Target.resize(max(v1.size(), v2.size()));
    // 两个容器的差集
    vector<int>::iterator itEnd3 = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v3Target.begin());
    for_each(v3Target.begin(), itEnd3, myPrint);
    cout << endl;
 
    vector<int> v4Target;
    v4Target.resize(max(v1.size(), v2.size()));
    // 两个容器的差集
    vector<int>::iterator itEnd4 = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), v4Target.begin());
    for_each(v4Target.begin(), itEnd4, myPrint);
    cout << endl;
}
 
int main() {
    // 随机数种子
    srand((unsigned int) time(NULL));
    // test01();
    // test02();
    // test03();
    // test04();
    // test05();
    // test06();
    // test07();
    // test08();
    // test09();
    // test10();
    // test11();
    // test12();
    // test13();
    // test14();
    // test15();
    // test16();
    // test17();
    // test18();
    // test19();
    // test20();
    // test21();
    // test22();
    test23();
    system("pause");
    return 0;
}
 
相关文章
|
4月前
|
存储 监控 算法
基于 C++ 哈希表算法实现局域网监控电脑屏幕的数据加速机制研究
企业网络安全与办公管理需求日益复杂的学术语境下,局域网监控电脑屏幕作为保障信息安全、规范员工操作的重要手段,已然成为网络安全领域的关键研究对象。其作用类似网络空间中的 “电子眼”,实时捕获每台电脑屏幕上的操作动态。然而,面对海量监控数据,实现高效数据存储与快速检索,已成为提升监控系统性能的核心挑战。本文聚焦于 C++ 语言中的哈希表算法,深入探究其如何成为局域网监控电脑屏幕数据处理的 “加速引擎”,并通过详尽的代码示例,展现其强大功能与应用价值。
103 2
|
5月前
|
存储 算法 C++
Windows共享文件:探秘C++实现的B树索引算法奇境
在数字化时代,Windows共享文件的高效管理至关重要。B树算法以其自平衡多路搜索特性,在文件索引与存储优化中表现出色。本文探讨B树在Windows共享文件中的应用,通过C++实现具体代码,展示其构建文件索引、优化数据存储的能力,提升文件检索效率。B树通过减少磁盘I/O操作,确保查询高效,为企业和个人提供流畅的文件共享体验。
|
6月前
|
编译器 C++ 容器
【c++丨STL】基于红黑树模拟实现set和map(附源码)
本文基于红黑树的实现,模拟了STL中的`set`和`map`容器。通过封装同一棵红黑树并进行适配修改,实现了两种容器的功能。主要步骤包括:1) 修改红黑树节点结构以支持不同数据类型;2) 使用仿函数适配键值比较逻辑;3) 实现双向迭代器支持遍历操作;4) 封装`insert`、`find`等接口,并为`map`实现`operator[]`。最终,通过测试代码验证了功能的正确性。此实现减少了代码冗余,展示了模板与仿函数的强大灵活性。
169 2
|
2月前
|
存储 监控 算法
基于跳表数据结构的企业局域网监控异常连接实时检测 C++ 算法研究
跳表(Skip List)是一种基于概率的数据结构,适用于企业局域网监控中海量连接记录的高效处理。其通过多层索引机制实现快速查找、插入和删除操作,时间复杂度为 $O(\log n)$,优于链表和平衡树。跳表在异常连接识别、黑名单管理和历史记录溯源等场景中表现出色,具备实现简单、支持范围查询等优势,是企业网络监控中动态数据管理的理想选择。
71 0
|
3月前
|
存储 机器学习/深度学习 算法
基于 C++ 的局域网访问控制列表(ACL)实现及局域网限制上网软件算法研究
本文探讨局域网限制上网软件中访问控制列表(ACL)的应用,分析其通过规则匹配管理网络资源访问的核心机制。基于C++实现ACL算法原型,展示其灵活性与安全性。文中强调ACL在企业与教育场景下的重要作用,并提出性能优化及结合机器学习等未来研究方向。
96 4
|
4月前
|
监控 算法 数据处理
基于 C++ 的 KD 树算法在监控局域网屏幕中的理论剖析与工程实践研究
本文探讨了KD树在局域网屏幕监控中的应用,通过C++实现其构建与查询功能,显著提升多维数据处理效率。KD树作为一种二叉空间划分结构,适用于屏幕图像特征匹配、异常画面检测及数据压缩传输优化等场景。相比传统方法,基于KD树的方案检索效率提升2-3个数量级,但高维数据退化和动态更新等问题仍需进一步研究。未来可通过融合其他数据结构、引入深度学习及开发增量式更新算法等方式优化性能。
139 17
|
3月前
|
机器学习/深度学习 存储 算法
基于 C++ 布隆过滤器算法的局域网上网行为控制:URL 访问过滤的高效实现研究
本文探讨了一种基于布隆过滤器的局域网上网行为控制方法,旨在解决传统黑白名单机制在处理海量URL数据时存储与查询效率低的问题。通过C++实现URL访问过滤功能,实验表明该方法可将内存占用降至传统方案的八分之一,查询速度提升约40%,假阳性率可控。研究为优化企业网络管理提供了新思路,并提出结合机器学习、改进哈希函数及分布式协同等未来优化方向。
84 0
|
5月前
|
存储 监控 算法
基于 C++ 哈希表算法的局域网如何监控电脑技术解析
当代数字化办公与生活环境中,局域网的广泛应用极大地提升了信息交互的效率与便捷性。然而,出于网络安全管理、资源合理分配以及合规性要求等多方面的考量,对局域网内计算机进行有效监控成为一项至关重要的任务。实现局域网内计算机监控,涉及多种数据结构与算法的运用。本文聚焦于 C++ 编程语言中的哈希表算法,深入探讨其在局域网计算机监控场景中的应用,并通过详尽的代码示例进行阐释。
115 4
|
6月前
|
存储 算法 安全
企业员工数据泄露防范策略:基于 C++ 语言的布隆过滤器算法剖析[如何防止员工泄密]
企业运营过程中,防范员工泄密是信息安全领域的核心议题。员工泄密可能致使企业核心数据、商业机密等关键资产的流失,进而给企业造成严重损失。为应对这一挑战,借助恰当的数据结构与算法成为强化信息防护的有效路径。本文专注于 C++ 语言中的布隆过滤器算法,深入探究其在防范员工泄密场景中的应用。
113 8
|
7月前
|
编译器 C++ 开发者
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。

热门文章

最新文章