C++学习笔记(11):STL中常用容器


本博文是学习黑马程序员C++视频时做的笔记,记录一下只是方便温故知新,不做其他用途。

一、string容器

1.1 string基本概念

1、本质
string是C++风格的字符串,而String本质是一个类

2、string与char*区别
(1)char* 是一个指针;
(2) string是一个类,内部封装了char*,管理这个字符串,是一个char*型的容器。

3、特点:
string内部封装了很多方法
例如:查找find,拷贝copy,删除delete,替换replace、插入insert;
string管理char*所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责。

1.2 string构造函数

构造函数原型:

  • string(); //创建一个空的字符串 例如string str;
  • string(const char* s); //使用字符串s初始化;
  • string(const string& str); //使用一个string对象初始化另一个string对象;
  • string(int n,char c); //使用n个字符c初始化;

示例:

#include <iostream>
using namespace std;
#include <string>

//string构造
void test01()
{
    //string();//创建一个空的字符串 例如string str;
    string s1; //创建空字符串,调用无参构造函数
    cout << "str1 = " << s1 << endl;

    //string(const char* s);//使用字符串s初始化;
    const char* str = "hello world";
    string s2(str); //把c_string转换成了string
    cout << "str2 = " << s2 << endl;

    //string(const string& str)//使用一个string对象初始化另一个string对象;
    string s3(s2); //调用拷贝构造函数
    cout << "str3 = " << s3 << endl;

    //string(int n,char c);//使用n个字符c初始化;
    string s4(10, 'c');
    cout << "str4 = " << s4 << endl;
}

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

在这里插入图片描述

1.3 string赋值操作

功能描述:给string字符串进行赋值。
函数原型

  • string& operator=(const char* s); //char*类型字符串 赋值给当前的字符串
  • string& operator=(const string &s); //把字符串s赋给当前的字符串
  • string& operator=(char 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>

//string 的赋值操作
void test01()
{
//    string& operator=(const char* s); //char*类型字符串 赋值给当前的字符串
    string str1;
    str1 = "hello world!";
    cout<<"str1="<<str1<<endl;

//    string& operator=(const string &s); //把字符串s赋给当前的字符串
    string str2;
    str2 = str1;
    cout<<"str2="<<str2<<endl;

//    string& operator=(char c); //字符赋值给当前的字符串
    string str3;
    str3 = 'a';
    cout << "str3 = " << str3 << endl;

//    string& assign(const char *s); //把字符串s赋给当前的字符串
    string str4;
    str4.assign("hello c++");
    cout << "str4 = " << str4 << endl;

//    string& assign(const char *s, int n); //把字符串s的前n个字符赋给当前的字符串
    string str5;
    str5.assign("hello c++",5);
    cout << "str5 = " << str5 << endl;

//    string& assign(const string &s); //把字符串s赋给当前字符串
    string str6;
    str6.assign(str5);
    cout << "str6 = " << str6 << endl;

//    string& assign(int n, char c); //用n个字符c赋给当前字符串
    string str7;
    str7.assign(5, 'c');
    cout << "str7 = " << str7 << endl;
}

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

在这里插入图片描述

1.4 string字符串拼接

功能描述
实现字符串末尾拼接字符串。
函数原型

  • string& operator+=(const char* str); //重载+=操作符
  • string& operator+=(const char c); //重载+=操作符
  • string& operator+=(const char* 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 = 我爱玩游戏

    str1 += ':';
    cout << "str1 = " << str1 << endl;//str1 = 我爱玩游戏:

    string str2 = "LOL DNF";
    str1 += str2;
    cout << "str1 = " << str1 << endl;//str1 = 我爱玩游戏:LOL DNF

//    使用append方式
    string str3 = "I";
    str3.append(" love ");
    str3.append("game abcde", 4);
    cout << "str3 = " << str3 << endl;//str3 = I love game

    str3.append(str2, 4, 3); // 从下标4位置开始 ,将str2中“LOL DNF”截取3个字符,拼接到字符串末尾
    cout << "str3 = " << str3 << endl;//str3 = I love gameDNF
}

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

在这里插入图片描述

1.5 string查找和替换

功能描述
1、查找:查找指定字符串是否存在;
2、替换:在指定位置替换字符串。
函数原型

  • 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()
{
    //1、查找
//    find() 从左往右查找
    string str1 = "abcdefgde";//其中a是0号位
//    int pos = str1.find("a");//pos = 0
    int pos = str1.find("de");
    if (pos == -1)
    {
        cout << "未找到" << endl;
    }
    else
    {
        cout << "pos = " << pos << endl;
    }

//    rfind() 从右往左查找
    pos = str1.rfind("de");
    cout << "pos = " << pos << endl;

}

void test02()
{
    //2、替换
    string str1 = "abcdefgde";
    str1.replace(1, 3, "1111");//从1号位置起3个字符,替换为“1111”
    cout << "str1 = " << str1 << endl;
}

int main()
{
    test01();
    test02();
    return 0;
}

在这里插入图片描述

1.6 string字符串比较

功能描述
字符串之间的比较。
比较方式
字符串之间的ASCLL码比较。

 = 返回 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 = "xello";
    string str2 = "hello";
    //关键:使用compare比较两个字符串的ASCLL码
    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;
}

