C++提高编程(三)

本阶段主要针对C++泛型编程和STL技术做详细讲解,探讨C++更深层的使用

1.模板

模板就是建立通用的摸具,大大提高复用性

1.1函数模板

作用:建立一个通用函数,其函数返回值类型和形参类型可以不具体制定,用一个虚拟的类型来代表。

语法:

template<typename T>
函数声明或定义
​
template -- 声明创建模板
typename -- 表面其后面的符号是一种数据类型,可以用class代替
T -- 通用的数据类型,名称可以替换,通常为大写字母
#include<iostream>
using namespace std;
​
void swapInt(int& a, int& b) {
    int temp = a;
    a = b;
    b = temp;
}
​
void swapDouble(double& c, double& d) {
    double temp = c;
    c = d;
    d = temp;
}
​
template<typename T>
void mySwap(T& a, T& b) {
    T temp = a;
    a = b;
    b = temp;
}
​
void test01() {
​
    int a = 10, b = 12;
    //swapInt(a, b);
    //两种方式使用函数模板
    //1.自动类型推导
    mySwap(a, b);
    cout << a << " " << b << endl;
    //2.显示指定类型
    mySwap<int>(a, b);
    cout << a << " " << b << endl;
}
​
void test02() {
​
    double c = 10.0, d = 12.2;
    swapDouble(c, d);
    cout << c << " " << d << endl;
}
int main() {
​
    test01();
    test02();
​
    system("pause");
    return 0;
}
#include<iostream>
using namespace std;
​
//实现:通用对数组进行排序的函数
//规则:从大到小
//算法:选择
//测试:char 数组 、int 数组
template<typename T>
void mySwap(T&a,T&b){
    T temp = a;
    a = b;
    b = temp;
}
​
template<typename T>
void mySort(T arr[], int len) {
    for (int i = 0; i < len; i++) {
        int max = i;
        for (int j = i + 1; j < len; j++) {
            if (arr[max] < arr[j]) {
                max = j;
            }
        }
        if (max != i) {
            mySwap(arr[max], arr[i]);
        }
    }
}
​
template<typename T>
void printArray(T arr[],int len) {
    for (int i = 0; i < len; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
}
​
void test01() {
​
    char charArr[] = "badcfe";
    int num = sizeof(charArr) / sizeof(char);
    mySort(charArr, num);
    printArray(charArr, num);
​
}
​
void test02() {
​
    int intArr[] = {1,23,4,6,8,2,0};
    int num = sizeof(intArr) / sizeof(int);
    mySort(intArr, num);
    printArray(intArr, num);
​
}
​
int main() {
​
    test01();
    test02();
​
    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 = 12;
    char c1 = 'a';
    char c2 = 'b';
    myPrint(a, b);
    myPrint(a, b, c);
    myPrint(c1, c2);
​
}
​
​
int main() {
​
    test01();
​
    system("pause");
    return 0;
}
​
结果:
普通函数调用
调用重载的模板
调用的模板

模板的局限性:

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

template<class T>
void f(T a, T b){
    a=b;
}

在上述代码中提供的赋值操作,如果传入的a和b是一个数组,就无法实现了

template<class T>
void f(T a, T b){
    if(a>b) {...}
}

在上述代码中,如果T的数据类型传入的是像Person这样的自定义数据类型,也无法正常运行

解决方法:提供模板的重载,可以为这些特定的类型提供具体化的模板

class Person{
    public:
        string Name;
        int Age;
};
​
//利用具体化Person的版本实现代码,具体化优先调用
template<> boolmyCompare(Person &p1, Person &p2){
    if(p1.Name==p2.Name&&p1.Age==p2.Age){
        return true;
    }
    else{
        return false;
    }
}
1.2类模板

作用:建立一个通用类,类中的成员数据类型可以不具体制定,用一个虚拟的类型来代替

语法:

template<typename T>
类
#include<iostream>
#include<string>
using namespace std;
​
template<class NameType, class AgeType>
class Person {
public:
    NameType Name;
    AgeType Age;
​
    Person(NameType name,AgeType age) {
        this->Name = name;
        this->Age = age;
    }
​
    void showPerson() {
        cout << "name=" << this->Name << "age=" << this->Age << endl;
    }
};
​
void test01() {
​
    Person<string, int> p1("孙悟空", 13);
    p1.showPerson();
}
​
​
int main() {
​
    test01();
​
    system("pause");
    return 0;
}

类模板与函数模板区别:

  • 类模板没有自动类型推导的使用方式

  • 类模板在模板参数列表中可以有默认参数

#include<iostream>
#include<string>
using namespace std;
​
//类模板
template<class NameType, class AgeType = int>
class Person {
public:
    NameType Name;
    AgeType Age;
​
    Person(NameType name,AgeType age) {
        this->Name = name;
        this->Age = age;
    }
​
    void showPerson() {
        cout << "name=" << this->Name << "age=" << this->Age << endl;
    }
};
​
void test01() {
​
    Person p1("孙悟空", 13); //错误,无法用自动类型推导
    Person<string, int> p1("孙悟空", 13);
    p1.showPerson();
}
​
void test02() {
​
    Person<string> p2("孙悟空", 13);
    p1.showPerson();
}
​
int main() {
​
    test01();
    test02();
​
    system("pause");
    return 0;
}

类模板中成员函数创建时机:

  • 普通类中的成员函数一开始就可以创建

  • 类模板中的成员函数在调用时才创建

类模板对象做函数参数传入方式:

  • 指定传入的类型 -- 直接显示对象的数据类型

  • 参数模板化 -- 将对象中的参数变为模板进行传递

  • 整个类模板化 -- 将这个对象类型模板化进行传递

#include<iostream>
#include<string>
using namespace std;
​
//类模板
template<class T1, class T2>
class Person {
public:
    T1 Name;
    T2 Age;
​
    Person(T1 name,T2 age) {
        this->Name = name;
        this->Age = age;
    }
​
    void showPerson() {
        cout << "name=" << this->Name << "age=" << this->Age << endl;
    }
};
​
//1.指定传入类型
void printPerson1(Person<string, int>&p){
    p.showPerson();
    
}
​
//2.参数模板化
template<class T1,class T2>
void printPerson2(Person<T1, T2>&p){
    p.showPerson();
    cout<<"T1的类型:"<<typeid(T1).name()<<endl;
    cout<<"T2的类型:"<<typeid(T2).name()<<endl;
    
}
​
//3.整个类模板化
template<class T>
void printPerson3(T &p){
    p.showPerson();
    
}
​
void test01() {
    Person<string, int> p1("孙悟空", 13);
    printPerson1(p1);
}
​
void test02() {
    Person<string, int> p1("猪八戒", 93);
    p1.showPerson2(p1);
}
​
void test03() {
    Person<string, int> p1("猪八戒", 93);
    p1.showPerson3(p1);
}
​
int main() {
​
    test01();
    test02();
    test03();
​
    system("pause");
    return 0;
}

类模板与继承

  • 当子类继承的父类是一个类模板时,子类在声明的时候,要指定出父类中T的类型

  • 如果不指定,编译器无法给子类分配内存

  • 如果想灵活指定出父类中T的类型,子类也需变为类模板

#include<iostream>
using namespace std;
​
template<class T>
class Base{
    T m;
};
​
class Son:public Base<int>{
    
};
​
void test01(){
    Son s1;
}
​
template<class T1, class T2>
class Son2:public Base<T2>{
    T1 obj;
}
​
void test02(){
    Son2<int, char>S2;
}
​
int main(){
    test01();
    test02();
    
    return 0;
}

类模板成员函数类外实现

#include<iostream>
#include<string>
using namespace std;
​
//类模板
template<class T1, class T2>
class Person {
public:
    T1 Name;
    T2 Age;
​
    Person(T1 name,T2 age);
​
    void showPerson();
};
​
template<class T1, class T2>
Person<T1,T2>::Person(T1 name,T2 age){
    this->Name = name;
    this->Age = age;
}
​
template<class T1, class T2>
void Person<T1,T2>::showPerson(){
    cout << "name=" << this->Name << "age=" << this->Age << endl;
}
​
void test01() {
    Person<string, int> p1("孙悟空", 13);
    p1.showPerson();
}
​
int main() {
​
    test01();
​
    system("pause");
    return 0;
}

类模板分文件编写

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

解决:

  • 解决方式1:直接包含.cpp源文件

person.h

#pragma once
#include<iostream>
#include<string>
using namespace std;
​
//类模板
template<class T1, class T2>
class Person {
public:
    T1 Name;
    T2 Age;
​
    Person(T1 name,T2 age);
​
    void showPerson();
};

person.cpp

#include "person.h"
​
template<class T1, class T2>
Person<T1,T2>::Person(T1 name,T2 age){
    this->Name = name;
    this->Age = age;
}
​
template<class T1, class T2>
void Person<T1,T2>::showPerson(){
    cout << "name=" << this->Name << "age=" << this->Age << endl;
}

main.cpp

#include<iostream>
using namespace std;
#include "person.cpp"
​
void test01() {
    Person<string, int> p1("孙悟空", 13);
    p1.showPerson();
}
​
int main() {
​
    test01();
​
    system("pause");
    return 0;
}
  • 解决方式2:将声明和实现写到同一个文件中,并更改后缀名为.hpp,hpp是约定的名称,并不是强制

person.hpp

#pragma once
#include<iostream>
#include<string>
using namespace std;
​
//类模板
template<class T1, class T2>
class Person {
public:
    T1 Name;
    T2 Age;
​
    Person(T1 name,T2 age);
​
    void showPerson();
};
​
template<class T1, class T2>
Person<T1,T2>::Person(T1 name,T2 age){
    this->Name = name;
    this->Age = age;
}
​
template<class T1, class T2>
void Person<T1,T2>::showPerson(){
    cout << "name=" << this->Name << "age=" << this->Age << endl;
}

mian.cpp

#include<iostream>
using namespace std;
#include "person.hpp"
​
void test01() {
    Person<string, int> p1("孙悟空", 13);
    p1.showPerson();
}
​
int main() {
​
    test01();
​
    system("pause");
    return 0;
}

类模板与友元

  • 全局函数类内实现 -- 直接在类内声明友元即可

  • 全局函数类外实现 -- 需要提前让编译器知道全局函数的存在

#include<iostream>
#include<string>
using namespace std;
​
template<class T1, class T2>
class Person;
​
//类外实现
template<class T1, class T2>
void showPerson2(Person<T1, T2> p){
    cout << "name=" << this->Name << "age=" << this->Age << endl;
}
​
template<class T1, class T2>
class Person {
private:
    T1 Name;
    T2 Age;
    
public:
    Person(T1 name,T2 age){
        this->Name = name;
        this->Age = age;
    }
​
    //全局函数类内实现
    friend void showPerson(Person<T1,T2> p){
        cout << "name=" << this->Name << "age=" << this->Age << endl;
    }
    
    //全局函数类外实现
    friend void showPerson2(Person<T1,T2> p);
};
​
//测试全局函数类内实现
void test01() {
    Person<string, int> p1("孙悟空", 13);
    p1.showPerson(p1);
}
​
//测试全局函数类外实现
void test02() {
    Person<string, int> p1("孙悟空", 13);
    p1.showPerson2(p1);
}
​
int main() {
​
    test01();
    test02();
​
    system("pause");
    return 0;
}
案例

实现一个通用的数组类

MyArray.hpp

#pragma once
#include<iostream>
using namespace std;
​
template<class T>
class MyArray {
private:
    T* pAddress; //指针指向堆区开辟的真实数组
    int Capacity; //数组容量
    int Size; //数组大小
​
public:
    MyArray(int capacity) {
        this->Capacity = capacity;
        this->Size = 0;
        this->pAddress = new T[this->Capacity];
    }
​
    // 拷贝构造
    MyArray(const MyArray& arr) {
        this->Capacity = arr.Capacity;
        this->Size = arr.Size;
​
        //深拷贝
        this->pAddress = new T[arr.Capacity];
        for (int i = 0; i < this->Size; i++) {
            this->pAddress[i] = arr.pAddress[i];
        }
    }
​
    //operator= 防止浅拷贝问题
    MyArray& operator=(const MyArray& arr) {
        //先判断原来堆区是否有数据,如果有先释放
        if (this->pAddress != NULL) {
            delete[] this->pAddress;
            this->pAddress = NULL;
            this->Capacity = 0;
            this->Size = 0;
        }
​
        //深拷贝
        this->Capacity = arr.Capacity;
        this->Size = arr.Size;
        this->pAddress = new T[arr.Capacity];
        for (int i = 0; i < this->Size; i++) {
            this->pAddress[i] = arr.pAddress[i];
        }
        return *this;
    }
​
    //尾插法
    void Push_Back(const T& val) {
        //判断容量是否等于大小
        if (this->Capacity == this->Size) {
            return;
        }
        this->pAddress[this->Size] = val;//在数组末尾插入数据
        this->Size++;//更新数组大小
    }
​
    //尾删法
    void Pop_Back() {
        //让用户访问不到最后一个元素
        if (this->Size==0) {
            return;
        }
        this->Size--;//更新数组大小
    }
​
    //返回数组的容量
    int getCapacity() {
        return this->Capacity;
    }
​
    //返回数组的大小
    int getSize() {
        return this->Size;
    }
​
    //通过下标的方式访问数组中的元素
    T& operator[](int index) {
        return this->pAddress[index];
    }
​
    ~MyArray() {
        if (this->pAddress != NULL) {
            delete[] this->pAddress;
            this->pAddress = NULL;
        }
    }
};

mian.cpp

#include<iostream>
using namespace std;
#include "MyArray.hpp"
#include<string>
​
void printIntArray(MyArray<int>& arr) {
    for (int i = 0; i < arr.getSize(); i++) {
        cout << arr[i] << endl;
    }
}
​
void test01() {
    MyArray <int>arr1(5);
​
    for (int i = 0; i < 5; i++) {
        arr1.Push_Back(i);
    }
    cout << "输出:" << endl;
    printIntArray(arr1);
​
    cout << "容量:" <<arr1.getCapacity()<< endl;
    cout << "大小:" <<arr1.getSize()<< endl;
​
    MyArray <int>arr2(arr1);
    cout << "输出:" << endl;
    printIntArray(arr2);
​
    arr2.Pop_Back();
    cout << "容量:" << arr2.getCapacity() << endl;
    cout << "大小:" << arr2.getSize() << endl;
}
​
//测试自定义数据类型
class Person {
public:
    string Name;
    int Age;
​
    Person() { };
    Person(string name, int age) {
        this->Name = name;
        this->Age = age;
    }
​
};
​
void printPersonArray(MyArray<Person>& arr) {
    for (int i = 0; i < arr.getSize(); i++) {
        cout << "姓名:" << arr[i].Age << "年龄:" << arr[i].Age<< endl;
    }
}
​
void test02() {
    MyArray<Person> arr(10);
​
    Person p1("孙悟空", 999);
    Person p2("安其拉", 19);
    Person p3("后裔", 9);
    Person p4("庄周", 9);
    Person p5("孙空", 88);
​
    //将数据插入到数组中
    arr.Push_Back(p1);
    arr.Push_Back(p2);
    arr.Push_Back(p3);
    arr.Push_Back(p4);
    arr.Push_Back(p5);
​
    printPersonArray(arr);
​
    cout << "容量:" << arr.getCapacity() << endl;
    cout << "大小:" << arr.getSize() << endl;
​
}
​
int main() {
    test01();
    test02();
​
    system("pause");
    return 0;
}

2.STL初识

2.1STL基本概念
  • STL(Standard Template Library,标准模板库)

  • STL从广义上分为:容器(container) 算法(algorithm) 迭代器(iterator)

  • 容器和算法之间通过迭代器进行无缝连接

  • STL几乎所有的代码都采用了模板类或者模板函数

2.2STL六大组件
  1. 容器:各种数据结构,如vector、list、deque、set、map等,用来存放数据

        STL容器就是将运用最广泛的一些数据结构实现出来

        常用的数据结构:数组、链表、树、栈、队列、集合、映射表等

        这些容器分为序列式容器关联式容器两种:

                序列式容器:强调值得排序,序列式容器中得每个元素均有固定得位置

                关联式容器:二叉树结构、各元素之间没有严格得物理上得顺序关系

     2.算法:各种常用的算法,如sort、find、copy、for_each等

        有限的步骤,解决逻辑或数学上的问题

        算法分为:质变算法非质变算法

                质变算法:是指运算过程中会更改区间内的元素的内容,例如拷贝、删除等

                非质变算法:是指运算过程中不会更改区间内的元素内容,例如查找、计数、遍历、寻找极值等

     3.迭代器:扮演了容器和算法之间的胶合剂

        提供一种方法,使之能够依序寻访某个容器所含的各个元素,而又无需暴露该容器的内部表示方式。

        每个容器都有自己专属的迭代器

        迭代器种类:

种类功能支持运算
输入迭代器对数据的只读访问只读,支持++、==、!=
输出迭代器对数据的只写访问只写,支持++
向前迭代器读写操作,并能向前推进迭代器读写,支持++、==、!=
双向迭代器读写操作,并能向前和向后操作读写,支持++、--
随机访问迭代器读写操作,可以以跳跃的方式访问任意数据,功能最强的迭代器读写,支持++、--、[n]、-n、<、<=、>、>=

        常用的容器中迭代器种类为双向迭代器,和随机访问迭代器

     4.仿函数:行为类似函数,可作为算法的某种策略

     5.适配器:一种用来修饰容器或者仿函数或迭代器接口的东西

     6.空间配置器:负责空间的配置与管理

2.3容器算法迭代器初始

Vector存放内置数据类型

容器:vector

算法:for_each

迭代器:vector<int>::iterator

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm> //标准算法头文件
​
void myPrint(int val) {
    cout << val << endl;
}
​
void test01() {
    //创建了一个vector容器,数组
    vector<int> v;
​
    //向容器中插入数据
    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);
​
    //通过迭代器访问容器中的数据
    //vector<int>::iterator itBegin = v.begin(); //起始迭代器,指向容器中第一个元素
    //vector<int>::iterator itEnd = v.end(); //结束迭代器,指向容器中最后一个元素的下一个位置
​
    //第一种遍历方式
    /*while (itBegin != itEnd) {
        cout << *itBegin << endl;
        itBegin++;
    }*/
​
    //第二种遍历方式
    /*for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
        cout << *it << endl;
    }*/
​
    //第三种遍历方式
    for_each(v.begin(), v.end(), myPrint);
}
​
int main() {
​
    test01();
​
    return 0;
}

