传智播客C++视频学习笔记(5)

#include <iostream>

using namespace std;

void swapInt(int& a, int& b)
{
    int temp = a;
    a = b;
    b = temp;
}

void swapDouble(double& a, double& b)
{
    double temp = a;
    a = b;
    b = temp;
}

//模板技术
template<typename T>
void mySwap(T& t1, T& t2)
{
    T temp = t1;
    t1 = t2;
    t2 = temp;
}

void test01()
{
    int a = 10;
    int b = 20;

    //自动类型推导
    mySwap(a, b);

    //显示指定类型
    //mySwap<int>(a, b);

    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
}

int main()
{
    test01();

    system("pause");

    return 0;
}
函数模板语法
#include <iostream>

using namespace std;

template<typename T>
void swapMy(T& t1, T& t2)
{
    T temp = t1;
    t1 = t2;
    t2 = temp;
}

void test01()
{
    int a = 10;
    int b = 20;
    char c = 'c';

    //自动类型推导,必须推导出一致的数据类型T才可以使用
    //swapMy(a, c); //错误
    swapMy(a, b);   //正确
}

template<typename T>
void func()
{
    cout << "func()调用" << endl;
}

void test02()
{
    //模板必须要确定出T的数据类型才可以使用
    func<int>();
}

int main()
{
    test01();

    test02();

    system("pause");

    return 0;
}
函数模板注意事项
#include <iostream>

using namespace std;

//利用函数模板封装一个排序的函数,可以对不同数据类型数组进行排序
//排序规则从大到小,排序算法为选择排序
//分别利用int数组和char数组进行测试

template<typename T>
void swapMy(T& t1, T& t2)
{
    T temp = t1;
    t1 = t2;
    t2 = temp;
}

template<typename T>
void sortMy(T arr[], int len)
{
    for (int i = 0; i < len; i++)
    {
        int max = i;
        for (int j = i + 1; j < len; j++)
        {
            if (arr[j] > arr[max])
            {
                max = j;
            }
        }
        if (max != i)
        {
            swapMy(arr[max], arr[i]);
        }
    }
}

template<typename T>
void printMy(T arr[], int len)
{
    for (int i = 0; i < len; i++)
    {
        cout << arr[i] << " ";
        cout << endl;
    }
}

void test01()
{
    int arr[] = { 1,2,3,4,5,6,7,8,9 };
    int len = sizeof(arr) / sizeof(int);
    sortMy(arr, len);
    printMy(arr, len);
}

void test02()
{
    char arr[] = "abcdefg";
    int len = sizeof(arr) / sizeof(char);
    sortMy(arr, len);
    printMy(arr, len);
}

int main()
{
    test01();

    test02();

    system("pause");

    return 0;
}
函数模板案例
#include <iostream>

using namespace std;

//普通函数调用时可以发生自动类型转换
//函数模板调用时,如果利用自动类型推导,不会发生自动类型转换
//如果利用显示指定类型的方式,可以发生自动类型转换

//普通函数
int myAdd01(int a, int b)
{
    return a + b;
}

//函数模板
template <typename T>
T myAdd02(T a, T b)
{
    return a + b;
}

void test01()
{
    int a = 10;
    int b = 20;
    char c = 'c';

    cout << myAdd01(a, c) << endl;

    //myAdd02(a, c); //错误

    cout << myAdd02<int>(a, c) << endl;
}

int main()
{
    test01();

    system("pause");

    return 0;
}
普通函数与函数模板的区别
#include <iostream>

using namespace std;

//如果函数模板和普通函数都可以实现,优先调用普通函数
//可以通过空模板参数列表来强制调用函数模板
//函数模板也可以发生重载
//如果函数模板可以产生更好的匹配,优先调用函数模板

//普通函数
void myPrint(int a, int b)
{
    cout << "普通函数调用" << endl;
}

//函数模板
template <typename T>
void myPrint(T a, T b)
{
    cout << "函数模板调用" << endl;
}

//重载函数模板
template <typename T>
void myPrint(T a, T b, T c)
{
    cout << "重载函数模板调用" << endl;
}

void test01()
{
    int a = 10;
    int b = 20;
    int c = 30;

    myPrint(a, b); //普通函数调用

    myPrint<>(a, b); //函数模板调用

    myPrint(a, b, c); //重载函数模板调用

    char c1 = 'a';
    char c2 = 'b';
    myPrint(c1, c2); //函数模板调用
}

int main()
{
    test01();

    system("pause");

    return 0;
}
普通函数与函数模板的调用规则
#include <iostream>

using namespace std;

//模板的通用性并不是万能的

//如果a和b传入的是数组,就无法实现了
template <typename T>
void t1(T a, T b)
{
    a = b;
}

//如果传入的是Preson自定义数据类型,也无法实现
template <typename T>
void t2(T a, T b)
{

    if (a > b)
    {

    }
}

//C++为了解决这种问题,提供模板的重载,可以为这些特定的类型提供 具体化 模板

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

//普通函数模板
template <typename T>
bool myCompare(T& a, T& b)
{
    if (a == b)
    {
        return true;
    }
    else
    {
        return false;
    }
}

//具体化模板
//具体化优先于常规模板
template<> bool myCompare(Preson& p1, Preson& p2)
{
    if (p1.m_name == p2.m_name && p1.m_age == p2.m_age)
    {
        return true;
    }
    else
    {
        return false;
    }
}

void test01()
{
    int a = 10;
    int b = 20;

    //内置数据类型可以直接使用通用的函数模板
    bool bl = myCompare(a, b);
    if (bl)
    {
        cout << "a = b" << endl;
    }
    else
    {
        cout << "a != b" << endl;
    }
}

void test02()
{
    Preson p1("张三", 20);
    Preson p2("张三", 20);

    //自定义数据类型,不会调用普通函数模板
    //可以创建 具体化 Preson数据类型模板
    bool bl = myCompare(p1, p2);
    if (bl)
    {
        cout << "p1 = p2" << endl;
    }
    else
    {
        cout << "p1 != p2" << endl;
    }
}

int main()
{
    test01();

    test02();

    system("pause");

    return 0;
}
模板的局限性
#include <iostream>

using namespace std;

template<class NameType, class AgeType>
class Preson
{
public:
    NameType m_name;
    AgeType m_age;
public:
    Preson(NameType name, AgeType age)
    {
        this->m_name = name;
        this->m_age = age;
    }
    void showPreson()
    {
        cout << "name = " << this->m_name << endl;
        cout << "age  = " << this->m_age << endl;
    }
};

void test01()
{
    Preson<string, int> p("张三", 20);
    p.showPreson();
}

int main()
{
    test01();

    system("pause");

    return 0;
}
类模板语法
#include <iostream>

using namespace std;

//类模板没有自动类型推导的使用方式
//类模板在模板参数列表中可以有默认参数

template<class NameType, class AgeType = int>
class Preson
{
public:
    NameType m_name;
    AgeType m_age;
public:
    Preson(NameType name, AgeType age)
    {
        this->m_name = name;
        this->m_age = age;
    }
    void showPreson()
    {
        cout << "name = " << this->m_name << endl;
        cout << "age  = " << this->m_age << endl;
    }
};

void test01()
{

    //Preson p1("张三", 20); //错误

    Preson<string, int> p2("张三", 20);
    p2.showPreson();
}

void test02()
{
    Preson<string> p("李四", 21);
    p.showPreson();
}

int main()
{
    test01();

    test02();

    system("pause");

    return 0;
}
类模板与函数模板的区别
#include <iostream>

using namespace std;

//普通类中成员函数一开始就可以创建
//类模板中成员函数在调用时才创建

class Preson1
{
public:
    void showPreson1()
    {
        cout << "showPreson1()函数调用" << endl;
    }
};