在这里插入图片描述

1.7 string字符串存取

string中单个字符串存取方式有两种:

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

示例:

#include <iostream>
using namespace std;
#include <string>

//字符串存取
void test01()
{
    string str = "hello";

    for (int i = 0; i < str.size(); i++)
    {
        cout << str[i] << " ";//关键1:通过[]访问单个字符
    }
    cout << endl;

    for (int i = 0; i < str.size(); i++)
    {
        cout << str.at(i) << " ";//关键2:通过at方式访问单个字符
    }
    cout << endl;

    //修改单个字符
    str[0] = 'x';
    cout << str << endl;//xello
    str.at(1) = 'x';
    cout << str << endl;//xxllo
}
int main()
{
    test01();
    return 0;
}

在这里插入图片描述

1.8 string插入和删除

功能描述:
对string字符串进行插入和删除字符操作
函数原型

  • string& insert(int pos, const char* s);//插入字符串
  • string& insert(int pos, const string& str); //插入字符串
  • string& insert(int pos, int n, char c);//在指定位置插入n个字符c
  • string& erase(int pos, int n = npos); //删除从Pos开始的n个字符

示例:

#include <iostream>
using namespace std;
#include <string>

//字符串 插入和删除
void test01()
{
    string str = "hello";
//    1、插入 关键1:insert();
    str.insert(1, "111");//从第一个位置起,插入“111”
    cout << str << endl;//h111ello

//    2、删除 关键2:erase();
    str.erase(1, 3);  //从1号位置开始删除3个字符
    cout << str << endl;//hello
}

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

在这里插入图片描述

1.9 string子串

功能描述
从字符串中获取想要的子串。
函数原型

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

示例:

#include <iostream>
using namespace std;
#include <string>

//string求子串
void test01()
{

//    关键1:substr()
    string str = "abcdefg";
    string subStr = str.substr(1, 3);//从位置1开始截取3个字符
    cout << "subStr = " << subStr << endl;//subStr = bcd
}

void test02()
{
//   例子:从邮箱地址中获取用户名信息
    string email = "hello@sina.com";
    int pos = email.find("@");
    cout<<"@的位置:"<<pos<<endl;//@的位置:5
    
    string username = email.substr(0, pos);
    cout << "username: " << username << endl;//username: hello
}

int main()
{
    test01();
    test02();
    return 0;
}

在这里插入图片描述

二、vector容器

2.1 vector的基本概念

功能
vector数据结构和数组非常相似,也称为单端数组
vector与普通数组区别:
不同之处在于数组是静态空间,而vector可以动态扩展
动态扩展
1、并不是在原空间之后续接新空间,而是寻找更大的内存空间,然后将元数据拷贝新空间,释放原空间。
2、vector容器的迭代器是支持随机访问的迭代器。

2.2 vector构造函数

功能描述
创建vector容器。
函数原型

  • vector 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 << " ";
    }
    cout << 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.end());
    printVector(v2);

    //通过n个elem方式构造
    vector<int> v3(10, 100);
    printVector(v3);

    //拷贝构造
    vector<int> v4(v3);
    printVector(v4);
}

int main()
{
    test01();

    return 0;
}

在这里插入图片描述

2.3 vector赋值操作

功能描述
给vector容器进行赋值。
函数原型

  • vector& 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 << " ";
    }
    cout << endl;
}

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

    vector<int>v2;
    v2 = v1;//关键1:重载等号操作符
    printVector(v2);

    vector<int>v3;
    v3.assign(v1.begin(), v1.end());//关键2:利用assign(beg,end)将[beg, end)区间中的数据拷贝赋值给本身。
    printVector(v3);

    vector<int>v4;
    v4.assign(10, 100); //关键3:利用assign(n, elem)将n个elem拷贝赋值给本身。
    printVector(v4);
}

int main()
{
    test01();

    return 0;
}

在这里插入图片描述

2.4 vector容量和大小

功能描述
对vector容器的容量和大小操作。
函数原型

  • empty(); //判断容器是否为空
  • capacity(); //容器的容量
  • size(); //返回容器中元素的个数
  • resize(int num); //重新指定容器的长度为num,若容器变长,则以默认值填充新位置。
    ​ //如果容器变短,则末尾超出容器长度的元素被删除。
  • resize(int num, elem); //重新指定容器的长度为num,若容器变长,则以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 << " ";
    }
    cout << endl;
}

// vector容量和大小
void test01()
{
    vector<int> v1;
    for (int i = 0; i < 10; i++)
    {
        v1.push_back(i);
    }
    printVector(v1);
//    知识点1:利用empty()判断容器是否为空
    if (v1.empty())
    {
        cout << "v1为空" << endl;
    }
    else
    {
        cout << "v1不为空" << endl;
        cout << "v1的容量 = " << v1.capacity() << endl;//知识点2:利用capacity()容器的容量
        cout << "v1的大小 = " << v1.size() << endl;//知识点3:size()返回容器中元素的个数
    }

    //知识点4:resize(int num, elem); 重新指定大小 ,若指定的更大,默认用0填充新位置,可以利用重载版本elem替换默认填充
    v1.resize(15,10);
    printVector(v1);

    //知识点5:resize(int num); 重新指定大小 ,若指定的更小,超出部分元素被删除
    v1.resize(5);
    printVector(v1);
}