Vector存放自定义数据类型

#include<iostream>
using namespace std;
#include<vector>
​
class Person {
public:
    string Name;
    int Age;
​
    Person(string name, int age) {
        this->Name = name;
        this->Age = age;
    }
};
​
void test01() {
    //创建了一个vector容器,数组
    vector<Person> v;
​
    //向容器中插入数据
    Person p1("aaa", 10);
    Person p2("aaa", 20);
    Person p3("aaa", 30);
    Person p4("aaa", 40);
    Person p5("aaa", 50);
​
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);
​
    for(vector<Person>::iterator it = v.begin(); it != v.end(); it++) {
        cout << "姓名:" << (*it).Name << "年龄:" << (*it).Age << endl;
    }
}
​
// 存放自定义数据类型 指针
void test02() {
    vector<Person*> v;
​
    //向容器中插入数据
    Person p1("aaa", 10);
    Person p2("aaa", 20);
    Person p3("aaa", 30);
    Person p4("aaa", 40);
    Person p5("aaa", 50);
​
    v.push_back(&p1);
    v.push_back(&p2);
    v.push_back(&p3);
    v.push_back(&p4);
    v.push_back(&p5);
​
    for (vector<Person*>::iterator it = v.begin(); it != v.end(); it++) {
        cout << "姓名:" << (*it)->Name << "年龄:" << (*it)->Age << endl;
    }
}
​
int main() {
​
    test01();
    test02();
​
    return 0;
}

Vector容器嵌套容器

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm> //标准算法头文件
​
void test01() {
    //创建了一个vector容器,数组
    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 vit = (*it).begin(); vit != (*it).end(); vit++) {
            cout << *vit << " ";
        }
        cout << endl;
    }
}
​
int main() {
​
    test01();
​
    return 0;
}

3.STL-常用容器

3.1string容器

3.1.1string基本概念

本质:string是C++风格的字符串,而string本质上是一个类

string和char*区别:

  • char * 是一个指针

  • string是一个类,类内部封装了char * ,管理这个字符串,是一个char * 型的容器

特点:string类内部封装了很多成员方法

string管理char * 所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责

3.1.2string构造函数

构造函数原型:

  • string(); //创建一个空的字符串 例如:string str;

string(const char* s); //使用字符串s初始化

  • string(const string str); //使用一个string对象初始化另一个string对象

  • string(int n, chat c); //使用n个字符c初始化

#include<iostream>
using namespace std;
#include<string>
​
void test01() {
    string s1;
​
    const char* str = "hello world";
    string s2(str);
    cout << "s2=" << s2 << endl;
​
    string s3(s2);
    cout << "s3=" << s3 << endl;
​
    string s4(10, 'a');
    cout << "s4=" << s4 << endl;
}
​
int main() {
​
    test01();
​
    return 0;
}

3.1.3string赋值操作

  • string& operator=(const char * s); //char*类型字符串赋值给当前的字符串

  • string& operator=(const string &s); //把字符串s赋给当前的字符串

  • string& operator=(chat c); //字符赋值给当前的字符串

  • string& assign(const char * s); //把字符串s赋给当前的字符串

  • string& assign(const char * s, int n); //把字符串s的前n个字符赋给当前的字符串

  • string& assign(const string &s); //把字符串s赋给当前字符串

  • string& assign(int n, char c); //用n个字符c赋给当前字符串

#include<iostream>
using namespace std;
#include<string>
​
void test01() {
    
    string str1;
    str1 = "hello world";
    cout << "str1=" << str1 << endl;
    
    string str2;
    str2 = str1;
    cout << "str2=" << str2 << endl;
​
    string str3;
    str3 = 'a';
    cout << "str3=" << str3 << endl;
​
    string str4;
    str4.assign("hello");
    cout << "str4=" << str4 << endl;
​
    string str5;
    str5.assign("hello", 4);
    cout << "str5=" << str5 << endl;
​
    string str6;
    str6.assign(str5);
    cout << "str6=" << str6 << endl;
​
    string str7;
    str7.assign(5, 'w');
    cout << "str7=" << str7 << endl;
}
​
int main() {
​
    test01();
​
    return 0;
}

3.1.4string字符串拼接

  • string& operator+=(const char * str); //重载+=操作符

  • string& operator+=(const chat c); //重载+=操作符

  • string& operator+=(const string& str); //重载+=操作符

  • string& append(const char * s); //把字符串s连接到当前字符串结尾

  • string& append(const char * s, int n); //把字符串s的前n个字符连接到当前字符串结尾

  • string& append(const string &s); //同operator+=(const string& str)

  • string& append(const string &s, int pos, int n); //字符串s中从pos开始的n个字符连接到字符串结尾

#include<iostream>
using namespace std;
#include<string>
​
void test01() {
    
    string str1 = "我";
    str1 += "爱玩游戏";
    cout << "str1=" << str1 << endl;
    
    str1 += ":";
    cout << "str1=" << str1 << endl;
​
    string str2 = "LOL";
    str1 += str2;
    cout << "str1=" << str1 << endl;
​
    string str3="I";
    str3.append("Love");
    cout << "str3=" << str3 << endl;
​
    str3.append("game abc",4);
    cout << "str3=" << str3 << endl;
​
    str3.append(str2);
    cout << "str3=" << str3 << endl;
​
    str3.append(str2,0,1);
    cout << "str3=" << str3 << endl;
}
​
int main() {
​
    test01();
​
    return 0;
}
​
结果:
str1=我爱玩游戏
str1=我爱玩游戏:
str1=我爱玩游戏:LOL
str3=ILove
str3=ILovegame
str3=ILovegameLOL
str3=ILovegameLOLL