class Preson2
{
public:
    void showPreson2()
    {
        cout << "showPreson2()函数调用" << endl;
    }
};

template<class T>
class myClass
{
public:
    T t;
public:
    void func1()
    {
        t.showPreson1();
    }
    void func2()
    {
        t.showPreson2();
    }
};

void test01()
{
    myClass<Preson1> mc;
    mc.func1();

    //错误,函数调用时才会创建成员函数
    //mc.func2();
}

int main()
{
    test01();

    system("pause");

    return 0;
}
类模板中成员函数创建时机
#include <iostream>

using namespace std;

//类模板实例化出的对象,向函数传参的方式
//一共有三种传递方式:
//指定传入的类型,直接显示对象的数据类型
//参数模板化,将对象中的参数变为模板进行传递
//整个类模板化,将这个对象类型模板化进行传递

//类模板
template<class NameType, class AgeType>
class Preson
{
public:
    NameType m_name;
    AgeType m_age;
public:
    Preson(NameType name, AgeType age)
    {
        this->m_name = name;
        this->m_age = age;
    }
    void showPreson()
    {
        cout << "name = " << this->m_name << endl;
        cout << "age  = " << this->m_age << endl;
    }
};

//1、指定传入的类型
void printPreson1(Preson<string, int>& p)
{
    p.showPreson();
}
void test01()
{
    Preson<string, int>p("张三", 20);
    printPreson1(p);
}

//2、参数模板化
template<class T1, class T2>
void printPreson2(Preson<T1, T2>& p)
{
    p.showPreson();

    cout << "T1的类型为: " << typeid(T1).name() << endl;
    cout << "T2的类型为: " << typeid(T2).name() << endl;
}
void test02()
{
    Preson<string, int>p("李四", 21);
    printPreson2(p);
}

//3、整个类模板化
template<class T>
void printPreson3(T& p)
{
    p.showPreson();
    cout << "T的类型为: " << typeid(T).name() << endl;
}
void test03()
{
    Preson<string, int>p("王五", 22);
    printPreson3(p);
}

int main()
{
    test01();
    test02();
    test03();

    system("pause");

    return 0;
}
类模板对象做函数参数
#include <iostream>

using namespace std;

//当子类继承的父类是一个模板时,子类在声明的时候,要指定出父类中T的类型
//如果不指定,编译器无法给子类分配内存
//如果想灵活指定出父类中T的类型,子类也需变为类模板

template<class T>
class Base
{
    T t;
};

//class Son :public Base
//{
//
//};

class Son1 :public Base<int> //必须指定一个类型
{

};
void test01()
{
    Son1 s1;
}

template<class T1, class T2>
class Son2 :public Base<T2>
{
public:
    Son2()
    {
        cout << typeid(T1).name() << endl;
        cout << typeid(T2).name() << endl;
    }
};
void test02()
{
    Son2<string, int>s;
}

int main()
{
    test01();

    test02();

    system("pause");

    return 0;
}
类模板与继承
#include <iostream>

using namespace std;

template<class T1, class T2>
class Preson
{
public:
    T1 m_name;
    T2 m_age;
public:
    Preson(T1 name, T2 age);
    void showPreson();
};

//构造函数类外实现
template<class T1, class T2>
Preson<T1, T2>::Preson(T1 name, T2 age)
{
    this->m_name = name;
    this->m_age = age;
}

//成员函数类外实现
template<class T1, class T2>
void Preson<T1, T2>::showPreson()
{
    cout << "name = " << this->m_name << endl;
    cout << "age = " << this->m_age << endl;
}

void test01()
{
    Preson<string, int>p("张三", 20);
    p.showPreson();
}

int main()
{
    test01();

    system("pause");

    return 0;
}
类模板成员函数类外实现
#pragma once

#include<iostream>

using namespace std;

//类模板分文件编写出现的问题:
//类模板中成员函数创建时机是在调用阶段,导致分文件编写时链接不到

//解决方法:
//直接包含.cpp源文件
//将声明和实现写在一个文件中,并更改文件后缀名为.hpp

template<class T1, class T2>
class Preson
{
public:
    T1 m_name;
    T2 m_age;
public:
    Preson(T1 name, T2 age);
    void showPreson();
};

template<class T1, class T2>
Preson<T1, T2>::Preson(T1 name, T2 age)
{
    this->m_name = name;
    this->m_age = age;
}

template<class T1, class T2>
void Preson<T1, T2>::showPreson()
{
    cout << "name  = " << this->m_name << endl;
    cout << "age  = " << this->m_age << endl;
}
类模板分文件编写(.hpp)
#include <iostream>

using namespace std;

#include "Preson.hpp"

void test01()
{
    Preson<string, int>p("张三", 100);
    p.showPreson();
}

int main()
{
    test01();

    system("pause");

    return 0;
}
类模板分文件编写(.cpp)
#include <iostream>

using namespace std;

//全局函数类内实现,直接在类内声明友元即可
//全局函数类外实现,需要让编译器提前知道全局函数的存在

//02
template <class T1, class T2>
class Preson;

//01
template <class T1, class T2>
void printPreson2(Preson<T1, T2>& p)
{
    cout << "类外name = " << p.m_name << endl;
    cout << "类外age = " << p.m_age << endl;
}

template <class T1, class T2>
class Preson
{
    //全局函数配合友元,类内实现
    friend void printPreson1(Preson<T1, T2>& p)
    {
        cout << "name = " << p.m_name << endl;
        cout << "age = " << p.m_age << endl;
    }

    //全局函数配合友元,类外实现
    friend void printPreson2<>(Preson<T1, T2>& p);

private:
    T1 m_name;
    T2 m_age;
public:
    Preson(T1 name, T2 age)
    {
        this->m_name = name;
        this->m_age = age;
    }
};

void test01()
{
    Preson<string, int>p("David", 20);
    printPreson1(p);
}

void test02()
{
    Preson<string, int>p("Alice", 40);
    printPreson2(p);
}

int main()
{
    test01();

    test02();

    system("pause");

    return 0;
}
类模板与友元
#include <iostream>

#include<vector>

#include<algorithm>

using namespace std;

//C++面向对象和泛型编程,目的就是代码复用性的提升
//为建立数据结构和算法的一套标准,诞生了STL标准模板库
//Standard Template Library

//STL大体上分为,容器、算法、迭代器、仿函数、适配器、空间配置器
//STL广义上分为,容器、算法、迭代器,容器和算法通过迭代器进行无缝连接

//容器,置物之所也,13254
//序列式容器,强调值排序,序列式容器中的每个元素均有固定位置,13254
//关联式容器,二叉树结构,元素之间没有严格的物理上的顺序关系,12345

//算法,问题之解法也
//质变算法,指运算过程中会更改区间内的元素内容,如拷贝、替换、删除等
//非质变算法,指运算过程中不会更改区间内的元素内容,如查找、计数、遍历、寻极值等

//迭代器,容器和算法之间的粘合剂
//每个容器都有自己的专属迭代器,迭代器的使用非常类似于指针

//回调函数
void myPrint(int n)
{
    cout << n << endl;
}

void test01()
{
    //创建容器对象
    vector<int> v;

    //向容器中存放数据
    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);

    //迭代器指向容器中第一个元素
    vector<int>::iterator pBegin = v.begin();
    //迭代器指向容器中最后一个元素的下一位置
    vector<int>::iterator pEnd = v.end();

    //第一种遍历方式
    while (pBegin != pEnd)
    {
        cout << *pBegin << endl;
        pBegin++;
    }

    //第二种遍历方式
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        //*it = < >
        cout << *it << endl;
    }

    //第三种遍历方式
    for_each(v.begin(), v.end(), myPrint);
}