int main()
{
    test01();

    return 0;
}

在这里插入图片描述

2.5 vector插入和删除

功能描述
对vector容器进行插入、删除操作。
函数原型

  • 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 << " ";
    }
    cout << endl;
}

// vector插入和删除
void test01()
{
    vector<int> v1;
    //知识点1:push_back(ele)尾部插入元素ele
    v1.push_back(10);
    v1.push_back(20);
    v1.push_back(30);
    v1.push_back(40);
    v1.push_back(50);
    printVector(v1);

    //知识点2:pop_back()尾删
    v1.pop_back();
    printVector(v1);

    //知识点3:insert(const_iterator pos, ele)迭代器指向位置pos插入元素ele
    v1.insert(v1.begin(), 100);
    printVector(v1);

//  知识点4:insert(const_iterator pos, int count,ele)迭代器指向位置pos插入count个元素ele
    v1.insert(v1.begin(), 2, 1000);
    printVector(v1);

    //知识点5:erase(const_iterator pos)删除迭代器指向的元素
    v1.erase(v1.begin());
    printVector(v1);

    //知识点6:erase(const_iterator start, const_iterator end)删除迭代器从start到end之间的元素
    v1.erase(v1.begin(), v1.end());
//    知识点7:clear()删除容器中所有元素
    v1.clear();
    printVector(v1);
}

int main()
{
    test01();

    return 0;
}

在这里插入图片描述

2.6 vector数据存取

功能描述
对vector容器的数据进行存取操作。
函数原型

  • at(int idx); //返回索引idx所指的数据
  • operator[]; //返回索引idx所指的数据
  • front(); //返回容器中第一个数据元素
  • back(); //返回容器中最后一个数据元素

示例:

#include <iostream>
using namespace std;
#include <vector>

//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] << " ";//知识点1:operator[]返回索引idx所指的数据
    }
    cout << endl;

    for (int i = 0; i < v1.size(); i++)
    {
        cout << v1.at(i) << " ";//知识点2:at(int idx)返回索引idx所指的数据
    }
    cout << endl;

    cout << "v1的第一个元素为: " << v1.front() << endl;//知识点3:front()返回容器中第一个数据元素
    cout << "v1的最后一个元素为: " << v1.back() << endl;//知识点4:back()返回容器中最后一个数据元素
}
int main()
{
    test01();

    return 0;
}

在这里插入图片描述

2.7 互换容器

功能描述
实现两个容器内的数据进行互换。
函数原型

  • 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 << " ";
    }
    cout << endl;
}

//vector互换容器
void test01()
{
    vector<int>v1;
    for (int i = 0; i < 10; i++)
    {
        v1.push_back(i);
    }
    printVector(v1);//0 1 2 3 4 5 6 7 8 9

    vector<int>v2;
    for (int i = 10; i > 0; i--)
    {
        v2.push_back(i);
    }
    printVector(v2);//10 9 8 7 6 5 4 3 2 1

    //互换容器
    cout << "互换后" << endl;
    v1.swap(v2);//知识点1:swap(vec)将vec与本身的元素互换
    printVector(v1);//10 9 8 7 6 5 4 3 2 1
    printVector(v2);//0 1 2 3 4 5 6 7 8 9
}

void test02()
{

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

    cout << "v的容量为:" << v.capacity() << endl;//131072
    cout << "v的大小为:" << v.size() << endl;//100000

    v.resize(3);//重新指定容器长度

    cout << "v的容量为:" << v.capacity() << endl;//131072
    cout << "v的大小为:" << v.size() << endl;//3

    //实际用途:收缩内存
    vector<int>(v).swap(v); //匿名对象

    cout << "v的容量为:" << v.capacity() << endl;//3
    cout << "v的大小为:" << v.size() << endl;//3
}

int main()
{
    test01();
    test02();
    return 0;
}

在这里插入图片描述

2.8 vector预留空间

功能描述
减少vector在动态扩展容量时的扩展次数。
函数原型

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

示例:

#include <iostream>
using namespace std;
#include <vector>

//vector预留空间
void test01()
{
    vector<int> v;
    //知识点1:reserve(int len)容器预留len个元素长度,预留位置不初始化,元素不可访问。
    v.reserve(100000);

    int num = 0;
    int* p = NULL;
    for (int i = 0; i < 100000; i++)
    {
        v.push_back(i);
        if (p != &v[0])
        {
            p = &v[0];
            num++;
        }
    }
    cout << "num:" << num << endl;
}

int main()
{
    test01();

    return 0;
}

在这里插入图片描述

三、deque容器

3.1 deque容器的基本概率

功能
双端数组,可以对头端进行插入删除操作。
deque和vector区别
1、vector对于头部的插入删除效率低,数据量大,效率越低;
2、deque相对而言,对头部的插入删除速度会比vector块;
3、vector访问元素时的速度会比deque快,这和两者内部实现相关。
deque内部工作原理
1、deque内部有个中控器,维护每段缓冲区中的容器,缓冲区中存放真实数据。
2、中控器维护的是每个缓冲区的地址,使得使用deque时像一片连续的内存空间。
3、deque容器的迭代器也是支持随机访问的。

3.2 deque构造函数