3.1.5string查找和替换

  • int find(const string& str, int pos = 0) const; //查找str第一次出现位置,从pos开始查找

  • int find(const char* s, int pos = 0) const; //查找s第一次出现位置,从pos开始查找

  • int find(const char* s, int pos, int n) const; //从pos位置查找s的前n个字符第一位置

  • int find(const char c, int pos = 0) const; //查找字符c第一次出现位置

  • int rfind(const string& str, int pos = npos) const; //查找str最后一次位置,从pos开始查找

  • int rfind(const char& s, int pos = npos) const; //查找s最后一次出现位置,从pos开始查找

  • int rfind(const char& s, int pos, int n) const; //从pos查找s的前n个字符最后一次位置

  • int rfind(const char c, int pos=0) const; //查找字符c最后一次出现位置

  • string& replace(int pos, int n, const string& str); //替换从pos开始n个字符为字符串str

  • string& replace(int pos, int n, const char& s); //替换从pos开始n个字符为字符串s

#include<iostream>
using namespace std;
#include<string>
​
//查找
void test01() {
    
    string str1 = "abcdefg";
    int ret = str1.find("d");
    cout << ret << endl;
​
    //rfind和find区别
    //rfind从右往左查找,find从左往右查找
    ret = str1.rfind("de");
    cout << ret << endl;
}
​
//替换
void test02() {
​
    string str1 = "abcdefg";
    str1.replace(1, 3, "1111");
    cout << str1 << endl;
}
​
int main() {
​
    test01();
    test02();
​
    return 0;
}
​
结果:
3
3
a1111efg

总结:

  • rfind从右往左查找,find从左往右查找

  • find找到字符串后返回查找的第一个字符位置,找不到返回-1

  • replace在替换时,要指定从那个位置起,多少个字符,替换成什么样的字符串

3.1.6string字符串比较

= 返回 0

>返回 1

<返回 -1

  • int compare(const string& s) const; //与字符串s比较

  • int compare(const char* s) const; //与字符串s比较

#include<iostream>
using namespace std;
#include<string>
​
void test01() {
    
    string str1 = "hello";
    string str2 = "hello"
​
    if(str1.compare(str2)==0){
        cout<<"str1=str2"<<endl;
    }
    else if(str1.compare(str2)>0){
        cout<<"str1>str2"<<endl;
    }
    else{
        cout<<"str1<str2"<<endl;
    }
}
​
int main() {
​
    test01();
​
    return 0;
}

3.1.7string字符存取

  • char& operator[](int n); //通过[ ]方式取字符

  • char& at(int n); //通过at方法获取字符

#include<iostream>
using namespace std;
#include<string>
​
void test01() {
    
    string str = "hello";
    cout<<"str="<<str<<endl;
    
    for(int i=0;i<str.size();i++){
        cout<<str[i]<<" ";
    }
    cout<<endl;
    
    for(int i=0;i<str.size();i++){
        cout<<str.at(i)<<" ";
    }
    cout<<endl;
    
    //修改单个字符
    str[0] = 'x';
    cout<<str<<endl;
}
​
int main() {
​
    test01();
​
    return 0;
}

3.1.8string插入和删除

  • strgin& insert[](int pos, const char* s); //插入字符串

  • strgin& insert[](int pos, const string& str); //插入字符串

  • strgin& insert[](int pos, int n, char c); //在指定位置插入n个字符c

  • strgin& erase[](int pos, int n = npos); //删除从pos开始的n个字符

#include<iostream>
using namespace std;
#include<string>
​
void test01() {
    
    string str = "hello";
    
    str.insert(1,"111");
    cout<<"str="<<str<<endl;
    
    str.erase(1,3);
    cout<<"str="<<str<<endl;
    
}
​
int main() {
​
    test01();
​
    return 0;
}
​
结果:
h111ello
hello

3.1.9string子串

  • strgin& substr(int pos = 0, int n = npos) const; //返回由pos开始的n个字符组成的字符串

#include<iostream>
using namespace std;
#include<string>
​
void test01() {
    
    string str = "hello";
    
    string subStr=str.substr(1,3);
    cout<<"subStr="<<subStr<<endl;
    
}
​
int main() {
​
    test01();
​
    return 0;
}
​
结果:
subStr=ell
3.2vector容器

vector数据结构和数组非常相似,可称为单端数组

vector和普通数组的区别:数组是静态孔家,而vector可以动态扩展

动态扩展:并不是在原有的空间之后续接新空间,而是找更大的内存空间,然后将原数据拷贝新空间,释放原空间

vector容器的迭代器是支持随机访问的迭代器

3.2.1vector构造函数

  • vector<T> v; //采用模板实现类实现

  • vector(v.begin(), v.end()); //将v[begin(), end()]区间中的元素拷贝给本身

  • vector(n, elem); //构造函数将n个elem拷贝给本身

  • vector(const vector & vec); //拷贝构造函数

#include<iostream>
using namespace std;
#include<vector>
​
void printVector(vector<int> &v ){
    for(vector<int>::iterator it=v.begin();it != v.end(); it++){
        cout<<*it<<" "<<endl;
    }
}
​
void test01() {
    
    vector<int>v1;
    
    for(int i=0;i<10;i++){
        v1.push_back(i);
    }
    printVector(v1);
    
    vector<int>v2(v1.begin(),v1.endl());
    printVector(v2);
    
    vector<int>v3(10,100);
    printVector(v3);
    
    vector<int>v4(v3);
    printVector(v4);
    
}
​
int main() {
​
    test01();
​
    return 0;
}

3.2.2vector赋值操作

  • vecto& operator=(const vector &vec); //重载等号操作符

  • assign(beg, end); //将[beg,end]区间中的数据拷贝赋值给本身

  • assign(n, elem); //将n个elem拷贝赋值给本身

#include<iostream>
using namespace std;
#include<vector>
​
void printVector(vector<int> &v ){
    for(vector<int>::iterator it=v.begin();it != v.end(); it++){
        cout<<*it<<" "<<endl;
    }
}
​
void test01() {
    
    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);
    
}
​
int main() {
​
    test01();
​
    return 0;
}

3.2.3vector容量和大小

  • empty(); //判断容器是否为空

  • capacity(); //容器的容量

  • size(); //返回容器中元素的个数

  • resize(int num); //重新指定容器的长度为num,若容器变长,则以默认值0填充新位置;若容器变短,则末尾超出容器长度的元素被删除

  • resize(int num, elem); //重新指定容器的长度为num,若容器变长,则以elem值0填充新位置;若容器变短,则末尾超出容器长度的元素被删除

#include<iostream>
using namespace std;
#include<vector>
​
void printVector(vector<int> &v ){
    for(vector<int>::iterator it=v.begin();it != v.end(); it++){
        cout<<*it<<" "<<endl;
    }
    cout<<endl;
}
​
void test01() {
    
    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; //13
        cout<<"v1的大小为:"<<v1.size()<<endl; //10
    }
    v1.resize(15);
    printVector(v1);  //0 1 2 3 4 5 6 7 8 9 0 0 0 0 0
    
    v1.resize(15, 100);
    printVector(v1);
    
    v1.resize(5);
    printVector(v1);  //0 1 2 3 4
    
}
​
int main() {
​
    test01();
​
    return 0;
}

3.2.4vector插入和删除

  • push_back(ele); //尾部插入元素ele

  • pop_back(); //删除最后一个元素

  • insert(const_iterator pos, ele); //迭代器指向位置pos插入元素ele

  • insert(const_iterator pos, int count, ele); //迭代器指向位置pos插入count个元素ele

  • erase(const_iterator pos); //删除迭代器指向的元素

  • erase(const_iterator start, const_iterator end); //删除迭代器从start到end之间的元素

  • clear(); //删除容器中所有元素

#include<iostream>
using namespace std;
#include<vector>
​
void printVector(vector<int> &v ){
    for(vector<int>::iterator it=v.begin();it != v.end(); it++){
        cout<<*it<<" "<<endl;
    }
    cout<<endl;
}
​
void test01() {
    
    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, 1);
    printVector(v1);
    
    //删除
    v1.erase(v1.begin());
    printVector(v1);
    
    //清空
    //v1.erase(v1.begin(), v1.end());
    v1.clear();
    printVector(v1);
    
}
​
int main() {
​
    test01();
​
    return 0;
}

3.2.5vector数据存取

  • at(int idx); //返回索引idx所指的数据

  • operator[]; //返回索引idx所指的数据

  • front(); //返回容器中第一个数据元素

  • back(); //返回容器中最后一个数据元素

#include<iostream>
using namespace std;
#include<vector>
​
void test01() {
    
    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.front()<<endl;
    
    cout<<"最后一个元素:"<<v1.back()<<endl;
    
}
​
int main() {
​
    test01();
​
    return 0;
}

3.2.6vector互换容器

  • swap(vec); //将vec与本身的元素互换

#include<iostream>
using namespace std;
#include<vector>
​
void printVector(vector<int> &v ){
    for(vector<int>::iterator it=v.begin();it != v.end(); it++){
        cout<<*it<<" "<<endl;
    }
    cout<<endl;
}
​
//1.基本使用
void test01() {
    
    vector<int>v1;
    
    for(int i=0;i<10;i++){
        v1.push_back(i);
    }
    printVector(v1);
    
    vector<int>v2;
    for(int i=0;i>0;i--){
        v2.push_back(i);
    }
    printVector(v2);
    
    cout<<"交换后:"<<endl;
    v1.swap(v2);
    printVector(v1);
    printVector(v2);
}
​
//2.实际使用:收缩内存空间
void test02(){
    vector<int>v;
    
    for(int i=0;i<1000;i++){
        v.push_back(i);
    }
    cout<<"容量:"<<v.capacity()<<endl;
    cout<<"大小:"<<v.size()<<endl;
    
    v.resize(3);
    cout<<"容量:"<<v.capacity()<<endl;
    cout<<"大小:"<<v.size()<<endl;
    
    //收缩内存空间
    vector<int>(v).swap(v);
    cout<<"容量:"<<v.capacity()<<endl;
    cout<<"大小:"<<v.size()<<endl;
}
​
int main() {
​
    test01();
    test02();
​
    return 0;
}

3.2.7vector预留空间

  • reserve(int len); //容器预留len个元素长度,预留位置不初始化,元素不可访问

#include<iostream>
using namespace std;
#include<vector>
​
void printVector(vector<int> &v ){
    for(vector<int>::iterator it=v.begin();it != v.end(); it++){
        cout<<*it<<" "<<endl;
    }
    cout<<endl;
}
​
void test02(){
    vector<int>v;
    
    v.reserve(1000);
    
    int num=0;
    int * p = NULL;
    for(int i=0;i<1000;i++){
        v.push_back(i);
        if(p!=&v[0]){
            p=&v[0];
            num++;
        }
    }   
    
}
​
int main() {
​
    test02();
​
    return 0;
}
3.3deque容器

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

deque与vector区别:

  • vector对于头部的插入和删除效率低,数据量越大,效率越低

  • deque相对而言,对头部的插入删除速度比vector快

  • vector访问元素时的速度会比deque快,这和两者内部实现有关

deque内部工作原理:

deque内部有个中控器,维护每段缓冲区中的内容,缓冲区中存放真实数据

中控器维护的是每个缓冲区的地址,使得使用deque时像一片连续得内存空间

3.3.1deque构造函数

  • deque<T> deq; //默认构造形式

  • deque(beg, end); //构造函数将[beg, end]区间中的元素拷贝给本身

  • de/que(n, elem); //构造函数将n个elem拷贝给本身

  • deque(const deque &deq); //拷贝构造函数

#include<iostream>
using namespace std;
#include<deque>
​
void printDeque(const deque<int>&d) {
    for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}
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);
}
​
int main() {
​
    test01();
​
    return 0;
}