//vector<>存放自定义数据类型
class Preson
{
public:
    string m_name;
    int m_age;
public:
    Preson(string name, int age)
    {
        this->m_name = name;
        this->m_age = age;
    }
};

void test02()
{
    vector<Preson> v;

    Preson p1("aaa", 1);
    Preson p2("bbb", 2);
    Preson p3("ccc", 3);
    Preson p4("ddd", 4);
    Preson p5("eee", 5);

    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);

    for (vector<Preson>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << "姓名 = " << (*it).m_name << endl;
        cout << "年龄 = " << (*it).m_age << endl;
    }
}

void test03()
{
    vector<Preson*> v;

    Preson p1("aaa", 1);
    Preson p2("bbb", 2);
    Preson p3("ccc", 3);
    Preson p4("ddd", 4);
    Preson p5("eee", 5);

    v.push_back(&p1);
    v.push_back(&p2);
    v.push_back(&p3);
    v.push_back(&p4);
    v.push_back(&p5);

    for (vector<Preson*>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << "&姓名 = " << (*it)->m_name << endl;
        cout << "&年龄 = " << (*it)->m_age << endl;
    }
}

//容器嵌套容器
void test04()
{
    vector<vector<int>> v;

    vector<int> v1;
    vector<int> v2;
    vector<int> v3;
    vector<int> v4;

    for (int i = 0; i < 4; i++)
    {
        v1.push_back(i + 1);
        v2.push_back(i + 2);
        v3.push_back(i + 3);
        v4.push_back(i + 4);
    }

    v.push_back(v1);
    v.push_back(v2);
    v.push_back(v3);
    v.push_back(v4);

    for (vector<vector<int>>::iterator it = v.begin(); it != v.end(); it++)
    {
        for (vector<int>::iterator it1 = (*it).begin(); it1 != (*it).end(); it1++)
        {
            cout << *it1 << " ";
        }
        cout << endl;
    }
}

int main()
{
    test01();

    test02();

    test03();

    test04();

    system("pause");

    return 0;
}
STL初识
#include <iostream>

using namespace std;

//string构造函数
void test01()
{
    string s1;
    cout << "s1 = " << s1 << endl;

    const char* c = "hello world";
    string s2(c);
    cout << "s2 = " << s2 << endl;

    string s3(s2);
    cout << "s3 = " << s3 << endl;

    string s4(10, 'a');
    cout << "s4 = " << s4 << endl;
}

//string字符串赋值操作
void test02()
{
    string s1;
    s1 = "hello world";
    cout << "s1 = " << s1 << endl;

    string s2;
    s2 = s1;
    cout << "s2 = " << s2 << endl;

    string s3;
    s3 = 'a';
    cout << "s3 = " << s3 << endl;

    string s4;
    s4.assign("hello c++");
    cout << "s4 = " << s4 << endl;

    string s5;
    s5.assign("hello c++", 5);
    cout << "s5 = " << s5 << endl;

    string s6;
    s6.assign(s5);
    cout << "s6 = " << s6 << endl;

    string s7;
    s7.assign(7, 'x');
    cout << "s7 = " << s7 << endl;
}

//string字符串拼接操作
void test03()
{
    string s1 = "";

    s1 += "爱玩游戏";
    cout << "s1 = " << s1 << endl;

    s1 += ':';
    cout << "s1 = " << s1 << endl;

    string s2 = "lol dnf";
    s1 += s2;
    cout << "s1 = " << s1 << endl;

    string s3 = "i";
    s3.append("love");
    s3.append("game abc", 4);
    s3.append(s2, 4, 3);
    cout << "s3 = " << s3 << endl;
}

//string字符串查找和替换
void test04()
{
    string s1 = "abcdefgde";

    int n = s1.find("de");

    if (n == -1)
    {
        cout << "no find" << endl;
    }
    else
    {
        cout << "n = " << n << endl;
    }

    n = s1.rfind("de");
    cout << "n = " << n << endl;

    string s2 = "abcdefgde";
    s2.replace(1, 3, "12345");
    cout << "s2 = " << s2 << endl;
}

//string字符串比较大小
void test05()
{
    string s1 = "hello";
    string s2 = "aello";

    int n = s1.compare(s2);

    if (n == 0)
    {
        cout << "s1 = s2" << endl;
    }
    else if (n > 0)
    {
        cout << "s1 > s2" << endl;
    }
    else
    {
        cout << "s1 < s2" << endl;
    }
}

//string字符串存取
void test06()
{
    string s1 = "hello world";

    for (int i = 0; i < s1.size(); i++)
    {
        cout << s1[i] << " ";
    }
    cout << endl;

    for (int i = 0; i < s1.size(); i++)
    {
        cout << s1.at(i) << " ";
    }
    cout << endl;

    s1[0] = 'a';
    s1.at(1) = 'b';
    cout << "s1 = " << s1 << endl;
}

//string字符串插入和删除
void test07()
{
    string s1 = "hello";

    s1.insert(1, "123");
    cout << "s1 = " << s1 << endl;

    s1.erase(1, 3);
    cout << "s1 = " << s1 << endl;
}

//string字符串截取
void test08()
{
    string s1 = "abcdefg";
    string substr = s1.substr(1, 3);
    cout << "sunstr = " << substr << endl;

    string s2 = "alice@qq.com";
    int n = s2.find("@");
    string name = s2.substr(0, n);
    cout << "name = " << name << endl;
}

int main()
{
    test01();

    test02();

    test03();

    test04();

    test05();

    test06();

    test07();

    test08();

    system("pause");

    return 0;
}
string容器
#include <iostream>

#include<vector>

using namespace std;

//vector数据结构和数组非常相似,也称为单端数组
//不同之处在于数组是静态空间,而vector可以动态扩展
//所谓动态扩展,即找到更大的新的内存空间,然后将数据拷贝至新空间并释放原空间