功能描述
deque容器构造。
函数原型

  • deque deqT; //默认构造形式
  • deque(beg, end);//构造函数将[beg, end)区间中的元素拷贝给本身。
  • deque(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;
}

//deque构造
void test01() {

//    知识点1:deque<T> deqT; //默认构造形式
    deque<int> d1;
    for (int i = 0; i < 10; i++)
    {
        d1.push_back(i);
    }
    printDeque(d1);//0 1 2 3 4 5 6 7 8 9

//    知识点2:deque(beg, end); //构造函数将[beg, end)区间中的元素拷贝给本身
    deque<int> d2(d1.begin(),d1.end());
    printDeque(d2);//0 1 2 3 4 5 6 7 8 9

//    知识点3:deque(n, elem); //构造函数将n个elem拷贝给本身
    deque<int>d3(10,100);
    printDeque(d3);//100 100 100 100 100 100 100 100 100 100

//    知识点4:deque(const deque &deq); //拷贝构造函数
    deque<int>d4 = d3;
    printDeque(d4);//100 100 100 100 100 100 100 100 100 100
}

int main()
{
    test01();

    return 0;
}

在这里插入图片描述

3.3 deque赋值操作

功能描述
给deque容器进行赋值。
函数原型

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

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

//   知识点1: deque& operator=(const deque &deq); //重载等号操作符
    deque<int>d2;
    d2 = d1;
    printDeque(d2);

//    知识点2:assign(beg, end); //将[beg, end)区间中的数据拷贝赋值给本身。
    deque<int>d3;
    d3.assign(d1.begin(), d1.end());
    printDeque(d3);

//   知识点3: assign(n, elem); //将n个elem拷贝赋值给本身。
    deque<int>d4;
    d4.assign(10, 100);
    printDeque(d4);

}

int main()
{
    test01();

    return 0;
}

在这里插入图片描述

3.4 deque大小操作

功能描述
对deque容器的大小进行操作。
函数原型

  • deque.empty(); //判断容器是否为空
  • deque.size(); //返回容器中元素的个数
  • deque.resize(num);//重新指定容器的长度为num,若容器变长,则以默认值填充新位置。
    ​ //如果容器变短,则末尾超出容器长度的元素被删除。
  • deque.resize(num, elem); //重新指定容器的长度为num,若容器变长,则以默认值填充新位置。
    ​ //如果容器变短,则末尾超出容器长度的元素被删除。
#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;
}

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

//    1、 deque.empty(); //判断容器是否为空
    if (d1.empty()) {
        cout << "d1为空!" << endl;
    }
    else {
        cout << "d1不为空!" << endl;
//      2、deque.size(); //返回容器中元素的个数
        cout << "d1的大小为:" << d1.size() << endl;
    }

//  3、 deque.resize(num, elem);//重新指定大小
    d1.resize(15, 1);
    printDeque(d1);

// 4、 deque.resize(num);//重新指定大小
    d1.resize(5);
    printDeque(d1);
}

int main()
{
    test01();

    return 0;
}

在这里插入图片描述

3.5 deque插入和删除

功能描述

  • 向deque容器中插入和删除数据。

函数原型
1、两端插入操作:

  • push_back(elem); //在容器尾部添加一个数据
  • push_front(elem);//在容器头部插入一个数据
  • pop_back();//删除容器最后一个数据
  • pop_front(); //删除容器第一个数据

2、指定位置操作:

  • 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>

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

//1、两端操作
void test01()
{
    deque<int> d;
//    1-1、push_back(elem); //在容器尾部添加一个数据
    d.push_back(10);
    d.push_back(20);
//    1-2、push_front(elem); //在容器头部插入一个数据
    d.push_front(100);
    d.push_front(200);

    printDeque(d);//200 100 10 20

//    1-3、pop_back(); //删除容器最后一个数据
    d.pop_back();
//    1-4、pop_front(); //删除容器第一个数据
    d.pop_front();
    printDeque(d);//100 10
}

//2、插入
void test02()
{
    deque<int> d;
    d.push_back(10);
    d.push_back(20);
    d.push_front(100);
    d.push_front(200);
    printDeque(d);//200 100 10 20
//    2-1 insert(pos,elem); //在pos位置插入一个elem元素的拷贝,返回新数据的位置。
    d.insert(d.begin(), 1000);
    printDeque(d);//1000 200 100 10 20
//    2-2 insert(pos,n,elem); //在pos位置插入n个elem数据,无返回值。
    d.insert(d.begin(), 2,10000);
    printDeque(d);//10000 10000 1000 200 100 10 20

    deque<int>d2;
    d2.push_back(1);
    d2.push_back(2);
    d2.push_back(3);
//    2-3 insert(pos,beg,end); //在pos位置插入[beg,end)区间的数据,无返回值。
    d.insert(d.begin(), d2.begin(), d2.end());
    printDeque(d);//1 2 3 10000 10000 1000 200 100 10 20

}

//删除
void test03()
{
    deque<int> d;
    d.push_back(10);
    d.push_back(20);
    d.push_front(100);
    d.push_front(200);
    printDeque(d);//200 100 10 20
//    3-1 erase(pos); //删除pos位置的数据,返回下一个数据的位置。
    d.erase(d.begin());
    printDeque(d);//100 10 20
//    3-2 erase(beg,end); //删除[beg,end)区间的数据,返回下一个数据的位置。
    d.erase(d.begin(), d.end());
//    3-3 clear(); //清空容器的所有数据
    d.clear();
    printDeque(d);
}

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

    return 0;
}