3.3.2deque赋值操作

  • deque& operator=(const deque &deq); //重载等号操作符

  • assign(beg, end); //将[beg, end]区间中的数据拷贝赋值给本身

  • assign(n, elem); //将n个elem拷贝赋值给本身

#include<iostream>
using namespace std;
#include<deque>
​
void printDeque(const deque<int>&d) {
    for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}
void test01() {
    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(), d2.end())
    printDeque(d3);
    
    deque<int>d4;
    d4.assign(10, 100);
    printDeque(d4);
}
​
int main() {
​
    test01();
​
    return 0;
}

3.3.3deque大小操作

  • deque.empty(); //判断容器是否为空

  • deque.size(); //返回容器中元素得个数

  • deque.resize(num); //重新指定容器得长度为num,若容器变长,则以默认值填充新位置;若容器变短,则末尾超出容器长度得元素被删除

  • deque.resize(num, elem); //重新指定容器得长度为num,若容器变长,则以elem值填充新位置;若容器变短,则末尾超出容器长度得元素被删除

#include<iostream>
using namespace std;
#include<deque>
​
void printDeque(const deque<int>&d) {
    for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}
void test01() {
    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,1);
    printDeque(d1);
    
    d1.resize(5);
    printDeque(d1);
}
​
int main() {
​
    test01();
​
    return 0;
}

3.3.4插入和删除

两端插入\删除操作:

  • push_back(elem); //在容器尾部添加一个数据

  • push_front(elem); //在容器头部插入一个数据

  • pop_back(); //删除容器最后一个数据

  • pop_front(); //删除容器第一个数据

指定位置插入\删除操作:

  • insert(pos, elem); //在pos位置插入一个elem元素的拷贝,返回新数据的位置

  • insert(pos, n, elem); //在pos位置插入n个elem数据,无返回值

  • insert(pos, beg, end); //在pos位置插入[beg, end]区间的数据,无返回值

  • clear(); //清空容器的所有数据

  • erase(beg, end); //删除[beg, end]区间的数据,返回下一个数据的位置

  • erase(pos); //删除pos位置的数据,返回下一个数据的位置

#include<iostream>
using namespace std;
#include<deque>
​
void printDeque(const deque<int>&d) {
    for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}
​
void test01() {
    deque<int>d1;
    
    //尾插
    d1.push_back(10);
    d1.push_back(20);
    
    //头插
    d1.push_front(100);
    d1.push_front(200);
    
    printDeque(d1);
    
    //尾删
    d1.pop_back();
    printDeque(d1);
    
    //头删
    d1.pop_front();
    printDeque(d1);
}
​
void test02() {
    deque<int>d1;
    
    d1.push_back(10);
    d1.push_back(20);
    d1.push_front(100);
    d1.push_front(200);
    printDeque(d1);
    
    d1.insert(d1.begin(), 1000);
    printDeque(d1);
    
    d1.insert(d1.begin(), 2, 10000);
    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);
}
​
void test03(){
    deque<int>d1;
    
    d1.push_back(10);
    d1.push_back(20);
    d1.push_front(100);
    d1.push_front(200);
    printDeque(d1); //200 100 10 20
    
    deque<int>::iterator it = d1.begin();
    it++;
    d1.erase(it);
    printDeque(d1); //200 10 20
    
    //清空
    d1.erase(d1.begin(), d1.end());
    //d1.clear();
    printDeque(d1);
}
​
int main() {
​
    test01();
    test02();
    test03();
​
    return 0;
}
​
结果:
200 100 10 20
200 100 10
100 10
200 100 10 20
1000 200 100 10 20
10000 10000 1000 200 100 10 20
1 2 3 10000 10000 1000 200 100 10 20

3.3.5数据存取

  • at(int idx); //返回索引idx所指的数据

  • operator[]; //返回索引idx所指的数据

  • back(); //返回容器中最后一个数据元素

  • front(); //返回容器中第一个数据元素

#include<iostream>
using namespace std;
#include<deque>
​
void test01() {
    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<<d[i]<<" "
    }
    cout<<endl;
    
    for(int i=0;i<d1.size();i++){
        cout<<d.at(i)<<" "
    }
    cout<<endl;
    
    cout<<"第一个元素:"<<d.front<<endl;
    cout<<"第一个元素:"<<d.back<<endl;
}
​
int main() {
​
    test01();
​
    return 0;
}
​
结果:
200 100 10 20
200
20

3.3.6deque排序

  • sort(iterator beg, iterator end); //对beg和end区间内元素进行排序

#include<iostream>
using namespace std;
#include<deque>
#include<algorithm>
​
void printDeque(const deque<int>&d) {
    for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}
​
void test01() {
    deque<int>d1;
    d1.push_back(10);
    d1.push_back(20);
    d1.push_front(100);
    d1.push_front(200);
    printDeque(d1);
    
    sort(d1.begin(), end());
    printDeque(d1);
}
​
int main() {
​
    test01();
​
    return 0;
}
​
结果:
200 100 10 20
10 20 100 200
案例—评委打分
#include<iostream>
using namespace std;
#include<vector>
#include<string>
#include<deque>
#include<algorithm>
#include<ctime>
​
class Person {
public:
    string Name;
    int Score;
​
    Person(string name, int score) {
        this->Name = name;
        this->Score = score;
    }
};
​
void createPerson(vector<Person>&v) {
    string nameSeed = "ABCDE";
    for (int i=0; i<5; i++) {
        string name = "选手";
        name += nameSeed[i];
​
        int score = 0;
        Person p(name, score);
​
        v.push_back(p);
    }
}
​
//打分
void setScore(vector<Person>& v) {
    for (vector<Person>::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->Name << "\t分数:" << endl;
        for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++) {
            cout << *dit << " ";
        }
        cout << endl;*/
​
        //排序
        sort(d.begin(), d.end());
​
        //去除最高和最低分
        d.pop_front();
        d.pop_back();
​
        //平均分
        int sum = 0;
        for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++) {
            sum += *dit;
        }
​
        int avg = sum / d.size();
​
        //将平均分赋值给选手上
        it->Score = avg;
    }
}
​
void showScore(vector<Person>& v) {
    for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {
        cout << "选手:" << it->Name << "\t平均分:" << it->Score << endl;
    }
}
​
int main() {
    //随机数种子
    srand((unsigned int)time(NULL));
​
    //1.创建5名选手
    vector<Person>v;
    createPerson(v);
​
    //2.给5名选手打分
    setScore(v);
    //3.显示最后的得分
    showScore(v);
​
    return 0;
}
3.4stack容器

stack是一种先进后出(Firs In Last Out, FILO)的数据结构,它只有一个出口

构造函数:

  • stack<T> stk; //stack采用模板类实现,stack对象的默认构造形式

  • stack(const stack &stk); //拷贝构造函数

赋值操作:

  • stack& operator=(const stack &stk) //重载等号操作符

数据存取:

  • push(elem); //向栈顶添加元素

  • pop(); //从栈顶移除一个元素

  • top(); //返回栈顶元素

大小操作:

  • empty(); //判断堆栈是否为空

  • size(); //返回栈的大小

#include<iostream>
using namespace std;
#include<stack>
​
void test01() {
    stack<int>s;
​
    s.push(10);
    s.push(20);
    s.push(30);
    s.push(40);
​
    cout << "栈的大小:" << s.size() << endl;
​
    while (!s.empty()) {
        cout << "栈顶元素:" << s.top() << endl;
        s.pop();
    }
​
    cout << "栈的大小:" << s.size() << endl;
}
​
int main() {
​
    test01();
​
    return 0;
}
​
结果:
栈的大小:4
栈顶元素:40
栈顶元素:30
栈顶元素:20
栈顶元素:10
栈的大小:0
3.5queue容器

Queue是一种先进先出(First In First Out, FIFO)的数据结构,它有两个出口

构造函数:

  • queue<T> que; //queue采用模板类实现,queue对象的默认构造形式

  • queue(const queue &que); //拷贝构造函数

赋值操作:

  • queue& operator=(const queue &que) //重载等号操作符

数据存取:

  • push(elem); //向栈顶添加元素

  • pop(); //从栈顶移除一个元素

  • top(); //返回栈顶元素

  • front(); //返回第一个元素

大小操作:

  • empty(); //判断堆栈是否为空

  • size(); //返回栈的大小

#include<iostream>
using namespace std;
#include<queue>
​
class Person {
public:
    string Name;
    int Age;
​
    Person(string name, int age) {
        this->Name = name;
        this->Age = age;
    }
};
​
void test01() {
    queue<Person>q;
​
    Person p1("孙悟空", 30);
    Person p2("唐僧", 550);
    Person p3("猪八戒", 20);
    Person p4("沙僧", 10);
​
    q.push(p1);
    q.push(p2);
    q.push(p3);
    q.push(p4);
​
    cout << "队列的大小:" << q.size() << endl;
​
    while (!q.empty()) {
        cout << "队头元素:" << q.front().Name<<q.front().Age << endl;
        cout << "队尾元素:" << q.back().Name << q.back().Age << endl;
​
        q.pop();
    }
​
    cout << "队列的大小:" << q.size() << endl;
}
​
int main() {
​
    test01();
​
    return 0;
}
​
结果:
队列的大小:4
队头元素:孙悟空30
队尾元素:沙僧10
队头元素:唐僧550
队尾元素:沙僧10
队头元素:猪八戒20
队尾元素:沙僧10
队头元素:沙僧10
队尾元素:沙僧10
队列的大小:0
3.6list容器

将数据进行链式存储

链表(List)是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接实现的。

链表的组成:链表由一系列结点组成

结点的组成:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域

STL中的链表是一个双向循环链表

由于链表的存储方式并不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器

优点:

  • 采用动态存储分配,不会造成内存浪费和溢出

  • 链表执行插入和删除操作十分方便,修改指针即可,不需要移动大量元素

缺点:

  • 链表灵活,但是空间(指针域)和时间(遍历)额外耗费较大

List有一个重要的性质,插入操作和删除操作都不会造成原有list迭代器的失效,这在vector是不成立的。

3.6.1构造函数

  • list<T> list; //list采用模板类实现,对象的默认构造形式

  • list(beg, end); //构造函数将[beg, end]区间中的元素拷贝给本身

  • list(n, elem); //构造函数将n个elem拷贝给本身

  • list(const list &list); //拷贝构造函数

#include<iostream>
using namespace std;
#include<list>
​
void printList(const list<int>& L) {
    for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}
​
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);
​
}
​
int main() {
​
    test01();
​
    return 0;
}

3.6.2list赋值和交换

  • assign(beg, end); //将[beg, end]区间中的数据拷贝赋值给本身

  • assign(n, elem); //将n个elem拷贝赋值给本身

  • list& operator=(const, list &list); //重载等号操作符

  • swap(list); //将list与本身的元素互换

#include<iostream>
using namespace std;
#include<list>
​
void printList(const list<int>& L) {
    for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}
​
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;
    L2=L1;
    printList(L2);
​
    list<int>L3;
    L3.assign(L2.begin(), L2.end());
    printList(L3);
​
    list<int>L4;
    L4.assign(10, 1000);
    printList(L4);
​
}
​
void test02(){
    list<int>L1;
​
    L1.push_back(10);
    L1.push_back(20);
    L1.push_back(30);
    L1.push_back(40);
​
    printList(L1);
    
    list<int>L2;
    L2.assign(10, 1000);
    printList(L2);
    
    //交换后
    L1.swap(L2);
    printList(L1);
    printList(L2);
​
}
​
int main() {
​
    test01();
    test02();
​
    return 0;
}

3.6.3list大小操作

  • size(); //返回容器中元素的个数

  • empty(); //判断容器是否为空

  • resize(num); //重新指定容器的长度为num,若容器变长,则以默认值填充新位置,若容器变短,则末尾超出容器长度的元素被删除

  • resize(num, elem); //重新指定容器的长度为num,若容器变长,则以elem值填充新位置,若容器变短,则末尾超出容器长度的元素被删除

#include<iostream>
using namespace std;
#include<list>
​
void printList(const list<int>& L) {
    for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}