void printVector(vector<int>& v)
{
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

//vector构造函数
void test01()
{
    vector<int> v1;
    for (int i = 0; i < 10; i++)
    {
        v1.push_back(i);
    }
    printVector(v1);

    vector<int> v2(v1.begin(), v1.end());
    printVector(v2);

    vector<int> v3(10, 100);
    printVector(v3);

    vector<int> v4(v3);
    printVector(v4);
}

//vector赋值操作
void test02()
{
    vector<int> v1;
    for (int i = 0; i < 10; i++)
    {
        v1.push_back(i);
    }
    printVector(v1);

    vector<int> v2;
    v2 = v1;
    printVector(v2);

    vector<int> v3;
    v3.assign(v1.begin(), v1.end());
    printVector(v3);

    vector<int> v4;
    v4.assign(10, 100);
    printVector(v4);
}

//vector容量和大小
void test03()
{
    vector<int> v1;
    for (int i = 0; i < 10; i++)
    {
        v1.push_back(i);
    }
    printVector(v1);

    if (v1.empty())
    {
        cout << "v1空" << endl;
    }
    else
    {
        cout << "v1不空" << endl;
        cout << "v1容量 = " << v1.capacity() << endl;
        cout << "v1大小 = " << v1.size() << endl;
    }

    v1.resize(15, 10);
    printVector(v1);

    v1.resize(5);
    printVector(v1);
}

//vector插入和删除
void test04()
{
    vector<int> v1;

    //尾插
    v1.push_back(10);
    v1.push_back(20);
    v1.push_back(30);
    v1.push_back(40);
    v1.push_back(50);
    printVector(v1);

    //尾删
    v1.pop_back();
    printVector(v1);

    //插入
    v1.insert(v1.begin(), 100);
    printVector(v1);
    v1.insert(v1.begin(), 2, 1000);
    printVector(v1);

    //删除
    v1.erase(v1.begin());
    printVector(v1);

    //清空
    v1.erase(v1.begin(), v1.end());
    v1.clear();
    printVector(v1);
}

//vector数据存取
void test05()
{
    vector<int> v1;
    for (int i = 0; i < 10; i++)
    {
        v1.push_back(i);
    }

    for (int i = 0; i < v1.size(); i++)
    {
        cout << v1[i] << " ";
    }
    cout << endl;

    for (int i = 0; i < v1.size(); i++)
    {
        cout << v1.at(i) << " ";
    }
    cout << endl;

    cout << "v1第一个元素 = " << v1.front() << endl;
    cout << "v1最后一个元素 = " << v1.back() << endl;
}

//vector互换容器
void test06()
{
    vector<int> v1;
    for (int i = 0; i < 10; i++)
    {
        v1.push_back(i);
    }
    printVector(v1);

    vector<int> v2;
    for (int i = 10; i > 0; i--)
    {
        v2.push_back(i);
    }
    printVector(v2);

    //互换容器
    cout << "互换后" << endl;
    v1.swap(v2);
    printVector(v1);
    printVector(v2);

    vector<int> v3;
    for (int i = 0; i < 10000; i++)
    {
        v3.push_back(i);
    }
    cout << "v3容量 = " << v3.capacity() << endl;
    cout << "v3大小 = " << v3.size() << endl;
    v3.resize(3);
    cout << "v3容量 = " << v3.capacity() << endl;
    cout << "v3大小 = " << v3.size() << endl;
    //收缩内存
    vector<int>(v3).swap(v3);
    cout << "v3容量 = " << v3.capacity() << endl;
    cout << "v3大小 = " << v3.size() << endl;
}

//vector预留空间
void test07()
{
    vector<int> v1;

    v1.reserve(100000);

    int num = 0;

    int* p = NULL;

    for (int i = 0; i < 1000000; i++)
    {
        v1.push_back(i);
        if (p != &v1[0])
        {
            p = &v1[0];
            num++;
        }
    }
    cout << "num = " << num << endl;
}

int main()
{
    test01();

    test02();

    test03();

    test04();

    test05();

    test06();

    test07();

    system("pause");

    return 0;
}
vetcor容器
#include <iostream>

#include<deque>

#include<algorithm>

using namespace std;

//双端数组,可以对头端进行插入删除操作

void printDeque(const deque<int>& d)
{
    for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

//deque构造函数
void test01()
{
    deque<int> d1;
    for (int i = 0; i < 10; i++)
    {
        d1.push_back(i);
    }
    printDeque(d1);

    deque<int> d2(d1.begin(), d1.end());
    printDeque(d2);

    deque<int> d3(10, 100);
    printDeque(d3);

    deque<int> d4(d3);
    printDeque(d4);
}

//deque赋值操作
void test02()
{
    deque<int> d1;
    for (int i = 0; i < 10; i++)
    {
        d1.push_back(i);
    }
    printDeque(d1);

    deque<int> d2;
    d2 = d1;
    printDeque(d2);

    deque<int> d3;
    d3.assign(d1.begin(), d1.end());
    printDeque(d3);

    deque<int> d4;
    d4.assign(10, 100);
    printDeque(d4);
}

//deque大小
void test03()
{
    deque<int> d1;
    for (int i = 0; i < 10; i++)
    {
        d1.push_back(i);
    }
    printDeque(d1);

    if (d1.empty())
    {
        cout << "d1空" << endl;
    }
    else
    {
        cout << "d1不空" << endl;
        cout << "d1大小 = " << d1.size() << endl;
    }

    d1.resize(15, 10);
    printDeque(d1);

    d1.resize(5);
    printDeque(d1);
}

//deque插入和删除
void test04()
{
    deque<int> d1;

    //尾插
    d1.push_back(10);
    d1.push_back(20);
    //头插
    d1.push_front(100);
    d1.push_front(200);
    printDeque(d1);

    //尾删
    d1.pop_back();
    //头删
    d1.pop_front();
    printDeque(d1);

    //插入
    d1.insert(d1.begin(), 100);
    printDeque(d1);

    d1.insert(d1.begin(), 2, 1000);
    printDeque(d1);

    deque<int> d2;
    d2.push_back(1);
    d2.push_back(2);
    d2.push_back(3);
    d1.insert(d1.begin(), d2.begin(), d2.end());
    printDeque(d1);

    //删除
    d1.erase(d1.begin());
    printDeque(d1);

    //清空
    d1.erase(d1.begin(), d1.end());
    d1.clear();
    printDeque(d1);
}

//deque数据存取
void test05()
{
    deque<int> d1;

    d1.push_back(10);
    d1.push_back(20);
    d1.push_front(100);
    d1.push_front(200);

    for (int i = 0; i < d1.size(); i++)
    {
        cout << d1[i] << " ";
    }
    cout << endl;

    for (int i = 0; i < d1.size(); i++)
    {
        cout << d1.at(i) << " ";
    }
    cout << endl;

    cout << "d1第一个元素 = " << d1.front() << endl;
    cout << "d1最后一个元素 = " << d1.back() << endl;
}

//deque排序
void test06()
{
    deque<int> d1;

    d1.push_back(10);
    d1.push_back(20);
    d1.push_front(100);
    d1.push_front(200);

    printDeque(d1);

    sort(d1.begin(), d1.end());
    printDeque(d1);
}

int main()
{
    test01();

    test02();

    test03();

    test04();

    test05();

    test06();

    system("pause");

    return 0;
}
deque容器
#include <iostream>

#include<vector>

#include<deque>

#include<algorithm>

#include<time.h>

using namespace std;

//有五名选手ABCDE,十个评委分别对其打分
//去除最高分和最低分,求平均分

class Preson
{
public:
    string m_name;
    int m_score;
public:
    Preson(string name, int score)
    {
        this->m_name = name;
        this->m_score = score;
    }
};

void createPreson(vector<Preson>& v)
{
    string nameAll = "ABCDE";
    for (int i = 0; i < 5; i++)
    {
        string name = "选手";
        name += nameAll[i];
        int score = 0;
        Preson p(name, score);
        v.push_back(p);
    }
}

void setScore(vector<Preson>& v)
{
    for (vector<Preson>::iterator it = v.begin(); it != v.end(); it++)
    {
        deque<int> d;
        for (int i = 0; i < 10; i++)
        {
            int score = rand() % 41 + 60;
            d.push_back(score);
        }

        cout << "选手:" << it->m_name << " 打分:" << it->m_score;
        for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)
        {
            cout << *dit << " ";
        }
        cout << endl;

        sort(d.begin(), d.end());
        d.pop_back();
        d.pop_front();
        int sum = 0;
        for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)
        {
            sum += *dit;
        }
        int avg = sum / d.size();
        it->m_score = avg;
    }
}

void showScore(vector<Preson>& v)
{
    for (vector<Preson>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << "姓名 = " << it->m_name << "分数 = " << it->m_score << endl;
    }
}

int main()
{
    srand((unsigned int)time(NULL));

    vector<Preson> v;

    createPreson(v);

    setScore(v);

    showScore(v);

    system("pause");

    return 0;
}
案例(评委打分)
#include <iostream>

#include<stack>

using namespace std;

//stack是一种先进后出的数据结构,它只有一个出口
//栈中只有顶端的元素才可以被外界使用,因此栈不允许有遍历行为
//栈中进入数据称为入栈(push)
//栈中弹出数据称为出栈(pop)
//生活中的栈:弹夹里面的子弹,地铁中的乘客