在这里插入图片描述

3.6 deque数据存取

功能描述
对deque中的数据的存取操作。
函数原型

  • at(int idx); //返回索引idx所指的数据
  • operator[]; //返回索引idx所指的数据
  • front();//返回容器中第一个数据元素
  • back(); //返回容器中最后一个数据元素

示例:

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

//deque 数据存取数据存取
void test01()
{
    deque<int> d;
    d.push_back(10);
    d.push_back(20);
    d.push_front(30);
    d.push_front(40);

    for (int i = 0; i < d.size(); i++)
    {
//      1、  operator[]; //返回索引idx所指的数据
        cout << d[i] << " ";
    }
    cout << endl;


    for (int i = 0; i < d.size(); i++)
    {
//       2、 at(int idx); //返回索引idx所指的数据
        cout << d.at(i) << " ";
    }
    cout << endl;

//    3、front(); //返回容器中第一个数据元素
    cout << "第一个元素为:" << d.front() << endl;//第一个元素为:40
//    4、back(); //返回容器中最后一个数据元素
    cout << "最后一个元素为:" << d.back() << endl;//最后一个元素为:20
}

int main()
{
    test01();

    return 0;
}

在这里插入图片描述

3.7 deque排序

功能描述
利用算法对deque容器进行排序。
函数原型

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

示例:

#include <iostream>
using namespace std;
#include <deque>
#include <algorithm>

//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> d;
    d.push_back(10);
    d.push_back(20);
    d.push_front(300);
    d.push_front(400);

    printDeque(d);//400 300 10 20
//  1、  sort(iterator beg, iterator end) //对beg和end区间内元素进行排序
    sort(d.begin(), d.end());
    printDeque(d);//10 20 300 400

}
int main()
{
    test01();

    return 0;
}

在这里插入图片描述

四、stack容器

4.1 stack的基本概念

基本概念
stack是一种先进后出的数据结构,它只有一个出口
注意事项
1、栈中只有顶端的元素才可以被外界使用,因此栈不允许有遍历行为。
2、栈中进入数据称为——入栈push
3、栈中弹出数据称为——出栈pop

4.2 stack 常用接口

功能描述
栈容器常用的对外接口。
构造函数

  • stack 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;

//  1、push(elem); //向栈顶添加元素
    s.push(10);
    s.push(20);
    s.push(30);
    s.push(40);

    while (!s.empty())
    {
//      2、  top(); //返回栈顶元素
        cout << "栈顶元素为: " << s.top() << endl;
//      3、  pop(); //从栈顶移除第一个元素
        s.pop();
    }
    cout << "栈的大小为:" << s.size() << endl;
}

int main()
{
    test01();

    return 0;
}

在这里插入图片描述

五、queue容器

5.1 queue基本概念

基本概念
queue是一种先进先出的数据结构,它有两个出口,队列中允许从一端新增元素,从另一端移除元素。
注意事项
1、队列中只有对头和队尾才可以被外界使用,因此不允许队列有遍历行为;
2、队列中进数据称为——入队push;
3、队列中出数据称为——出队pop。

5.2 queue常用接口

功能描述
栈容器常用的对外接口。
构造函数

  • queue que; //queue采用模板类实现,queue对象的默认构造形式
  • queue(const queue &que); //拷贝构造函数

赋值操作

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

数据存取

  • push(elem);//往队尾添加元素
  • pop(); //从队头移除第一个元素
  • back(); //返回最后一个元素
  • front();//返回第一个元素

大小操作

  • empty(); //判断堆栈是否为空
  • size(); //返回栈的大小

示例:

#include <iostream>
using namespace std;
#include <queue>
#include <string>

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

    string m_Name;
    int m_Age;
};