​
void test01() {
    list<int>L1;
​
    L1.push_back(10);
    L1.push_back(20);
    L1.push_back(30);
    L1.push_back(40);
​
    printList(L1);
​
    if(L1.empty()){
        cout<<"L1为空"<<endl;
    }
    else{
        cout<<"L1不为空"<<endl;
        cout<<"L1的元素个数:"<<L1.size()<<endl;
    }
    L1.resize(10);
    printList(L1);
    
    L1.resize(11,10000);
    printList(L1);
    
    L1.resize(2);
    printList(L1);
}
​
int main() {
​
    test01();
​
    return 0;
}

3.6.4list插入和删除

  • push_back(elem); //在容器尾部加入一个元素

  • pop_back(); //删除容器中最后一个元素

  • push_front(elem); //在容器开头插入一个元素

  • pop_front(); //从容器开头移除第一个元素

  • insert(pos, elem); //在pos位置插elem元素的拷贝,返回新数据的位置

  • insert(pos, n, elem); //在pos位置插入n个elem数据,无返回值

  • insert(pos, beg, end); //在pos位置插入[beg, end]区间的数据,无返回值

  • clear(); //移除容器中的所有数据

  • erase(beg,end); //删除[beg, end]区间的数据,返回下一个数据的位置

  • erase(pos); //删除pos位置的数据,返回下一个数据的位置

  • remove(elem); //删除容器中所有与elem值匹配的元素

#include<iostream>
using namespace std;
#include<list>
​
void printList(const list<int>& L) {
    for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}
​
void test01() {
    list<int>L1;
​
    L1.push_back(10);
    L1.push_back(20);
    L1.push_front(30);
    L1.push_front(40);
​
    printList(L1); // 40 30 10 20
    
    L1.pop_back(); 
    printList(L1); // 40 30 10
    
    L1.pop_front(); 
    printList(L1); // 30 10
    
    L1.insert(L1.begin(), 1000); 
    printList(L1); // 1000 30 10 20
    
    list<int>::iterator it = L.begin():
    L1.insert(++it, 10000); 
    printList(L1); // 1000 10000 30 10 20
    
    it=L1.begin();
    L.erase(++it);
    printList(L1); // 1000 30 10 20
    
    L1.remove(1000); 
    printList(L1); // 30 10 20
    
    L1.clear();
    printList(L1); 
​
}
​
int main() {
​
    test01();
​
    return 0;
}

3.6.5list数据存取

  • front(); //返回第一个元素

  • back(); //返回最后一个元素

#include<iostream>
using namespace std;
#include<list>
​
void test01() {
    list<int>L1;
​
    L1.push_back(10);
    L1.push_back(20);
    L1.push_back(30);
    L1.push_back(40);
    
    //L1[0] 不可以用[]访问list容器中的元素
    //L1.at(0) 不可以用at访问list容器中的元素
    //原因是list本质链表,不是用连续线性空间存储数据,迭代器也是不支持随机访问
​
    cout<<"第一个元素:"<<L1.front()<<endl;
    cout<<"最后一个元素:"<<L1.back()<<endl;
}
​
int main() {
​
    test01();
​
    return 0;
}

3.6.6list反转和排序

  • reverse(); //反转链表

  • sort(); //链表排序

#include<iostream>
using namespace std;
#include<list>
​
void printList(const list<int>& L) {
    for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}
​
void test01() {
    list<int>L1;
​
    L1.push_back(10);
    L1.push_back(60);
    L1.push_back(80);
    L1.push_back(40);
    
    printList(L1); //10 60 80 40
    
    L1.reverse();
    printList(L1); //40 80 60 10
    
    L1.sort();
    printList(L1); //10 40 60 80
}
​
int main() {
​
    test01();
​
    return 0;
}

3.6.7排序案例

#include<iostream>
using namespace std;
#include<string>
#include<list>
​
class Person {
public:
    string Name;
    int Age;
    int Height;
​
    Person(string name, int age, int height) {
        this->Name = name;
        this->Age = age;
        this->Height = height;
    }
};
​
bool comparePerson(Person& p1, Person& p2) {
    //按年龄做一个升序
    if (p1.Age == p2.Age) {
        //按照升高降序排列
        return p1.Height > p2.Height;
    }
    return p1.Age < p2.Age;
}
​
void test01() {
    list<Person>L;
​
    Person p1("刘备", 35, 180);
    Person p2("张飞", 25, 188);
    Person p3("关羽", 30, 184);
    Person p4("赵云", 25, 178);
    Person p5("曹操", 33, 184);
​
    L.push_back(p1);
    L.push_back(p2);
    L.push_back(p3);
    L.push_back(p4);
    L.push_back(p5);
​
    for (list<Person>::iterator it = L.begin(); it != L.end(); it++) {
        cout << "姓名:" << (*it).Name << "\t年龄:" << it->Age << "\t身高:" << it->Height <<endl;
    }
    cout << endl;
​
    cout << "排序后:" << endl;
    L.sort(comparePerson);
    for (list<Person>::iterator it = L.begin(); it != L.end(); it++) {
        cout << "姓名:" << (*it).Name << "\t年龄:" << it->Age << "\t身高:" << it->Height <<endl;
    }
    cout << endl;
​
}
​
int main() {
​
    test01();
​
    return 0;
}
3.7set/multiset容器

所有元素都会在插入时自动被排序

本质:set/multiset属于关联式容器,底层结构是用二叉树实现

set和multiset区别:

  • set不可以插入重复数据,而multiset可以

  • set插入数据的同时会返回插入结果,表示插入是否成功

  • multiset不会检测数据,因此可以插入重复数据

3.7.1set构造和赋值

构造:

  • set<T> st; //默认构造函数

  • set(const set &st); //拷贝构造函数

赋值:

  • set& operator=(const set &st); //重载等号操作符

#include<iostream>
using namespace std;
#include<set>
​
void printSet(set<int>& s) {
    for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}
​
void test01() {
    set<int>s1;
​
    s1.insert(10);
    s1.insert(20);
    s1.insert(30);
    s1.insert(40);
    
    printSet(s1);
    
    set<int>s2(s1);
    printSet(s2);
    
    set<int>s3;
    s3=s2;
    printSet(s3);
}
​
int main() {
​
    test01();
​
    return 0;
}

3.7.2set大小和交换

  • size(); //返回容器中元素的数目

  • empty(); //判断容器是否为空

  • swap(st); //交换两个集合容器

#include<iostream>
using namespace std;
#include<set>
​
void printSet(set<int>& s) {
    for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}
​
void test01() {
    set<int>s1;
​
    s1.insert(10);
    s1.insert(20);
    s1.insert(30);
    s1.insert(40);
    
    printSet(s1);
    
    if(s1.empty()){
        cout<<"s1为空"<<endl;
    }
    else{
        cout<<"s1不为空"<<endl;
    }
    
}
​
void test02() {
    set<int>s1;
​
    s1.insert(10);
    s1.insert(20);
    s1.insert(30);
    s1.insert(40);
    printSet(s1);
    
    set<int>s2;
​
    s2.insert(100);
    s2.insert(200);
    s2.insert(300);
    s2.insert(400);
    printSet(s2);
    
    cout<<"交换后:"<<endl;
    s1.swap(s2);
    printSet(s1);
    printSet(s2);
}
​
int main() {
​
    test01();
    test02();
​
    return 0;
}

3.7.3set插入和删除

  • insert(elem); //在容器中插入元素

  • clear(); //清除所有元素

  • erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器

  • erase(beg, end); //删除区间[beg, end]的所有元素,返回下一个元素的迭代器

  • erase(elem); //删除容器中值为elem的元素

#include<iostream>
using namespace std;
#include<set>
​
void printSet(set<int>& s) {
    for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}
​
void test01() {
    set<int>s1;
​
    s1.insert(10);
    s1.insert(20);
    s1.insert(30);
    s1.insert(40);
    
    printSet(s1); //10 20 30 40 
    
    s1.erase(s1.begin());
    printSet(s1); //20 30 40 
    
    s1.erase(30);
    printSet(s1); //20 40 
    
    s1.erase(s1.begin(), s1.end());
    //s1.clear();
    printSet(s1);
}
​
int main() {
​
    test01();
​
    return 0;
}

3.7.4set查找和统计

  • find(key); //查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();

  • count(key); //统计key的元素个数

#include<iostream>
using namespace std;
#include<set>
​
void printSet(set<int>& s) {
    for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}
​
void test01() {
    set<int>s1;
​
    s1.insert(10);
    s1.insert(20);
    s1.insert(30);
    s1.insert(40);
    
    set<int>::iterator pos = s1.find(30);
    
    if(pos!=s1.end()){
        cout<<"找到元素:"<<*pos<<endl;
    }
    else{
        cout<<"未找到元素:"<<endl;
    }
}
​
void test02() {
    set<int>s1;
​
    s1.insert(10);
    s1.insert(20);
    s1.insert(30);
    s1.insert(40);
    
    int num=s1.count(30);
    cout<<num<<endl;
}
​
int main() {
​
    test01();
    test02();
​
    return 0;
}

3.7.5set和multiset区别

  • set不可以插入重复数据,而multiset可以

  • set插入数据的同时会返回插入结果,表示插入是否成功

  • multiset不会检测数据,因此可以插入重复数据

#include<iostream>
using namespace std;
#include<set>
​
void test01() {
    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>ms;
    ms.insert(10);
    ms.insert(10);
    for(multiset<int>::iterator it = ms.begin(); it!=ms.end();it++){
        cout<<*it<<" ";
    }
    cout<<endl;
}
​
int main() {
​
    test01();
​
    return 0;
}

3.7.6pair对组创建

  • pair<type, type> p (value1, value2);

  • pair<type, type> p = make_pair(value1, value2);

#include<iostream>
using namespace std;
#include<set>
​
void test01() {
    pair<string, int>p("Tom", 20);
    cout<<"姓名:"p.first<<"年龄:"<<p.second<<endl;
    
    pair<string, int>p2=make_pair("Jerry", 20);
    cout<<"姓名:"p2.first<<"年龄:"<<p2.second<<endl;
}
​
int main() {
​
    test01();
​
    return 0;
}

3.7.7set容器排序

  • set容器默认排序规则为从小到大,掌握如何改变排序规则

  • 利用仿函数,可以改变排序规则

示例一:set存放内置数据类型
#include<iostream>
using namespace std;
#include<set>
​
class MyCompare{
public:
    bool operator()(int v1, int v2){
        return v1>v2;
    }
};
​
void test01() {
    set<int>s1;
​
    s1.insert(10);
    s1.insert(20);
    s1.insert(30);
    s1.insert(40);
    
    for(set<int>::iterator it = s1.begin(); it!=s1.end();it++){
        cout<<*it<<" ";
    }
    cout<<endl;
    
    set<int, MyCompare>s2;
​
    s2.insert(10);
    s2.insert(20);
    s2.insert(30);
    s2.insert(40);
    
    for(set<int, MyCompare>::iterator it = s2.begin(); it!=s2.end();it++){
        cout<<*it<<" ";
    }
    cout<<endl;
}
​
int main() {
​
    test01();
​
    return 0;
}
示例二:set存放自定义数据类型
#include<iostream>
using namespace std;
#include<set>
#include<string>
​
class Person{
public:
    string Name;
    int Age;
    
    Person(string name, int age){
        this->Name=name;
        this->Age=age;
    }
};
​
class MyCompare{
public:
    bool operator()(const Person& p1, const Person& p2){
        return p1.Age>p2.Age;
    }
};
​
void test01() {
    set<Person, MyCompare>s;
    
    Person p1("刘备",23);
    Person p1("张飞",33);
    Person p1("关羽",26);
    Person p1("赵云",29);
​
    s.insert(p1);
    s.insert(p2);
    s.insert(p3);
    s.insert(p4);
    
    for(set<Person, MyCompare>::iterator it = s.begin(); it!=s.end();it++){
        cout<<"姓名:"<<it->Name<<"年龄:"<<it->Age<<endl;
    }
    cout<<endl;
}   
​
int main() {
​
    test01();
​
    return 0;
}
3.8map/multimap容器
  • map中所有元素都是pair

  • pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)

  • 所有元素都会根据元素的键值自动排序