void test01()
{
    stack<int> s;

    s.push(10);
    s.push(20);
    s.push(30);

    while (!s.empty())
    {
        cout << "栈顶元素为 = " << s.top() << endl;
        s.pop();
    }

    cout << "栈的大小 = " << s.size() << endl;
}

int main()
{
    test01();

    system("pause");

    return 0;
}
stack容器
#include <iostream>

#include<queue>

using namespace std;

//queue是一种先进先出的数据结构,它有两个出口
//队列容器允许从一端新增元素,从另一端移除元素
//队列中只有队头和队尾才可以被外界使用,因此队列不允许有遍历行为
//队列中入数据称为入队(push)
//队列中出数据称为出队(pop)
//生活中的队列:如排队打饭

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

void test01()
{
    queue<Preson> q;

    Preson p1("唐僧", 30);
    Preson p2("孙悟空", 1000);
    Preson p3("猪八戒", 900);
    Preson p4("沙师弟", 800);

    q.push(p1);
    q.push(p2);
    q.push(p3);
    q.push(p4);

    //队列不提供迭代器,更不支持随机访问
    while (!q.empty())
    {
        cout << "队头元素(姓名) = " << q.front().m_name << " 队头元素(年龄) = " << q.front().m_age << endl;

        cout << "队尾元素(姓名) = " << q.back().m_name << " 队尾元素(年龄) = " << q.back().m_age << endl;

        cout << endl;

        q.pop();
    }

    cout << "队列大小 = " << q.size() << endl;
}

int main()
{
    test01();

    system("pause");

    return 0;
}
queue容器
#include <iostream>

#include<list>

using namespace std;

//将数据进行链式存储
//链表是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接实现的
//链表的组成:链表由一系列节点组成
//STL中的链表是一个双向循环链表
//由于链表的存储方式并不是连续的内存空间,因此链表(list)中的迭代器只支持前移和后移,属于双向迭代器
//list优点:
//采用动态存储分配,不会造成内存浪费和溢出
//链表执行插入和删除操作十分方便,修改指针即可,不需要移动大量元素
//list缺点:
//链表灵活,但空间(指针域)和时间(遍历)额外耗费巨大
//list插入操作和删除操作都不会造成原有list迭代器的失效,这在vector是不成立的