void test01()
{
    //创建队列
    queue<Person> q;

    //准备数据
    Person p1("唐僧", 30);
    Person p2("孙悟空", 1000);
    Person p3("猪八戒", 900);
    Person p4("沙僧", 800);

//    push(elem); //往队尾添加元素
    q.push(p1);
    q.push(p2);
    q.push(p3);
    q.push(p4);

    while (!q.empty())
    {
//        front(); //返回第一个元素
        cout << "队头元素-- 姓名: " << q.front().m_Name
             << " 年龄: "<< q.front().m_Age << endl;
//        back(); //返回最后一个元素
        cout << "队尾元素-- 姓名: " << q.back().m_Name
             << " 年龄: " << q.back().m_Age << endl;

        cout << endl;
//        pop(); //从队头移除第一个元素
        q.pop();
    }

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

int main()
{
    test01();

    return 0;
}

在这里插入图片描述

六、list容器

6.1 list基本概念

功能
将数据进行链式存储。
注意事项
1、链表(list)是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接实现的。
2、链表的组成:链表由一系列节点组成。
3、节点的组成:一个是存储数据元素的数据域,另一个是存储下一个节点地址的指针域。STL中的链表是一个双向循环链表。
4、由于链表的存储方式并不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器。
5、list的优点:
(1)采用动态存储分配,不会再次内存浪费和溢出。
(2)链表执行插入和删除操作十分方便,修改指针即可,不需要移动大量元素。
6、list的缺点:
链表灵活,但是空间(指针域)和时间(遍历)额外耗费大。
7、list有一个重要的性质,插入操作和删除操作都不会造成原有list迭代器的实效,这在vector是不成立的。
8、总结:STL中list和vector是两个最常被使用的容器,各有优缺点。

6.2 list构造函数

功能描述
创建list容器。
函数原型

  • list lst; //list采用采用模板类实现,对象的默认构造形式:
  • list(beg,end); //构造函数将[beg, end)区间中的元素拷贝给本身。
  • list(n,elem); //构造函数将n个elem拷贝给本身。
  • list(const list &lst); //拷贝构造函数。

示例:

#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()
{
//  1、list<T> lst; //list采用采用模板类实现,对象的默认构造形式:
    list<int>L1;
    //添加数据
    L1.push_back(10);
    L1.push_back(20);
    L1.push_back(30);
    L1.push_back(40);

    printList(L1);//10 20 30 40

//  2、  list(beg,end); //构造函数将[beg, end)区间中的元素拷贝给本身。
    list<int>L2(L1.begin(),L1.end());
    printList(L2);//10 20 30 40

//  3、  list(n,elem); //构造函数将n个elem拷贝给本身。
    list<int>L3(L2);
    printList(L3);//10 20 30 40

//  4、  list(const list &lst); //拷贝构造函数。
    list<int>L4(10, 1000);
    printList(L4);//1000 1000 1000 1000 1000 1000 1000 1000 1000 1000
}

int main()
{
    test01();

    return 0;
}

在这里插入图片描述

6.3 list赋值和交换

功能描述
给list容器进行赋值,以及交换list容器。
函数原型

  • assign(beg, end); //将[beg, end)区间中的数据拷贝赋值给本身。
  • assign(n, elem); //将n个elem拷贝赋值给本身。
  • list& operator=(const list &lst); //重载等号操作符
  • swap(lst); //将lst与本身的元素互换。

示例:

#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;
// 1、 operator=赋值
    L2 = L1;
    printList(L2);

    list<int>L3;
//  2、  assign(beg, end); //将[beg, end)区间中的数据拷贝赋值给本身。
    L3.assign(L2.begin(), L2.end());
    printList(L3);

    list<int>L4;
//  3、  assign(n, elem); //将n个elem拷贝赋值给本身
    L4.assign(10, 100);
    printList(L4);

}

//交换
void test02()
{

    list<int>L1;
    L1.push_back(10);
    L1.push_back(20);
    L1.push_back(30);
    L1.push_back(40);

    list<int>L2;
    L2.assign(10, 100);

    cout << "交换前: " << endl;
    printList(L1);
    printList(L2);

    cout << endl;

//  4、  swap(lst); //将lst与本身的元素互换。
    L1.swap(L2);

    cout << "交换后: " << endl;
    printList(L1);
    printList(L2);

}

int main()
{
    test01();
    test02();
    return 0;
}

在这里插入图片描述

6.4 list大小操作

功能描述
对list容器的大小进行操作。
函数原型

  • 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);
//  1、  empty(); //判断容器是否为空
    if (L1.empty())
    {
        cout << "L1为空" << endl;
    }
    else
    {
        cout << "L1不为空" << endl;
//  2、      size(); //返回容器中元素的个数
        cout << "L1的大小为:" << L1.size() << endl;
    }

//  3、  resize(num); //重新指定容器的长度为num,若容器变长,则以默认值填充新位置。
    L1.resize(10);
    printList(L1);
//  4、  resize(num, elem); //重新指定容器的长度为num,若容器变长,则以elem值填充新位置。
    L1.resize(15,5);
    printList(L1);
}

int main()
{
    test01();

    return 0;
}

在这里插入图片描述

6.5 list 插入和删除

功能描述
对list容器进行数据的插入和删除。
函数原型

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

//list 插入和删除
void test01()
{
    list<int> L;
    //1、尾插
    L.push_back(10);
    L.push_back(20);
    L.push_back(30);
    //2、头插
    L.push_front(100);
    L.push_front(200);
    L.push_front(300);

    printList(L);

    //3、尾删
    L.pop_back();
    printList(L);

    //4、头删
    L.pop_front();
    printList(L);

    //5、插入
    list<int>::iterator it = L.begin();
    L.insert(++it, 1000);
    printList(L);

    //6、删除
    it = L.begin();
    L.erase(++it);
    printList(L);

    L.push_back(10000);
    L.push_back(10000);
    L.push_back(10000);
    printList(L);
//  7、  remove(elem);//删除容器中所有与elem值匹配的元素。
    L.remove(10000);
    printList(L);

//  8、清空
    L.clear();
    printList(L);
}


int main()
{
    test01();

    return 0;
}

在这里插入图片描述

6.6 list数据存取

功能描述
对list容器中的数据进行存取。
函数原型

  • 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);

    //cout << L1.at(0) << endl;//错误 不支持at访问数据
    //cout << L1[0] << endl; //错误  不支持[]方式访问数据
//    原因:list本质是链表,不是用连续线性空间存储数据