本质:map/multimap属于关联式容器,底层结构是用二叉树实现的

优点:可以根据key值快速找到value值

map和multimap区别:

  • map不允许容器中有重复key值元素

  • multimap允许容器中有重复key值元素

3.8.1map构造和赋值

构造:

  • map<T1, T2> mp; //默认构造函数

  • map(const map &mp); //拷贝构造函数

赋值:

  • map& operator=(const map &mp); //重载等号操作符

#include<iostream>
using namespace std;
#include<map>
​
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;
}
​
void test01() {
    map<int, int>m;
​
    m.insert(pair<int, int>(1, 10));
    m.insert(pair<int, int>(2, 20));
    m.insert(pair<int, int>(3, 30));
    m.insert(pair<int, int>(4, 40));
    
    printMap(m);
    
    map<int, int>m2(m);
    printMap(m2);
    map<int, int>m3;
    m3=m2;
    printMap(m3);
}
​
int main() {
​
    test01();
​
    return 0;
}

3.8.2map大小和交换

  • size(); //返回容器中元素的数目

  • empty(); //判断容器是否为空

  • swap(st); //交换两个集合容器

#include<iostream>
using namespace std;
#include<map>
​
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;
}
​
void test01() {
    map<int, int>m;
​
    m.insert(pair<int, int>(1, 10));
    m.insert(pair<int, int>(2, 20));
    m.insert(pair<int, int>(3, 30));
    m.insert(pair<int, int>(4, 40));
    
    printMap(m);
    
    if(m.empty()){
        cout<<"m为空"<<endl;
    }
    else{
        cout<<"m不为空"<<endl;
    }
}
​
void test02() {
    map<int, int>m;
​
    m.insert(pair<int, int>(1, 10));
    m.insert(pair<int, int>(2, 20));
    m.insert(pair<int, int>(3, 30));
    m.insert(pair<int, int>(4, 40));
    printMap(m);
    
    map<int, int>m2;
​
    m2.insert(pair<int, int>(1, 100));
    m2.insert(pair<int, int>(2, 200));
    m2.insert(pair<int, int>(3, 300));
    m2.insert(pair<int, int>(4, 400));
    printMap(m2);
    
    cout<<"交换后:"<<endl;
    m.swap(m2);
    printMap(m);
    printMap(m2);
}
​
int main() {
​
    test01();
    test02();
​
    return 0;
}

3.8.3set插入和删除

  • insert(elem); //在容器中插入元素

  • clear(); //清除所有元素

  • erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器

  • erase(beg, end); //删除区间[beg, end]的所有元素,返回下一个元素的迭代器

  • erase(key); //删除容器中值为key的元素

#include<iostream>
using namespace std;
#include<map>
​
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;
}
​
void test01() {
    map<int, int>m;
​
    m.insert(pair<int, int>(1, 10));
    m.insert(make_pair(2, 20));
    m.insert(map<int, int>::value_type(3, 30));
    m[4]=40;
    
    printMap(m);
    
    m.erase(m.begin());
    printSet(m); 
    
    m.erase(3);
    printSet(m);
    
    m.erase(m.begin(), m.end());
    //s1.clear();
    printSet(m);
}
​
int main() {
​
    test01();
​
    return 0;
}

3.8.4set查找和统计

  • find(key); //查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();

  • count(key); //统计key的元素个数

#include<iostream>
using namespace std;
#include<map>
​
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;
}
​
void test01() {
    map<int, int>m;
​
    m.insert(pair<int, int>(1, 10));
    m.insert(pair<int, int>(2, 20));
    m.insert(pair<int, int>(3, 30));
    m.insert(pair<int, int>(4, 40));
    printMap(m);
    
    map<int, int>::iterator pos = m.find(3);
    
    if(pos!=m.end()){
        cout<<"找到元素:"<<*pos<<endl;
    }
    else{
        cout<<"未找到元素:"<<endl;
    }
}
​
void test02() {
    map<int, int>m;
​
    m.insert(pair<int, int>(1, 10));
    m.insert(pair<int, int>(2, 20));
    m.insert(pair<int, int>(3, 30));
    m.insert(pair<int, int>(4, 40));
    
    int num=m.count(3);
    cout<<num<<endl;
}
​
int main() {
​
    test01();
    test02();
​
    return 0;
}

3.8.5map容器排序

  • map容器默认排序规则为从小到大,掌握如何改变排序规则

  • 利用仿函数,可以改变排序规则

#include<iostream>
using namespace std;
#include<set>
​
class MyCompare{
public:
    bool operator()(int v1, int v2){
        return v1>v2;
    }
};
​
void test01() {
    map<int, int, MyCompare>m;
​
    m.insert(pair<int, int>(1, 10));
    m.insert(pair<int, int>(2, 20));
    m.insert(pair<int, int>(3, 30));
    m.insert(pair<int, int>(4, 40));
    printMap(m);
    
    for(map<int, int>::iterator it = m.begin(); it!=m.end();it++){
        cout <<"key="<< (*it).first << "value="<<it->second<<endl;
    }
    cout<<endl;
    
}
​
int main() {
​
    test01();
​
    return 0;
}
案例—员工分组
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<map>
#include<ctime>
​
#define CHEHUA 0
#define MEISHU 1
#define YANFA 2
​
class Worker {
public:
    string Name;
    int Salary;
​
};
​
void createWorker(vector<Worker>& v) {
    string nameSeed = "ABCDEFGHIJ";
    for (int i = 0; i < 10; i++) {
        Worker worker;
        worker.Name = "员工";
        worker.Name += nameSeed[i];
​
        worker.Salary = rand() % 10000 + 10000;
​
        v.push_back(worker);
    }
}
//员工分组
void setGroup(vector<Worker>& v, multimap<int, Worker>& m) {
    for (vector<Worker>::iterator it = v.begin(); it != v.end(); it++) {
        int deptId = rand() % 3;
​
        m.insert(make_pair(deptId, *it));
    }
}
​
void showWorkerByGroup(multimap<int, Worker>&m) {
    cout << "策划部门:" << endl;
    multimap<int, Worker>::iterator pos = m.find(CHEHUA);
    int count = m.count(CHEHUA);
    int index = 0;
    for (; pos != m.end() && index<count ; pos++, index++) {
        cout << "姓名:" << pos->second.Name << "工资:" << pos->second.Salary << endl;
    }
​
    cout << "美术部门:" << endl;
    pos = m.find(MEISHU);
    count = m.count(MEISHU);
    index = 0;
    for (; pos != m.end() && index < count; pos++, index++) {
        cout << "姓名:" << pos->second.Name << "工资:" << pos->second.Salary << endl;
    }
​
    cout << "研发部门:" << endl;
    pos = m.find(YANFA);
    count = m.count(YANFA);
    index = 0;
    for (; pos != m.end() && index < count; pos++, index++) {
        cout << "姓名:" << pos->second.Name << "工资:" << pos->second.Salary << endl;
    }
}
​
int main() {
    srand((unsigned int)time(NULL));
​
    vector<Worker>vWorker;
    createWorker(vWorker);
​
    multimap<int, Worker>mWorker;
    setGroup(vWorker, mWorker);
​
    showWorkerByGroup(mWorker);
​
    return 0;
}

4.STL-函数对象

4.1函数对象

概念:

  • 重载函数调用操作符的类,其对象常称为函数对象

  • 函数对象使用重载的()时,行为类似函数调用,也叫仿函数

本质:函数对象(仿函数)是一个类,不是一个函数

函数对象的使用:

  • 函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值

  • 函数对象超出普通函数的概念,函数对象可以有自己的状态

  • 函数对象可以作为参数传递

#include<iostream>
using namespace std;
#include<string>
​
//函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
class MyAdd {
public:
    int operator()(int v1, int v2) {
        return v1 + v2;
    }
};
​
void test01() {
    MyAdd myAdd;
    cout << myAdd(10, 10) << endl;
}
​
//函数对象超出普通函数的概念,函数对象可以有自己的状态
class MyPrint {
public:
    int count;
​
    MyPrint() {
        this->count=0;
    }
​
    void operator()(string test) {
        cout << test << endl;
        this->count++;
    }
    
};
​
void test02() {
    MyPrint myPrint;
    myPrint("hello");
    myPrint("hello");
​
    cout << "调用次数:" << myPrint.count << endl;
}
​
//函数对象可以作为参数传递
void doPrint(MyPrint& mp, string test) {
    mp(test);
}
​
void test03() {
    MyPrint myPrint;
    doPrint(myPrint, "hello C++");
}
​
int main() {
​
    /*test01();
    test02();*/
    test03();
​
    return 0;
}
4.2谓词
  • 返回bool类型的仿函数称为谓词

  • 如果operator()接受一个参数,那么叫做一元谓词

  • 如果operator()接受两个参数,那么叫做二元谓词

示例:一元谓词
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
​
class GreaterFive {
public:
    bool operator()(int val) {
        return val>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(), GreaterFive());
    if (it == v.end()) {
        cout << "未找到" << endl;
    }
    else {
        cout << "找到了" << endl;
    }
}
​
int main() {
​
    test01();
​
    return 0;
}
示例:二元谓词
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
​
class MyCompare {
public:
    bool operator()(int val1, int val2) {
        return val1>val2;
    }
};
​
void test01() {
    vector<int>v;
    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);
    v.push_back(50);
    
    sort(v.begin(), v.end());
    for(vector<int>::iterator it = v.begin(); it!=v.end();it++){
        cout<<*it<<" ";
    }
    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();
​
    return 0;
}
4.3内建函数对象

STL内建了一些函数对象

分类:

  • 算术仿函数

  • 关系仿函数

  • 逻辑仿函数

用法:

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

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

4.3.1算术仿函数

  • 实现四则运算

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

  • template<class T> T plus<T> //加法仿函数

  • template<class T> T minus<T> //减法仿函数

  • template<class T> T multiplies<T> //乘法仿函数

  • template<class T> T divides<T> //除法仿函数

  • template<class T> T modulus<T> //取模仿函数

  • template<class T> T negate<T> //取反仿函数

#include<iostream>
using namespace std;
#include<functional>
​
void test01() {
    negate<int>n;
​
    cout<<n(50)<<endl;
​
}
​
void test02() {
    plus<int>p;
​
    cout << p(10, 20) << endl;
}
​
int main() {
​
    test01();
    test02();
​
    return 0;
}

4.3.2关系仿函数

  • template<class T> bool equal_to<T> //等于

  • template<class T> bool not_equal_to<T> //不等于

  • template<class T> bool greater<T> //大于

  • template<class T> bool greater_equal<T> //大于等于

  • template<class T> bool less<T> //小于

  • template<class T> bool less_equal<T> //小于等于

#include<iostream>
using namespace std;
#include<vector>
#include<functional>
#include<algorithm>
​
class MyCompare {
public:
    bool operator()(int val1, int val2) {
        return val1>val2;
    }
};
​
void test01() {
    vector<int>v;
    
    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);
    v.push_back(50);
    
    for(vector<int>::iterator it = v.begin(); it!=v.end();it++){
        cout<<*it<<" ";
    }
    cout<<endl;
    
    //sort(v.begin(), v.end(), MyCompare());
    sort(v.begin(), v.end(), greater<int>());
    for(vector<int>::iterator it = v.begin(); it!=v.end();it++){
        cout<<*it<<" ";
    }
    cout<<endl;
​
}
​
int main() {
​
    test01();
​
    return 0;
}

4.3.3逻辑仿函数

  • template<class T> bool logical_and<T> //与

  • template<class T> bool logical_or<T> //或

  • template<class T> bool logical_not<T> //非