void printList(const list<int>& l)
{
    for (list<int>::const_iterator it = l.begin(); it != l.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

//list构造函数
void test01()
{
    list<int> l1;

    l1.push_back(10);
    l1.push_back(20);
    l1.push_back(30);
    l1.push_back(40);
    printList(l1);

    list<int> l2(l1.begin(), l1.end());
    printList(l2);

    list<int> l3(l2);
    printList(l3);

    list<int> l4(10, 1000);
    printList(l4);
}

//list赋值和交换
void test02()
{
    list<int> l1;

    l1.push_back(100);
    l1.push_back(200);
    l1.push_back(300);
    l1.push_back(400);
    printList(l1);

    //赋值
    list<int> l2;
    l2 = l1;
    printList(l2);

    list<int> l3;
    l3.assign(l2.begin(), l2.end());
    printList(l3);

    list<int> l4;
    l4.assign(10, 10000);
    printList(l4);

    //交换
    list<int> l5;

    l5.push_back(500);
    l5.push_back(600);
    l5.push_back(700);
    l5.push_back(800);

    cout << "交换前:" << endl;
    printList(l1);
    printList(l5);
    cout << "----------" << endl;
    l1.swap(l5);
    cout << "交换后:" << endl;
    printList(l1);
    printList(l5);
}

//list大小
void test03()
{
    list<int> l1;

    l1.push_back(100);
    l1.push_back(200);
    l1.push_back(300);
    l1.push_back(400);

    if (l1.empty())
    {
        cout << "l1为空" << endl;
    }
    else
    {
        cout << "l1不为空" << endl;
        cout << "l1大小 = " << l1.size() << endl;
    }

    l1.resize(10);
    printList(l1);

    l1.resize(2);
    printList(l1);
}

//list插入和删除
void test04()
{
    list<int> l1;

    //尾插
    l1.push_back(100);
    l1.push_back(200);
    l1.push_back(300);

    //头插
    l1.push_front(400);
    l1.push_front(500);
    l1.push_front(600);

    printList(l1);

    //插入
    list<int>::iterator it = l1.begin();
    l1.insert(++it, 111);
    printList(l1);

    //移除
    it = l1.begin();
    l1.erase(++it);
    printList(l1);

    //移除
    l1.push_back(10000);
    l1.push_back(10000);
    l1.push_back(10000);
    l1.push_back(10000);
    printList(l1);
    l1.remove(10000);
    printList(l1);

    //清空
    l1.clear();
    printList(l1);
}

//list数据存取
void test05()
{
    list<int> l1;

    l1.push_back(10);
    l1.push_back(20);
    l1.push_back(30);
    l1.push_back(40);

    cout << "第一个元素 = " << l1.front() << endl;
    cout << "最后一个元素 = " << l1.back() << endl;

    //list容器中不支持通过[]和at方式访问数据
    //list容器的迭代器是双向迭代器,不支持随机访问
    //list<int>::iterator it = l1.begin();
    //it = it + 1;
}

bool myCompare(int n1, int n2)
{
    return n1 > n2;
}

//list反转和排序
void test06()
{
    list<int> l1;

    l1.push_back(90);
    l1.push_back(30);
    l1.push_back(20);
    l1.push_back(70);
    printList(l1);

    //反转
    l1.reverse();
    printList(l1);

    //排序
    //默认排序
    l1.sort();
    printList(l1);
    //自定义排序
    l1.sort(myCompare);
    printList(l1);
}

class Preson
{
public:
    string m_name;
    int m_age;
    int m_height;
public:
    Preson(string name, int age, int height)
    {
        this->m_name = name;
        this->m_age = age;
        this->m_height = height;
    }
};

bool comparePreson(Preson& p1, Preson& p2)
{
    if (p1.m_age == p2.m_age)
    {
        return p1.m_height > p2.m_height;
    }
    else
    {
        return p1.m_age < p2.m_age;
    }
}

void test07()
{
    //将Preson自定义数据类型进行排序
    //Preson属性中有姓名、年龄、身高
    //按照年龄升序,年龄相同时身高降序排列

    list<Preson> l1;

    Preson p1("刘备", 35, 175);
    Preson p2("曹操", 45, 180);
    Preson p3("孙权", 40, 170);
    Preson p4("赵云", 25, 190);
    Preson p5("关羽", 35, 160);
    Preson p6("张飞", 35, 200);

    l1.push_back(p1);
    l1.push_back(p2);
    l1.push_back(p3);
    l1.push_back(p4);
    l1.push_back(p5);
    l1.push_back(p6);

    for (list<Preson>::iterator it = l1.begin(); it != l1.end(); it++)
    {
        cout << "姓名 = " << it->m_name << " 年龄 = " << it->m_age << " 身高 = " << it->m_height << endl;
    }

    l1.sort(comparePreson);
    cout << "排序后" << endl;

    for (list<Preson>::iterator it = l1.begin(); it != l1.end(); it++)
    {
        cout << "姓名 = " << it->m_name << " 年龄 = " << it->m_age << " 身高 = " << it->m_height << endl;
    }
}

int main()
{
    test01();

    test02();

    test03();

    test04();

    test05();

    test06();

    test07();

    system("pause");

    return 0;
}
list容器
#include <iostream>

#include<set>


using namespace std;

//所有元素都会在插入时被自动排序
//set/multiset属于关联式容器,底层结构是用二叉树实现
//set/multiset区别:set容器中不允许有重复的元素,multiset容器中允许有重复的元素

void printSet(set<int>& s)
{
    for (set<int>::iterator it = s.begin(); it != s.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

//set构造和赋值
void test01()
{
    set<int> s1;

    s1.insert(10);
    s1.insert(30);
    s1.insert(20);
    s1.insert(40);
    printSet(s1);

    //拷贝构造
    set<int> s2(s1);
    printSet(s2);

    //赋值
    set<int> s3;
    s3 = s2;
    printSet(s3);
}

//set大小和交换
void test02()
{
    set<int> s1;
    s1.insert(10);
    s1.insert(30);
    s1.insert(20);
    s1.insert(40);

    //大小
    if (s1.empty())
    {
        cout << "s1为空" << endl;
    }
    else
    {
        cout << "s1不为空" << endl;
        cout << "s1大小 = " << s1.size() << endl;
    }

    //交换
    set<int> s2;
    s2.insert(100);
    s2.insert(300);
    s2.insert(200);
    s2.insert(400);

    cout << "交换前" << endl;
    printSet(s1);
    printSet(s2);
    s1.swap(s2);
    cout << "交换后" << endl;
    printSet(s1);
    printSet(s2);
}

//set插入和删除
void test03()
{
    set<int> s1;

    //插入
    s1.insert(10);
    s1.insert(30);
    s1.insert(20);
    s1.insert(40);
    printSet(s1);

    //删除
    s1.erase(s1.begin());
    printSet(s1);
    s1.erase(30);
    printSet(s1);

    //清空
    s1.erase(s1.begin(), s1.end());
    //s1.clear();
    printSet(s1);
}

//set查找和统计
void test04()
{
    set<int> s1;
    s1.insert(10);
    s1.insert(30);
    s1.insert(20);
    s1.insert(40);

    //查找
    set<int>::iterator pos = s1.find(50);
    if (pos != s1.end())
    {
        cout << "找到元素 = " << *pos << endl;
    }
    else
    {
        cout << "未找到元素" << endl;
    }

    //统计
    int num = s1.count(30);
    cout << "num = " << num << endl;
}

//set/multiset区别
//set不可以插入重复数据
//set插入数据的同时会返回插入结果,表示插入是否成功
void test05()
{
    set<int> s1;
    pair<set<int>::iterator, bool> ret = s1.insert(10);
    if (ret.second)
    {
        cout << "第一次插入成功" << endl;
    }
    else
    {
        cout << "第一次插入失败" << endl;
    }
    ret = s1.insert(10);
    if (ret.second)
    {
        cout << "第二次插入成功" << endl;
    }
    else
    {
        cout << "第二次插入失败" << endl;
    }

    multiset<int> ms1;
    ms1.insert(10);
    ms1.insert(10);
    for (multiset<int>::iterator it = ms1.begin(); it != ms1.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

//pair对组创建
//成对出现的数据,利用对组可以返回两个数据
void test06()
{
    pair<string, int> p1(string("tom"), 20);
    cout << "姓名 = " << p1.first << " 年龄 = " << p1.second << endl;

    pair<string, int> p2 = make_pair("alice", 21);
    cout << "姓名 = " << p2.first << " 年龄 = " << p2.second << endl;
}

class myCompare6
{
public:
    bool operator()(int n1, int n2) const
    {
        return n1 > n2;
    }
};

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

class comparePreson
{
public:
    bool operator()(const Preson& p1, const Preson& p2) const
    {
        return p1.m_age > p2.m_age;
    }
};

//set容器排序
//默认排序规则为从小到大
//利用仿函数可以改变排序规则
void test07()
{
    set<int> s1;
    s1.insert(10);
    s1.insert(40);
    s1.insert(20);
    s1.insert(30);
    s1.insert(50);
    //默认排序规则从小到大
    for (set<int>::iterator it = s1.begin(); it != s1.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;

    set<int, myCompare6> s2;
    s2.insert(1);
    s2.insert(4);
    s2.insert(2);
    s2.insert(3);
    s2.insert(5);
    //改变排序规则
    for (set<int, myCompare6>::iterator it = s2.begin(); it != s2.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;

    set<Preson, comparePreson> s3;
    Preson p1("刘备", 50);
    Preson p2("关羽", 35);
    Preson p3("张飞", 70);
    Preson p4("马超", 20);
    s3.insert(p1);
    s3.insert(p2);
    s3.insert(p3);
    s3.insert(p4);
    //自定义数据类型改变排序规则
    for (set<Preson, comparePreson>::iterator it = s3.begin(); it != s3.end(); it++)
    {
        cout << "姓名 = " << it->m_name << " 年龄 = " << it->m_age << endl;
    }
}

int main()
{
    test01();

    test02();

    test03();

    test04();

    test05();

    test06();

    test07();

    system("pause");

    return 0;
}
set容器
#include <iostream>

#include<map>

using namespace std;

//map中所有元素都是pair
//pair中第一个元素为key(键值),起到索引作用
//pair中第二个元素为value(实值)
//所有元素都会根据元素的键值自动排序
//map/multimap属于关联式容器,底层结构是用二叉树实现
//优点:可以根据key值快速找到value值
//map/multimap区别:
//map容器中不允许有重复的key值元素
//multimap容器中允许有重复的key值元素

void printMap(map<int, int>& m)
{
    for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
    {
        cout << "key = " << it->first << " value = " << it->second << endl;
    }
    cout << endl;
}

//map构造和赋值
void test01()
{
    //默认构造
    map<int, int> m1;
    m1.insert(pair<int, int>(1, 10));
    m1.insert(pair<int, int>(2, 20));
    m1.insert(pair<int, int>(3, 30));
    printMap(m1);

    //拷贝构造
    map<int, int> m2(m1);
    printMap(m2);

    //赋值
    map<int, int> m3;
    m3 = m2;
    printMap(m3);
}

//map大小和交换
void test02()
{
    map<int, int> m1;

    m1.insert(pair<int, int>(1, 10));
    m1.insert(pair<int, int>(2, 20));
    m1.insert(pair<int, int>(3, 30));

    //大小
    if (m1.empty())
    {
        cout << "m1为空" << endl;
    }
    else
    {
        cout << "m1不为空" << endl;
        cout << "m1大小 = " << m1.size() << endl;
    }

    //交换
    map<int, int> m2;
    m2.insert(pair<int, int>(4, 40));
    m2.insert(pair<int, int>(5, 50));
    m2.insert(pair<int, int>(6, 60));
    cout << "交换前:" << endl;
    printMap(m1);
    printMap(m2);
    cout << "交换后:" << endl;
    m1.swap(m2);
    printMap(m1);
    printMap(m2);
}

//map插入和删除
void test03()
{
    map<int, int> m1;

    //插入
    m1.insert(pair<int, int>(1, 10));
    m1.insert(make_pair(2, 20));
    m1.insert(map<int, int>::value_type(3, 30));
    m1[4] = 40;
    printMap(m1);

    //删除
    m1.erase(m1.begin());
    printMap(m1);
    m1.erase(3);
    printMap(m1);

    //清空
    m1.erase(m1.begin(), m1.end());
    //m1.clear();
    printMap(m1);
}

//map查找和统计
void test04()
{
    map<int, int> m1;

    m1.insert(pair<int, int>(1, 10));
    m1.insert(pair<int, int>(2, 20));
    m1.insert(pair<int, int>(3, 30));

    //查找
    map<int, int>::iterator pos = m1.find(3);
    if (pos != m1.end())
    {
        cout << "找到元素 key = " << (*pos).first << " value = " << (*pos).second << endl;
    }
    else
    {
        cout << "未找到元素" << endl;
    }

    //统计
    int num = m1.count(3);
    cout << "num = " << num << endl;
}

class myCompare
{
public:
    bool operator()(int n1, int n2) const
    {
        return n1 > n2;
    }
};

//map排序
void test05()
{
    map<int, int, myCompare> m1;

    m1.insert(make_pair(1, 10));
    m1.insert(make_pair(2, 20));
    m1.insert(make_pair(3, 30));
    m1.insert(make_pair(4, 40));
    m1.insert(make_pair(5, 50));

    for (map<int, int, myCompare>::iterator it = m1.begin(); it != m1.end(); it++)
    {
        cout << "key = " << it->first << " value = " << it->second << endl;
    }
}

int main()
{
    test01();

    test02();

    test03();

    test04();

    test05();

    system("pause");

    return 0;
}
map容器
#include <iostream>

using namespace std;

//函数对象概念:
//重载函数调用操作符的类,其对象常称为函数对象
//函数对象使用重载的()时,行为类似函数调用,也称仿函数
//函数对象(仿函数)是一个类,不是一个函数

class myAdd
{
public:
    int operator()(int n1, int n2)
    {
        return n1 + n2;
    }
};

//函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
void test01()
{
    myAdd ma;
    cout << ma(1, 10) << endl;
}

class myPrint
{
public:
    int count;
public:
    myPrint()
    {
        count = 0;
    }
    void operator()(string s)
    {
        cout << s << endl;
        count++;
    }
};

//函数对象超出普通函数的概念,函数对象可以有自己的状态
void test02()
{
    myPrint my;
    my("hello world");
    my("hello world");
    my("hello world");
    my("hello world");
    my("hello world");
    cout << "hello world调用次数 = " << my.count << endl;
}

void doPrint(myPrint& mp, string s)
{
    mp(s);
}

//函数对象可以作为参数传递
void test03()
{
    myPrint mp;
    doPrint(mp, "hello c++");
}

int main()
{
    test01();

    test02();

    test03();

    system("pause");

    return 0;
}
函数对象
#include <iostream>

#include<vector>

#include<algorithm>

using namespace std;

//返回bool类型的仿函数称为谓词
//如果operator()接受一个参数,那么叫做一元谓词
//如果operator()接受两个参数,那么叫做二元谓词

//一元谓词
class greateFive
{
public:
    bool operator()(int n)
    {
        return n > 5;
    }
};

void test01()
{
    vector<int> v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }
    vector<int>::iterator it = find_if(v.begin(), v.end(), greateFive());
    if (it == v.end())
    {
        cout << "未找到" << endl;
    }
    else
    {
        cout << "找到 = " << *it << endl;
    }
}

//二元谓词
class myCompare
{
public:
    int operator()(int n1, int n2)
    {
        return n1 > n2;
    }
};

void test02()
{
    vector<int> v;
    v.push_back(10);
    v.push_back(40);
    v.push_back(20);
    v.push_back(30);
    v.push_back(50);
    //默认排序
    sort(v.begin(), v.begin());
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << " ";
        cout << endl;
    }
    cout << "----------" << endl;
    //自定义排序
    sort(v.begin(), v.end(), myCompare());
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << " ";
        cout << endl;
    }
}

int main()
{
    test01();

    test02();

    system("pause");

    return 0;
}
谓词
#include <iostream>

#include<vector>

#include<functional>

#include<algorithm>

using namespace std;

//算数仿函数
//实现四则运算
//negate是一元运算,其余为二元运算
//加法仿函数(plus)
//减法仿函数(minus)
//乘法仿函数(multiplies)
//除法仿函数(divides)
//取模反函数(modulus)
//取反仿函数(negate)
void test01()
{
    negate<int> n;
    cout << n(50) << endl;

    plus<int> p;
    cout << p(10, 20) << endl;
}

class myCompare
{
public:
    bool operator()(int n1, int n2)
    {
        return n1 > n2;
    }
};

//关系仿函数
//实现关系对比
//等于(equal_to)
//不等于(not_equal_to)
//大于(greater)
//大于等于(greater_equal)
//小于(less)
//小于等于(less_equal)
void test02()
{
    vector<int> v;
    v.push_back(10);
    v.push_back(30);
    v.push_back(50);
    v.push_back(40);
    v.push_back(20);
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;

    //自己实现仿函数
    //sort(v.begin(), v.end(), myCompare());

    //STL内建仿函数
    sort(v.begin(), v.end(), greater<int>());
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

//逻辑仿函数
//实现逻辑运算
//逻辑与(logical_and)
//逻辑或(logical_or)
//逻辑非(logical_not)
void test03()
{
    vector<bool> v1;
    v1.push_back(true);
    v1.push_back(false);
    v1.push_back(true);
    v1.push_back(false);
    for (vector<bool>::iterator it = v1.begin(); it != v1.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;

    vector<bool> v2;
    v2.resize(v1.size());
    transform(v1.begin(), v1.end(), v2.begin(), logical_not<bool>());
    for (vector<bool>::iterator it = v2.begin(); it != v2.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

int main()
{
    test01();

    test02();

    test03();

    system("pause");

    return 0;
}
内建函数对象
#include <iostream>

#include<vector>

#include<algorithm>

using namespace std;

//普通函数
void print01(int n)
{
    cout << n << " ";
}

//函数对象
class print02
{
public:
    void operator()(int n)
    {
        cout << n << " ";
    }
};

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;
}

class transForm
{
public:
    int operator()(int n)
    {
        return n;
    }
};

class myPrint
{
public:
    void operator()(int n)
    {
        cout << n << " ";
    }
};

void test02()
{
    vector<int> v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }

    vector<int> vTarget;
    vTarget.resize(v.size());
    //搬运算法
    transform(v.begin(), v.end(), vTarget.begin(), transForm());
    for_each(vTarget.begin(), vTarget.end(), myPrint());
    cout << endl;
}

int main()
{
    test01();

    test02();

    system("pause");

    return 0;
}
常用遍历算法
#include <iostream>

#include<vector>

#include<algorithm>

using namespace std;

//查找指定元素(find)
//按条件查找指定元素(find_if)
//查找相邻重复元素(adjacent_find)
//二分查找法(binary_search)
//统计元素个数(count)
//按条件统计元素个数(count_if)

//find
void test01()
{
    vector<int> v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i + 1);
    }
    vector<int>::iterator it = find(v.begin(), v.end(), 5);
    if (it == v.end())
    {
        cout << "未找到" << endl;
    }
    else
    {
        cout << "找到 = " << *it << endl;
    }
}
class Preson
{
public:
    string m_name;
    int m_age;
public:
    Preson(string name, int age)
    {
        this->m_name = name;
        this->m_age = age;
    }
    bool operator==(const Preson& p)
    {
        if (this->m_name == p.m_name && this->m_age == p.m_age)
        {
            return true;
        }
        return false;
    }
};
void test02()
{
    vector<Preson> v;
    Preson p1("aaa", 10);
    Preson p2("bbb", 20);
    Preson p3("ccc", 30);
    Preson p4("ddd", 40);
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    vector<Preson>::iterator it = find(v.begin(), v.end(), p2);
    if (it == v.end())
    {
        cout << "未找到" << endl;
    }
    else
    {
        cout << "姓名 = " << it->m_name << " 年龄 = " << it->m_age << endl;
    }
}



//find_if
//内置数据类型
class greateFive
{
public:
    bool operator()(int n)
    {
        return n > 5;
    }
};
void test03()
{
    vector<int> v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i + 1);
    }
    vector<int>::iterator it = find_if(v.begin(), v.end(), greateFive());
    if (it == v.end())
    {
        cout << "未找到" << endl;
    }
    else
    {
        cout << "找到 = " << *it << endl;
    }
}
//自定义数据类型
class comparePreson
{
public:
    bool operator()(Preson& p)
    {
        return p.m_age > 20;
    }
};
void test04()
{
    vector<Preson> v;
    Preson p1("aaa", 10);
    Preson p2("bbb", 20);
    Preson p3("ccc", 30);
    Preson p4("ddd", 40);
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    vector<Preson>::iterator it = find_if(v.begin(), v.end(), comparePreson());
    if (it == v.end())
    {
        cout << "未找到" << endl;
    }
    else
    {
        cout << "姓名 = " << it->m_name << " 年龄 = " << it->m_age << endl;
    }
}



//adjacent_find
void test05()
{
    vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(5);
    v.push_back(2);
    v.push_back(4);
    v.push_back(4);
    v.push_back(3);
    vector<int>::iterator it = adjacent_find(v.begin(), v.end());
    if (it == v.end())
    {
        cout << "未找到" << endl;
    }
    else
    {
        cout << "找到相邻重复元素 = " << *it << endl;
    }
}



//binary_search
//无序序列中不可使用
void test06()
{
    vector<int> v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }
    bool b = binary_search(v.begin(), v.end(), 2);
    if (b)
    {
        cout << "找到" << endl;
    }
    else
    {
        cout << "未找到" << endl;
    }
}



//count
void test07()
{
    vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(4);
    v.push_back(5);
    v.push_back(3);
    v.push_back(4);
    v.push_back(4);
    int num = count(v.begin(), v.end(), 4);
    cout << "4的个数为:" << num << endl;
}



//count_if
//内置数据类型
class greateFour
{
public:
    bool operator()(int n)
    {
        return n >= 4;
    }
};
void test08()
{
    vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(4);
    v.push_back(5);
    v.push_back(3);
    v.push_back(4);
    v.push_back(4);
    int num = count_if(v.begin(), v.end(), greateFour());
    cout << "大于等于4的个数为:" << num << endl;
}

int main()
{
    test01();

    test02();

    test03();

    test04();

    test05();

    test06();

    test07();

    test08();

    system("pause");

    return 0;
}
常用查找算法
#include <iostream>

#include<vector>

#include<algorithm>

#include<ctime>

using namespace std;

//sort(排序)
void myPrint01(int n)
{
    cout << n << " ";
}
void test01()
{
    vector<int> v;
    v.push_back(10);
    v.push_back(30);
    v.push_back(50);
    v.push_back(20);
    v.push_back(40);
    //默认从小到大
    sort(v.begin(), v.end());
    for_each(v.begin(), v.end(), myPrint01);
    cout << endl;
    //从大到小
    sort(v.begin(), v.end(), greater<int>());
    for_each(v.begin(), v.end(), myPrint01);
    cout << endl;
}



//random_shuffle(洗牌)
class myPrint02
{
public:
    void operator()(int n)
    {
        cout << n << " ";
    }
};
void test02()
{
    srand((unsigned int)time(NULL));
    vector<int> v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }
    for_each(v.begin(), v.end(), myPrint02());
    cout << endl;
    //洗牌
    random_shuffle(v.begin(), v.end());
    for_each(v.begin(), v.end(), myPrint02());
    cout << endl;
}



//merge(合并)
void test03()
{
    vector<int> v1;
    vector<int> v2;
    for (int i = 0; i < 10; i++)
    {
        v1.push_back(i);
        v2.push_back(i + 1);
    }
    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(), myPrint02());
    cout << endl;
}