//  1、  front(); //返回第一个元素。
    cout << "第一个元素为: " << L1.front() << endl;//第一个元素为: 10
//  2、  back(); //返回最后一个元素。
    cout << "最后一个元素为: " << L1.back() << endl;//最后一个元素为: 40

    //list容器的迭代器是双向迭代器,不支持随机访问
    list<int>::iterator it = L1.begin();
    //it = it + 1;//错误,不可以跳跃访问,即使是+1
}

int main()
{
    test01();

    return 0;
}

在这里插入图片描述

6.7 反转和排序

功能描述
将容器中的元素反转,以及将容器中的数据进行排序。
函数原型

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

bool myCompare(int val1 , int val2)
{
    return val1 > val2;
}

//反转和排序
void test01()
{
    list<int> L;
    L.push_back(20);
    L.push_back(10);
    L.push_back(50);
    L.push_back(40);
    L.push_back(30);
    printList(L);//20 10 50 40 30

//  1、  reverse(); //反转链表
    L.reverse();
    printList(L);//30 40 50 10 20

//  2、  sort(); //链表排序
    L.sort(); //默认的排序规则 从小到大
    printList(L);//10 20 30 40 50

    L.sort(myCompare); //指定规则,从大到小
    printList(L);//50 40 30 20 10
}

int main()
{
    test01();

    return 0;
}

在这里插入图片描述

七、set/multiset容器

7.1 set的基本概念

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

7.2 set构造和赋值

功能描述
创建set容器以及赋值。
函数原型
构造:

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

//set构造和赋值
void test01()
{
//  1、 set<T> st; //默认构造函数:
    set<int> s1;

//    插入数据用insert
    s1.insert(10);
    s1.insert(30);
    s1.insert(20);
    s1.insert(40);
//    set插入数据会自动排序
    printSet(s1);//10 20 30 40

//  2、  set(const set &st); //拷贝构造函数
    set<int>s2(s1);
    printSet(s2);

//  3、  set& operator=(const set &st); //重载等号操作符
    set<int>s3;
    s3 = s2;
    printSet(s3);
}

int main()
{
    test01();

    return 0;
}

在这里插入图片描述

7.3 set大小和交换

功能描述
统计set容器大小以及交换set容器。
函数原型

  • 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;
}
/*set大小和交换*/

//大小
void test01()
{

    set<int> s1;

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

//  1、  empty(); //判断容器是否为空
    if (s1.empty())
    {
        cout << "s1为空" << endl;
    }
    else
    {
        cout << "s1不为空" << endl;
//  2、      size(); //返回容器中元素的数目
        cout << "s1的大小为: " << s1.size() << endl;
    }

}

//交换
void test02()
{
    set<int> s1;

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

    set<int> s2;

    s2.insert(100);
    s2.insert(300);
    s2.insert(200);
    s2.insert(400);

    cout << "交换前" << endl;
    printSet(s1);//10 20 30 40
    printSet(s2);//100 200 300 400
    cout << endl;

    cout << "交换后" << endl;
//  3、  swap(st); //交换两个集合容器
    s1.swap(s2);
    printSet(s1);//100 200 300 400
    printSet(s2);//10 20 30 40
}

int main()
{
    test01();
    test02();
    return 0;
}

在这里插入图片描述

7.4 set插入和删除

功能描述
set容器进行插入数据和删除数据。
函数原型

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

//set插入和删除
void test01()
{
    set<int> s1;
//  1、  insert(elem); //在容器中插入元素。
    s1.insert(10);
    s1.insert(30);
    s1.insert(20);
    s1.insert(40);
    printSet(s1);//10 20 30 40

//  2、  erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器。
    s1.erase(s1.begin());
    printSet(s1);//20 30 40
    
//  3、  erase(elem); //删除容器中值为elem的元素。
    s1.erase(30);
    printSet(s1);//20 40

//  4、  erase(beg, end); //删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
    //s1.erase(s1.begin(), s1.end());
    
//  5、  clear(); //清除所有元素
    s1.clear();
    printSet(s1);
}

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

在这里插入图片描述

7.5 set查找和统计

功能描述
对set容器进行查找数据以及统计数据。
函数原型

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

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

//  1、  find(key); //查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
    set<int>::iterator pos = s1.find(30);

    if (pos != s1.end())
    {
        cout << "找到了元素 : " << *pos << endl;
    }
    else
    {
        cout << "未找到元素" << endl;
    }

//  2、  count(key); //统计key的元素个数
    int num = s1.count(30);
    cout << "num = " << num << endl;
}

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

在这里插入图片描述

7.6 set和multiset区别

1、set不可以插入重复数据,而multiset可以。
2、set插入数据的同时会返回插入结果,表示插入成功。
3、multiset不会检测数据,因此可以插入重复数据。

示例:

#include <iostream>
using namespace std;
#include <set>