#include<iostream>
using namespace std;
#include<vector>
#include<functional>
#include<algorithm>
​
void test01() {
    vector<bool>v;
    
    v.push_back(true);
    v.push_back(false);
    v.push_back(true);
    v.push_back(false);
    
    for(vector<bool>::iterator it = v.begin(); it!=v.end();it++){
        cout<<*it<<" ";
    }
    cout<<endl;
    
    vector<bool>v2;
    v2.resize(v.size());
    
    transform(v.begin(), v.end(), v2.begin(), logical_not<bool>);
    for(vector<bool>::iterator it = v2.begin(); it!=v2.end();it++){
        cout<<*it<<" ";
    }
    cout<<endl;
​
}
​
int main() {
​
    test01();
​
    return 0;
}

5.STL-常用算法

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

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

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

  • <numeric>体积很小,只包括几个在序列上面进行简单数学运算的模板函数

5.1常用遍历算法
  • for_each(iterator beg, iterator end, _func); //遍历容器

  • transform(iterator beg1, iterator end1, iterator beg2, _func); //搬运容器到另一个容器中

#include<iostream>
using namespace std;
#include<vector>
#include<functional>
#include<algorithm>
​
//普通函数
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;
}
​
int main() {
​
    test01();
​
    return 0;
}
#include<iostream>
using namespace std;
#include<vector>
#include<functional>
#include<algorithm>
​
//仿函数
class Transform{
public:
    int operator()(int val){
        return 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);
    }
    
    vector<int>vTarget; //目标容器
    vTarget.resize(v.size()); //目标容器需要提前开辟空间
    
    transform(v.begin(), v.end(), vTarget.begin(), Transform());
    cout<<endl;
    
    for_each(vTarget.begin(), vTarget.end(), print02());
    cout<<endl;
}
​
int main() {
​
    test01();
​
    return 0;
}
5.2常用查找算法
  • find(iterator beg, iterator end, value); //查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器end()

#include<iostream>
using namespace std;
#include<vector>
#include<functional>
#include<algorithm>
#include<string>
​
//查找——内置数据类型
void test01() {
    vector<int>v;
    
    for(int i=0;i<10;i++){
        v.push_back(i);
    }
    
    vector<int>::iterator it = find(v.begin(), v.end(), 5);
    if(it == v.end()){
        cout<<"没有找到"<<endl;
    }
    else{
        cout<<"找到"<<*it<<endl;
    }
    
}
​
class Person{
public:
    string Name;
    int Age;
    
    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;
        }
        else{
            return false;
        }
    }
};
​
//查找——自定义数据类型
void test02() {
    vector<Person>v;
    
    Person p1("a",1);
    Person p2("b",2);
    Person p3("c",3);
    Person p4("d",4);
    
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    
    vector<Person>::iterator it = find(v.begin(), v.end(), p2);
    if(it == v.end()){
        cout<<"没有找到"<<endl;
    }
    else{
        cout<<"找到"<<"姓名:"<<it->Name<<"年龄:"<<it->Age<<endl;
    }
}
​
int main() {
​
    test01();
    test02();
​
    return 0;
}
  • find_if(iterator beg, iterator end, _Pred); //按条件查找元素

#include<iostream>
using namespace std;
#include<vector>
#include<functional>
#include<algorithm>
#include<string>
​
class GreaterFive{
public:
    bool operator()(int val){
        return val>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(), GreaterFive());
    if(it == v.end()){
        cout<<"没有找到"<<endl;
    }
    else{
        cout<<"找到"<<*it<<endl;
    }
    
}
​
class Person{
public:
    string Name;
    int Age;
    
    Person(string name, int age){
        this->Name=name;
        this->Age=age;
    }
    
};
​
class Greater20{
public:
    bool operator()(Person &p){
        return p.Age>20;
    }
};
​
//查找——自定义数据类型
void test02() {
    vector<Person>v;
    
    Person p1("a",1);
    Person p2("b",2);
    Person p3("c",3);
    Person p4("d",4);
    
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    
    vector<Person>::iterator it = find—_fi(v.begin(), v.end(), Greater20);
    if(it == v.end()){
        cout<<"没有找到"<<endl;
    }
    else{
        cout<<"找到"<<"姓名:"<<it->Name<<"年龄:"<<it->Age<<endl;
    }
}
​
int main() {
​
    test01();
    test02();
​
    return 0;
}
  • adjacent_find(iterator beg, iterator end); //查找相邻重复元素

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
​
void test01() {
    vector<int>v;
    
    v.push_back(1);
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(2);
    v.push_back(4);
    
    vector<int>::iterator pos = adjacent_find(v.begin(), v.end());
    if(pos == v.end()){
        cout<<"没有找到"<<endl;
    }
    else{
        cout<<"找到相邻重复元素:"<<*pos<<endl;
    }
    
}
​
int main() {
​
    test01();
​
    return 0;
​
  • binary_search(iterator beg, iterator end, value); //二分查找法指定元素是否存在

        注意:容器必须是有序序列

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
​
void test01() {
    vector<int>v;
    
    for(int i=0;i<10;i++){
        v.push_back(i);
    }
    
    bool ret = binary_search(v.begin(), v.end(), 9);
    if(ret){
        cout<<"找到:"<<*pos<<endl;
    }
    else{
        cout<<"没有找到"<<endl;
    }
    
}
​
int main() {
​
    test01();
​
    return 0;
​
  • count(iterator beg, iterator end, value); //统计元素个数

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
​
//统计——内置数据类型
void test01() {
    vector<int>v;
    
    v.push_back(1);
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(2);
    v.push_back(4);
    
    int num = count(v.begin(), v.end(), 1);
    cout<<"个数为:"<<num<<endl;
    
}
​
class Person{
public:
    string Name;
    int Age;
    
    Person(string name, int age){
        this->Name=name;
        this->Age=age;
    }
    
    bool operator==(const Person & p){
        if(this->Age=p.Age){
            return true;
        }
        else{
            return false;
        }
    }
    
};
​
//统计——自定义数据类型
void test02() {
    vector<Person>v;
    
    Person p1("a",1);
    Person p2("b",2);
    Person p3("c",2);
    Person p4("d",4);
    
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    
    int num = count(v.begin(), v.end(), p);
    cout<<"个数为:"<<num<<endl;
}
​
int main() {
​
    test01();
    test02();
​
    return 0;
}
  • count_ifiterator beg, iterator end, _Pred); //按条件统计元素个数

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
​
class Greater2{
public:
    bool operator()(int val){
        return val>2;
    }
};
​
//统计——内置数据类型
void test01() {
    vector<int>v;
    
    v.push_back(1);
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(2);
    v.push_back(4);
    
    int num = count(v.begin(), v.end(), Greater20());
    cout<<"个数为:"<<num<<endl;
    
}
​
class Person{
public:
    string Name;
    int Age;
    
    Person(string name, int age){
        this->Name=name;
        this->Age=age;
    }
    
};
​
class AgeGreater2{
public:
    bool operator()(const Person &p){
        return p.Age>20;
    }
};
​
//统计——自定义数据类型
void test02() {
    vector<Person>v;
    
    Person p1("a",1);
    Person p2("b",2);
    Person p3("c",2);
    Person p4("d",4);
    
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    
    int num = count(v.begin(), v.end(), AgeGreater2());
    cout<<"个数为:"<<num<<endl;
}
​
int main() {
​
    test01();
    test02();
​
    return 0;
}
5.3常用排序算法
  • sort(iterator beg, iterator end, _Pred); //对容器内元素进行排序

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<functional>
​
void MyPrint(int val){
    cout<<val<<" ";
}
​
void test01() {
    vector<int>v;
    
    v.push_back(1);
    v.push_back(3);
    v.push_back(2);
    v.push_back(5);
    v.push_back(0);
    
    //升序
    sort(v.begin(), v.end());
    for_each(v.begin(), v.end(), MyPrint);
    cout<<endl;
    
    //降序
    sort(v.begin(), v.end());
    for_each(v.begin(), v.end(), greater<int>());
    cout<<endl;
}
​
int main() {
​
    test01();
​
    return 0;
}
  • random_shuffle(iterator beg, iterator end); //洗牌,指定范围内的元素随机调整次序

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<functional>
#include<ctime>
​
void MyPrint(int val){
    cout<<val<<" ";
}
​
void test01() {
    srand((unsigned int)time(NULL));
    
    vector<int>v;
    
    for(int i=0;i<10;i++){
        v.push_back(i);
    }
    
    random_shuffle(v.begin(), v.end());
    for_each(v.begin(), v.end(), MyPrint);
    cout<<endl;
}
​
int main() {
​
    test01();
​
    return 0;
}
  • merge(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest); //容器元素合并,并存储到另一容器中

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<functional>
​
void MyPrint(int val){
    cout<<val<<" ";
}
​
void test01() {
    
    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(), MyPrint);
    cout<<endl;
}
​
int main() {
​
    test01();
​
    return 0;
}
  • reverse(iterator beg, iterator end); //反转指定范围的元素

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<functional>
​
void MyPrint(int val){
    cout<<val<<" ";
}
​
void test01() {
    
    vector<int>v1;
    
    for(int i=0;i<10;i++){
        v1.push_back(i);
    }
    
    for_each(v1.begin(), v1.end(), MyPrint);
    cout<<endl;
    
    reverse(v1.begin(), v1.end());
    
    for_each(v1.begin(), v1.end(), MyPrint);
    cout<<endl;
}
​
int main() {
​
    test01();
​
    return 0;
}
5.4常用拷贝和替换算法
  • copy(iterator beg, iterator end, iterator dest); //容器内指定范围的元素拷贝到另一容器中

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<functional>
​
void MyPrint(int val){
    cout<<val<<" ";
}
​
void test01() {
    
    vector<int>v1;
    vector<int>v2;
    
    for(int i=0;i<10;i++){
        v1.push_back(i);
    }
    
    v2.resize(v1.size());
    copy(v1.begin(), v1.end(), v2.begin());
    
    for_each(v1.begin(), v1.end(), MyPrint);
    cout<<endl;
    
}
​
int main() {
​
    test01();
​
    return 0;
}
  • replace(iterator beg, iterator end, oldvalue, newvalue); //将容器内指定范围的旧元素改为新元素

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<functional>
​
void MyPrint(int val){
    cout<<val<<" ";
}
​
void test01() {
    
    vector<int>v1;
    
    for(int i=0;i<10;i++){
        v1.push_back(i);
    }
    
    replace(v1.begin(), v1.end(), 5, 50);
    
    for_each(v1.begin(), v1.end(), MyPrint);
    cout<<endl;
    
}
​
int main() {
​
    test01();
​
    return 0;
}
  • replace_if(iterator beg, iterator end, _func, newvalue); //容器内指定范围满足条件的元素替换为新元素

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<functional>
​
void MyPrint(int val){
    cout<<val<<" ";
}
​
class Greater3{
public:
    bool operator()(int val){
        return val>3;
    }
};
​
void test01() {
    
    vector<int>v1;
    
    for(int i=0;i<10;i++){
        v1.push_back(i);
    }
    
    for_each(v1.begin(), v1.end(), MyPrint);
    cout<<endl;
    
    replace_if(v1.begin(), v1.end(), Greater3(), 50);
    
    for_each(v1.begin(), v1.end(), MyPrint);
    cout<<endl;
    
}
​
int main() {
​
    test01();
​
    return 0;
}
  • swap(container c1, container c2); //互换两个容器的元素

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<functional>
​
void MyPrint(int val){
    cout<<val<<" ";
}
​
class Greater3{
public:
    bool operator()(int val){
        return val>3;
    }
};
​
void test01() {
    
    vector<int>v1;
    vector<int>v2;
    
    for(int i=0;i<10;i++){
        v1.push_back(i);
        v2.push_back(i+100);
    }
    
    for_each(v1.begin(), v1.end(), MyPrint);
    cout<<endl;
    for_each(v2.begin(), v2.end(), MyPrint);
    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();
​
    return 0;
}
5.5常用算术生成算法

包含的头文件#include <numeric>

  • accumulate(iterator beg, iterator end, value); //计算容器元素累计总和,value起始值