//reverse(反转)
void test04()
{
    vector<int> v;
    v.push_back(10);
    v.push_back(30);
    v.push_back(50);
    v.push_back(20);
    v.push_back(40);
    cout << "反转前" << endl;
    for_each(v.begin(), v.end(), myPrint02());
    cout << endl;
    cout << "反转后" << endl;
    reverse(v.begin(), v.end());
    for_each(v.begin(), v.end(), myPrint02());
    cout << endl;
}

int main()
{
    test01();

    test02();

    test03();

    test04();

    system("pause");

    return 0;
}
常用排序算法
#include <iostream>

#include<vector>

#include<algorithm>

#include<ctime>

using namespace std;

class myPrint
{
public:
    void operator()(int n)
    {
        cout << n << " ";
    }
};

//copy
void test01()
{
    vector<int> v1;
    for (int i = 0; i < 10; i++)
    {
        v1.push_back(i + 1);
    }
    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 test02()
{
    vector<int> v;
    v.push_back(10);
    v.push_back(30);
    v.push_back(50);
    v.push_back(20);
    v.push_back(40);
    cout << "替换前" << endl;
    for_each(v.begin(), v.end(), myPrint());
    cout << endl;
    cout << "替换后" << endl;
    //替换
    replace(v.begin(), v.end(), 20, 2000);
    for_each(v.begin(), v.end(), myPrint());
    cout << endl;
}



//replace_if
class replaceGreate30
{
public:
    bool operator()(int n)
    {
        return n >= 30;
    }
};
void test03()
{
    vector<int> v;
    v.push_back(20);
    v.push_back(30);
    v.push_back(20);
    v.push_back(40);
    v.push_back(50);
    v.push_back(10);
    v.push_back(20);
    cout << "替换前" << endl;
    for_each(v.begin(), v.end(), myPrint());
    cout << endl;
    cout << "替换后" << endl;
    //按条件替换
    replace_if(v.begin(), v.end(), replaceGreate30(), 3000);
    for_each(v.begin(), v.end(), myPrint());
    cout << endl;
}



//swap
void test04()
{
    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;
    cout << "交换后" << endl;
    //交换
    swap(v1, v2);
    for_each(v1.begin(), v1.end(), myPrint());
    cout << endl;
    for_each(v2.begin(), v2.end(), myPrint());
    cout << endl;
}
int main()
{
    test01();

    test02();

    test03();

    test04();

    system("pause");

    return 0;
}
常用拷贝替换算法
#include <iostream>

#include<vector>

#include<algorithm>

#include<numeric>

using namespace std;

class myPrint
{
public:
    void operator()(int n)
    {
        cout << n << " ";
    }
};



//accumulate
void test01()
{
    vector<int> v;
    for (int i = 0; i <= 100; i++)
    {
        v.push_back(i);
    }
    //求和
    int total = accumulate(v.begin(), v.end(), 0);
    cout << "total = " << total << endl;
}



//fill
void test02()
{
    vector<int> v;
    v.resize(10);
    //填充
    fill(v.begin(), v.end(), 100);
    for_each(v.begin(), v.end(), myPrint());
    cout << endl;
}

int main()
{
    test01();

    test02();

    system("pause");

    return 0;
}
常用算数生成算法
#include <iostream>

#include<vector>

#include<algorithm>

using namespace std;

//必须为有序序列集合
//交集(set_intersection)
//并集(set_union)
//差集(set_difference)

class myPrint
{
public:
    void operator()(int n)
    {
        cout << n << " ";
    }
};

//set_intersection
void test01()
{
    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 it = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
    for_each(vTarget.begin(), it, myPrint());
    cout << endl;
}



//set_union
void test02()
{
    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(v1.size() + v2.size());
    //并集
    vector<int>::iterator it = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
    for_each(vTarget.begin(), it, myPrint());
    cout << endl;
}



//set_difference
void test03()
{
    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(max(v1.size(), v2.size()));
    //差集
    cout << "v1与v2的差集为:" << endl;
    vector<int>::iterator it1 = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
    for_each(vTarget.begin(), it1, myPrint());
    cout << endl;
    cout << "v2与v1的差集为:" << endl;
    vector<int>::iterator it2 = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), vTarget.begin());
    for_each(vTarget.begin(), it2, myPrint());
    cout << endl;
}

int main()
{
    test01();

    test02();

    test03();

    system("pause");

    return 0;
}
常用集合算法 

 

end

posted @ 2020-02-28 17:58  _Huang95  阅读(260)  评论(0编辑  收藏  举报