//set和multiset容器的区别
void test01()
{
    set<int> s;
    pair<set<int>::iterator, bool>  ret = s.insert(10);
    if (ret.second)
    {
        cout << "第一次插入成功!" << endl;
    }
    else
    {
        cout << "第一次插入失败!" << endl;
    }

    ret = s.insert(10);
    if (ret.second) {
        cout << "第二次插入成功!" << endl;
    }
    else {
        cout << "第二次插入失败!" << endl;
    }

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

在这里插入图片描述

7.7 pair对组创建

功能描述
成对出现的数据。利用对组可以返回两个数据。
函数原型

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

示例:

#include <iostream>
using namespace std;
#include <string>

//对组创建
void test01()
{
    pair<string, int> p("Tom", 20);
    cout << "姓名: " <<  p.first << " 年龄: " << p.second << endl;

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

int main()
{
    test01();

    return 0;
}

在这里插入图片描述

7.8 set容器排序

学习目标:
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(40);
    s1.insert(20);
    s1.insert(50);
    s1.insert(30);

    //默认顺序从小到大
    for (set<int>::iterator it = s1.begin(); it != s1.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;

    //指定排序从大到小
    set<int,MyCompare> s2;
    s2.insert(10);
    s2.insert(40);
    s2.insert(20);
    s2.insert(50);
    s2.insert(30);

    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>

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

    string m_Name;
    int m_Age;

};

class comparePerson
{
public:
    bool operator()(const Person& p1, const Person &p2)
    {
        //按照年龄大小进行排序
        return p1.m_Age > p2.m_Age;
    }
};

void test01()
{
    set<Person, comparePerson> s;

    Person p1("刘备", 24);
    Person p2("关羽", 28);
    Person p3("张飞", 25);
    Person p4("赵云", 21);

    s.insert(p1);
    s.insert(p2);
    s.insert(p3);
    s.insert(p4);

    for (set<Person, comparePerson>::iterator it = s.begin(); it != s.end(); it++)
    {
        cout << "姓名: " << it->m_Name << " 年龄: " << it->m_Age << endl;
    }
}

int main()
{
    test01();

    return 0;
}

在这里插入图片描述

八、map/multimap容器

8.1 map基本概念

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

8.2 map构造和赋值

功能描述
对map容器进行构造和赋值操作。
函数原型
构造:

  • map<T1, T2> mp; //map默认构造函数:
  • 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()
{
//  1、  map<T1, T2> mp; //map默认构造函数:
    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);

//  2、  map(const map &mp); //拷贝构造函数
    map<int, int>m2(m); //拷贝构造
    printMap(m2);

//  3、  map& operator=(const map &mp); //重载等号操作符
    map<int, int>m3;
    m3 = m2; //赋值
    printMap(m3);
}

int main()
{
    test01();

    return 0;
}

在这里插入图片描述

8.3 map大小和交换

功能描述
统计map容器大小以及交换map容器。
函数原型

  • 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));

//  1、  empty(); //判断容器是否为空
    if (m.empty())
    {
        cout << "m为空" << endl;
    }
    else
    {
        cout << "m不为空" << endl;
//   2、     size(); //返回容器中元素的数目
        cout << "m的大小为: " << m.size() << 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));

    map<int, int>m2;
    m2.insert(pair<int, int>(4, 100));
    m2.insert(pair<int, int>(5, 200));
    m2.insert(pair<int, int>(6, 300));

    cout << "交换前" << endl;
    printMap(m);
    printMap(m2);

    cout << "交换后" << endl;
//  3、  swap(st); //交换两个集合容器
    m.swap(m2);
    printMap(m);
    printMap(m2);
}

int main()
{
    test01();
    test02();
    return 0;
}

在这里插入图片描述

8.4 map插入和删除

功能描述
map容器进行插入数据和删除数据。
函数原型

  • insert(elem); //在容器中插入元素。
  • clear(); //清除所有元素
  • erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器。
  • erase(beg, end);//删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
  • erase(elem);//删除容器中值为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;
}

//map插入和删除
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);

//    erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器。
    m.erase(m.begin());
    printMap(m);
//    erase(key); //删除容器中值为key的元素。
    m.erase(3);
    printMap(m);

//    erase(beg, end); //删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
    m.erase(m.begin(),m.end());
//    clear(); //清除所有元素
    m.clear();
    printMap(m);
}

int main()
{
    test01();

    return 0;
}

在这里插入图片描述

8.5 map查找和统计

功能描述
对map容器进行查找数据以及统计数据。
函数原型

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

示例:

#include <iostream>
using namespace std;
#include <map>

//map查找和统计
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));

//  1、  find(key); //查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
    map<int, int>::iterator pos = m.find(3);

    if (pos != m.end())
    {
        cout << "找到了元素 key = " << (*pos).first << " value = " << (*pos).second << endl;
    }
    else
    {
        cout << "未找到元素" << endl;
    }

//  2、  count(key); //统计key的元素个数
    int num = m.count(3);
    cout << "num = " << num << endl;
}

int main()
{
    test01();

    return 0;
}

在这里插入图片描述

8.6 map容器排序

学习目标
map容器默认排序规则为按照key值进行从小到大排序,掌握如何改变排序规则

主要技术点:
利用仿函数,可以改变排序规则

示例:

#include <iostream>
using namespace std;
#include <map>

class MyCompare
{
public:
    bool operator()(int v1, int v2)
    {
        return v1 > v2;
    }
};

//map容器排序
void test01()
{
    //默认从小到大排序
    //利用仿函数实现从大到小排序
    map<int, int, MyCompare> m;

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

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

int main()
{
    test01();

    return 0;
}

在这里插入图片描述

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值