#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
​
void test01() {
    
    vector<int>v1;
    
    for(int i=0;i<=100;i++){
        v1.push_back(i);
    }
    int total = accumulate(v1.begin(), v1.end(), 0);
    
    cout<<total<<endl;
}
​
int main() {
​
    test01();
​
    return 0;
}
  • fill(iterator beg, iterator end, value); //向容器中添加元素

#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
#include<algorithm>
​
void MyPrint(int val){
    cout<<val<<" ";
}
​
void test01() {
    
    vector<int>v1;
    v1.resize(10);
    
    fill(v1.begin(), v1.end(), 100);
    
    for_each(v.begin(), v.end(), MyPrint);
    cout<<endl;
}
​
int main() {
​
    test01();
​
    return 0;
}
5.6常用集合算法
  • set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest); //求两个容器的交集

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
​
void MyPrint(int val){
    cout<<val<<" ";
}
​
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 itEnd = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
    
    for_each(vTarget.begin(), itEnd, MyPrint);
    cout<<endl;
    
}
​
int main() {
​
    test01();
​
    return 0;
}
  • set_union(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest); //求两个容器的并集

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
​
void MyPrint(int val){
    cout<<val<<" ";
}
​
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 itEnd = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
    
    for_each(vTarget.begin(), itEnd, MyPrint);
    cout<<endl;
    
}
​
int main() {
​
    test01();
​
    return 0;
}
  • set_difference(iterator beg, iterator end, value); //求两个容器的差集

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
​
void MyPrint(int val){
    cout<<val<<" ";
}
​
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(max(v1.size(), v2.size());
    
    cout<<"v1和v2的差集:"<<endl;
    vector<int>::iterator itEnd = set_difference( v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
    
    for_each(vTarget.begin(), itEnd, MyPrint);
    cout<<endl;
    
    cout<<"v2和v1的差集:"<<endl;
    vector<int>::iterator itEnd = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), vTarget.begin());
    
    for_each(vTarget.begin(), itEnd, MyPrint);
    cout<<endl;
    
}
​
int main() {
​
    test01();
​
    return 0;
}

实列:演讲比赛流程管理系统

speechManager.h

#pragma once
#include<iostream>
using namespace std;
#include<vector>
#include<map>
#include "Speaker.h"
#include<algorithm>
#include<deque>
#include<functional>
#include<numeric>
#include<string>
#include<fstream>
​
class SpeechManager {
public:
    SpeechManager();
​
    //菜单功能
    void show_Menu();
​
    //退出系统
    void exitSystem();
​
    ~SpeechManager();
​
    //初始化容器和属性
    void initSpeech();
​
    //创建12名选手
    void createSpeaker();
​
    //开始比赛
    void startSpeech();
​
    //抽签
    void speechDraw();
​
    //比赛
    void speechContest();
​
    //显示成绩
    void showScore();
​
    //保存记录
    void saveRecord();
​
    //查看记录
    void loadRecord();
​
    //判断文件是否为空
    bool fileIsEmpty;
​
    //存放往届记录的容器
    map<int, vector<string>>Record;
​
    //显示往届记录
    void showRecord();
​
    //清空记录
    void clearRecord();
​
    //成员属性
    //保存第一轮比赛选手编号容器
    vector<int>v1;
​
    //第一轮晋级选手编号容器
    vector<int>v2;
    
    //胜出前三名选手编号容器
    vector<int>vVictory;
​
    //存放编号以及对应具体选手容器
    map<int, Speaker>Sperker;
​
    //存放比赛轮数
    int Index;
​
​
};

speechManager.cpp

#include "speechManager.h"
​
SpeechManager::SpeechManager() {
    this->initSpeech();
​
    this->createSpeaker();
​
    this->loadRecord();
}
​
//菜单功能
void SpeechManager::show_Menu() {
    cout << "******************************************************************" << endl;
    cout << "************************欢迎参加演讲比赛**************************" << endl;
    cout << "************************1.开始演讲比赛****************************" << endl;
    cout << "************************2.查看往届记录****************************" << endl;
    cout << "************************3.清空比赛记录****************************" << endl;
    cout << "************************0.退出比赛程序****************************" << endl;
    cout << "******************************************************************" << endl;
    cout << endl;
}
​
//退出系统
void SpeechManager::exitSystem() {
    cout << "欢迎下次使用" << endl;
    system("pause");
    exit(0);
​
}
​
//初始化容器和属性
void SpeechManager::initSpeech() {
    this->v1.clear();
    this->v2.clear();
    this->vVictory.clear();
    this->Sperker.clear();
​
    this->Index = 1;
​
    this->Record.clear();
}
​
//创建12名选手
void SpeechManager::createSpeaker() {
    string nameSeed = "ABCDEFGHIJKL";
    for (int i = 0; i < nameSeed.size(); i++) {
        string name = "选择";
        name += nameSeed[i];
​
        Speaker sp;
        sp.Name = name;
​
        for (int j = 0; j < 2; j++) {
            sp.Score[j] = 0;
        }
​
        this->v1.push_back(i + 10001);
​
        this->Sperker.insert(make_pair(i + 10001, sp));
    }
}
​
//开始比赛
void SpeechManager::startSpeech() {
    //第一轮
    //1.抽签
    this->speechDraw();
    //2.比赛
    this->speechContest();
    //3.晋级结果
    this->showScore();
​
    //第二轮
    this->Index++;
    //1.抽签
    this->speechDraw();
    //2.比赛
    this->speechContest();
    //3.最终结果
    this->showScore();
    //4.保存分数到文件中
    this->saveRecord();
​
    //重置比赛,获取记录
    this->initSpeech();
    this->createSpeaker();
    this->loadRecord();
​
    cout << "本届比赛完毕" << endl;
    system("pause");
    system("cls");
​
}
​
//抽签
void SpeechManager::speechDraw() {
    cout << "第" << this->Index << "轮比赛选手正在抽签" << endl;
    cout << "__________________________" << endl;
    cout << "抽签后演讲顺序如下:" << endl;
​
    if (this->Index == 1) {
        random_shuffle(v1.begin(), v1.end());
        for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++) {
            cout << *it << " ";
        }
        cout << endl;
    }
    else {
        random_shuffle(v2.begin(), v2.end());
        for (vector<int>::iterator it = v2.begin(); it != v2.end(); it++) {
            cout << *it << " ";
        }
        cout << endl;
    }
    cout << "__________________________" << endl;
    system("pause");
    cout << endl;
}
​
//比赛
void SpeechManager::speechContest() {
    cout << "第" << this->Index << "轮比赛正式开始" << endl;
​
    multimap<double, int, greater<double>> groupScore;
​
    int num = 0;
​
    vector<int>v_Src;
    if (this->Index == 1) {
        v_Src = v1;
    }
    else {
        v_Src = v2;
    }
​
    for (vector<int>::iterator it = v_Src.begin(); it != v_Src.end(); it++) {
        num++;
        deque<double>d;
        for (int i = 0; i < 10; i++) {
            double score = rand() % 401 + 600 / 10.f;
            //cout << score << " ";
            d.push_back(score);
        }
​
        sort(d.begin(), d.end(), greater<double>());
        d.pop_front();
        d.pop_back();
​
        double sum = accumulate(d.begin(), d.end(), 0);
        double avg = sum / (double)d.size();
​
        this->Sperker[*it].Score[this->Index - 1] = avg;
​
        groupScore.insert(make_pair(avg, *it));
​
        if (num % 6 == 0) {
            cout << "第" << num / 6 << "小组比赛名次:" << endl;
            for (multimap<double, int, greater<double>>::iterator it = groupScore.begin(); it != groupScore.end(); it++) {
                cout << "编号:" << it->second << "选手:" << this->Sperker[it->second].Name << "平均分:" << this->Sperker[it->second].Score[this->Index-1] << endl;
            }
​
            int count = 0;
            for (multimap<double, int, greater<double>>::iterator it = groupScore.begin(); it != groupScore.end()&&count<3; it++,count++) {
                if (this->Index == 1) {
                    v2.push_back((*it).second);
                }
                else {
                    vVictory.push_back((*it).second);
                }
            }
​
            groupScore.clear();
        }
    }
    cout << "第" << this->Index  << "轮比赛完毕" << endl;
    system("pause");
}
​
//显示成绩
void SpeechManager::showScore() {
    cout << "第" << this->Index << "轮晋级选手如下:" << endl;
​
    vector<int>v;
    if (this->Index == 1) {
        v = v2;
    }
    else {
        v = vVictory;
    }
​
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
        cout << "编号:" << *it << "选手:" << this->Sperker[*it].Name << "平均分:" << this->Sperker[*it].Score[this->Index - 1] << endl;
    }
    cout << endl;
​
    system("pause");
    system("cls");
    this->show_Menu();
​
}
​
//保存记录
void SpeechManager::saveRecord() {
    ofstream ofs;
    ofs.open("speech.csv", ios::out | ios::app);
​
    for (vector<int>::iterator it = vVictory.begin(); it != vVictory.end(); it++) {
        ofs << *it << "," << this->Sperker[*it].Score[1] << ",";
    }
    ofs << endl;
​
    ofs.close();
    cout << "记录完毕" << endl;
​
    this->fileIsEmpty = false;
}
​
//查看记录
void SpeechManager::loadRecord() {
    ifstream ifs("speech.csv", ios::in);
    if (!ifs.is_open()) {
        this->fileIsEmpty = true;
        ifs.close();
        return;
    }
    char ch;
    ifs >> ch;
    if (ifs.eof()) {
        //cout << "文件为空" << endl;
        this->fileIsEmpty = true;
        ifs.close();
        return;
    }
​
    this->fileIsEmpty = false;
​
    ifs.putback(ch);
​
    string data;
    int index = 0;
​
    while (ifs >> data) {
        vector<string>v;
​
        int pos = -1;
        int start = 0;
​
        while (true) {
            pos = data.find(",", start);
            if (pos == -1) {
                break;
            }
            string temp = data.substr(start, pos - start);
            v.push_back(temp);
            start = pos + 1;
        }
        this->Record.insert(make_pair(index, v));
        index++;
    }
​
    ifs.close();
​
}
​
//显示往届记录
void SpeechManager::showRecord() {
    if (this->fileIsEmpty) {
        cout << "文件为空或者文件不存在" << endl;
    }
    else {
        for (int i = 0; i < this->Record.size(); i++) {
            cout << "第" << i + 1 << "届"
                << "冠军编号:" << this->Record[i][0] << "分数:" << this->Record[i][1] << " "
                << "亚军编号:" << this->Record[i][2] << "分数:" << this->Record[i][3] << " "
                << "季军编号:" << this->Record[i][4] << "分数:" << this->Record[i][5] << endl;
        }
    }
    
    system("pause");
    system("cls");
}
​
//清空记录
void SpeechManager::clearRecord() {
    cout << "是否确定清空文件?(1.是 2.否)" << endl;
​
    int select = 0;
    cin >> select;
​
    if (select == 1) {
        ofstream ofs("speech.csv", ios::trunc);
        ofs.close();
​
        this->initSpeech();
        this->createSpeaker();
        this->loadRecord();
​
        cout << "清空成功" << endl;
    }
    system("pause");
    system("cls");
}
​
SpeechManager::~SpeechManager() {
​
}

演讲比赛流程管理系统.cpp

#include<iostream>
using namespace std;
#include "speechManager.h"
#include<ctime>
​
​
​
int main() {
​
    srand((unsigned int)time(NULL));
​
    SpeechManager sm;
​
    int choice = 0;
​
    while (true) {
        sm.show_Menu();
        cout << "请输入您的选择:" << endl;
        cin >> choice;
​
        switch (choice) {
        case 1:
            sm.startSpeech();
            break;
        case 2:
            sm.showRecord();
            break;
        case 3:
            sm.clearRecord();
            break;
        case 0:
            sm.exitSystem();
            break;
        default:
            system("cls");
            break;
        }
    }
​
    return 0;
}

课程链接:01 演讲比赛流程管理系统-比赛需求分析以及成品展示_哔哩哔哩_bilibili 

  • 26
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值