当前位置: 首页 > news >正文

STL-常用容器

3.1.1. string基本概念

本质:

  • string是C++风格的字符串,char*是C语言风格的字符串
  • string本质上是一个类

stringchar*的区别:

  • char*是一个指针
  • string是一个类,类内部封装并负责管理char*,是一个char*型的容器

特点:

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

3.1.2. string构造函数

初始化string函数方法:

  1. string();:创建一个空的字符串,例如:string str;
  2. string(const char* s);:使用字符串s初始化
  3. string(const string& str);:使用string对象初始化另一个string对象
  4. string(int n, char c);:使用n个相同的字符c初始化
#include<iostream>
using namespace std;void Test01()
{string s1;  //默认构造s1 = "hello";cout << s1 << endl;const char* str = "hello world";string s2(str);cout << s2 << endl;string s3(s2);cout << s3 << endl;string s4(10, 'a');cout << s4 << endl;}
int main()
{Test01();return 0;
}输出:
-----------------------------------------------------------------------------------------
hello
hello world
hello world
aaaaaaaaaa

3.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;void Test01()
{string str1;str1 = "hello world";cout << "str1 = " << str1 << endl;string str2(str1);cout << "str2 = " << str2 << endl;string str3 = "a";cout << "str3 = " << str3 << endl;string str4;str4.assign("hello C++");cout << "str4 = " << str4 << endl;string str5;str5.assign("hello C++", 5);cout << "str5 = " << str5 << endl;string str6;str6.assign(str5);cout << "str6 = " << str6 << endl;string str7;str7.assign(5, 'a');cout << "str7 = " << str7 << endl;
}
int main()
{Test01();return 0;
}输出:
-----------------------------------------------------------------------------------------
str1 = hello world
str2 = hello world
str3 = a
str4 = hello C++
str5 = hello
str6 = hello
str7 = aaaaa

3.1.4. string字符串拼接

功能描述:实现在字符串末尾拼接字符串

函数原型:

  • string& operator+=(const char* str); //重载+=操作符
  • string& operator+=(const char c); //重载+=操作符
  • string& operator+=(const string& str);string& append(const char *s); //把字符串s连接到当前字符串结尾
  • string& append(const char *s,int n); //同operator+=(const string& str)
  • string& append(const string &s); //同operator+=(const string& str)
  • string& append(const string&s,int pos,int n); //字符串s中从pos开始的n个字符连接到字符串结尾
#include<iostream>
#include<string>
using namespace std;void Test01()
{string str1 = "我";str1 += "爱学习";cout << "str1 = " << str1 << endl;str1 += ":";cout << "str1 = " << str1 << endl;string str2 = "C++ Python ";str1 += str2;cout << "str1 = " << str1 << endl;string str3 = "I ";str3.append("Love");cout << "str3 = " << str3 << endl;str3.append(" Study: C++ Python ", 8);cout << "str3 = " << str3 << endl;str3.append(str2);cout << "str3 = " << str3 << endl;str3.append(str2, 4, 10);cout << "str3 = " << str3 << endl;
}
int main()
{Test01();return 0;
}输出:
-----------------------------------------------------------------------------------------
str1 = 我爱学习
str1 = 我爱学习:
str1 = 我爱学习:C++ Python
str3 = I Love
str3 = I Love Study:
str3 = I Love Study: C++ Python
str3 = I Love Study: C++ Python Python

3.1.5. string查找和替换

功能描述:

  • 查找:查找指定字符串是否存在
  • 替换:在指定位置替换字符串

函数原型:

  • 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 charc,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>
#include<string>
using namespace std;//查找
void Test01()
{string str1 = "abcdefgde";int pos = str1.find("de");if (pos == -1){cout << "未找到字符串" << endl;}else{cout << "找到字符串,位置是:" << pos << endl;}//rfind:从右往左查找,find:从左往右查找pos = str1.rfind("de");if (pos == -1){cout << "未找到字符串" << endl;}else{cout << "找到字符串,位置是:" << pos << endl;}
}//替换
void Test02()
{string str1 = "abcdefg";//从1号位置起,替换3个字符为11111str1.replace(1, 3, "11111");cout << str1 << endl;
}int main()
{Test01();Test02();return 0;
}输出:
-----------------------------------------------------------------------------------------
找到字符串,位置是:3
找到字符串,位置是:7
a11111efg

3.1.6. string字符串比较

功能描述:字符串之间的比较

比较方式:比较字符的ASCII码(=返回0,>返回1,<返回-1)

函数原型:

  • int compare(const string &s) const; //与字符串s比较
  • int compare(const char *s) const; //与字符串s比较
#include<iostream>
#include<string>
using namespace std;void Test01()
{string str1 = "xello";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;
}输出:
-----------------------------------------------------------------------------------------
str1 > str2

3.1.7. string字符存取

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

  • char& operator[](int n); //通过[]方式取字符
  • char& at(int n); //通过at方式取字符
#include<iostream>
#include<string>
using namespace std;void Test01()
{string str1 = "hello";cout << "str1 = " << str1 << endl;for (int i = 0; i < str1.size(); i++){cout << str1[i] << " ";}cout << endl;for (int i = 0; i < str1.size(); i++){cout << str1.at(i) << " ";}cout << endl;str1[0] = 'x';cout << "str1 = " << str1 << endl;str1.at(1) = 'a';cout << "str1 = " << str1 << endl; }int main()
{Test01();return 0;
}输出:
-----------------------------------------------------------------------------------------
str1 = hello
h e l l o
h e l l o
str1 = xello
str1 = xallo

3.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>
#include<string>
using namespace std;void Test01()
{string str = "hello";str.insert(1, "666");cout << str << endl;str.erase(1, 3);cout << str << endl;
}int main()
{Test01();return 0;
}输出:
-----------------------------------------------------------------------------------------
h666ello
hello

3.1.9. string子串

功能描述:从字符串中获取想要的子串

函数原型:

  • string substr(int pos = 0, int n = npos) const; //返回由pos开始的n个字符组成的字符串
#include<iostream>
#include<string>
using namespace std;void Test01()
{string str = "hello";// 从第一号位置开始返回3个字符string sub_str = str.substr(1, 3);cout << "str = " << str << endl;cout << "sub_str = " << sub_str << endl;
}void Test02()
{string email = "zhangsan@sina.com";int pos = email.find("@");    // pos = 8// 从@开始返回pos个字符string name = email.substr(0,pos);cout << "name = " << name << endl;
}
int main()
{Test01();Test02();return 0;
}输出:
-----------------------------------------------------------------------------------------
str = hello
sub_str = ell
name = zhangsan

3.2. vector容器

3.2.1. vector基本概念

功能:

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

vector与普通数组的区别:

  • 数组是静态空间,分配固定内存,不能扩展;vector可以动态扩展内存空间

动态扩展:

  • 并不是在原空间后面续接新的空间,而是找更大的内存空间,然后将原数据拷贝到新空间,释放原空间

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

3.2.2. vector构造函数

功能描述:创建vector容器

函数原型:

  • vector<T> v; //采用模板实现类实现,默认构造函数
  • vector(v.begin(),v.end()); //将v[begin(), end())区间中的元素拷贝给本身
  • vector(n, elem); //构造函数将n个elem拷贝给本身
  • vector(const vector &vec); //拷贝构造函数
#include <iostream>
#include<vector>
using namespace std;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);//只能删除最后一个元素v1.pop_back();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;
}输出:
-----------------------------------------------------------------------------------------
0 1 2 3 4 5 6 7 8 9 
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8
100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100

3.2.3. vector赋值操作

功能描述:给vector容器赋值

函数原型:

  • vector& operator=(const vector &vec); //重载等号操作符
  • assign(beg,end); //将[beg, end)区间中的数据拷贝赋值给本身。
  • assign(n, elem); //将n个elem拷贝赋值给本身。
#include <iostream>
#include<vector>
using namespace std;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);}v1[0] = 5;PrintVector(v1);vector<int> 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;
}输出:
-----------------------------------------------------------------------------------------
5 1 2 3 4 5 6 7 8 9 
5 1 2 3 4 5 6 7 8 9
5 1 2 3 4 5 6 7 8 9 
100 100 100 100 100 100 100 100 100 100

3.2.4. vector容量和大小

功能描述:对vector容器的容量和大小进行操作

函数原型:

  • empty(); //判断容器是否为空
  • capacity(); //容器的容量
  • size(); //返回容器中元素的个数
  • resize(int num); //重新指定容器的长度为num,若容器变长,则以默认值填充新位置;如果容器变短,则末尾超出容器长度的元素被删除。
  • resize(int num, elem); //重新指定容器的长度为num,若容器变长,则以elem值填充新位置;如果容器变短,则末尾超出容器长度的元素被删除
#include<iostream>
#include<vector>
using namespace std;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 < 15; i++){v1.push_back(i);}PrintVector(v1);if(v1.empty()){cout << "v1为空" << endl;}else{cout << "v1不为空" << endl;cout << "v1的容量为:" << v1.capacity() << endl;cout << "v1的大小为:" << v1.size() << endl;}v1.resize(19);PrintVector(v1);cout << "v1的容量为:" << v1.capacity() << endl;cout << "v1的大小为:" << v1.size() << endl;v1.resize(23, 100);PrintVector(v1);cout << "v1的容量为:" << v1.capacity() << endl;cout << "v1的大小为:" << v1.size() << endl;v1.resize(5);PrintVector(v1);cout << "v1的容量为:" << v1.capacity() << endl;cout << "v1的大小为:" << v1.size() << endl;
}int main() 
{Test01();return 0;
}输出:
-----------------------------------------------------------------------------------------
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
v1不为空
v1的容量为:16
v1的大小为:15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0 0 0 0
v1的容量为:30
v1的大小为:19
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0 0 0 0 100 100 100 100
v1的容量为:30
v1的大小为:23
0 1 2 3 4
v1的容量为:30
v1的大小为:5

3.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 start,const iterator end); //删除迭代器指向的元素

erase(const iterator pos); //删除迭代器从start到end之间的元素

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

#include<iostream>
#include<vector>
using namespace std;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;  //尾插法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, 200);PrintVector(v1);//删除,第一个参数为迭代器v1.erase(v1.begin());PrintVector(v1);v1.erase(v1.begin(), v1.begin() + 2);PrintVector(v1);//清空v1.clear();PrintVector(v1);
}int main() 
{Test01();return 0;
}输出:
-----------------------------------------------------------------------------------------
10 20 30 40 50 
10 20 30 40
100 10 20 30 40
200 200 100 10 20 30 40
200 100 10 20 30 40
10 20 30 40

3.2.6. vector数据存取

函数原型:

  • at(int idx); //返回索引idx所指的数据
  • operator[]; //返回索引idx所指的数据
  • front(); //返回容器中的第一个数据
  • back(); //返回容器中的最后一个数据
#include<iostream>
#include<vector>
using namespace std;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;
}输出:
-----------------------------------------------------------------------------------------
0 1 2 3 4 5 6 7 8 9 
0 1 2 3 4 5 6 7 8 9
第一个元素为:0
最后一个元素为:9

3.2.7. vector互换容器

函数原型:

  • swap(vec); //将vec与本身的元素互换
#include<iostream>
#include<vector>
using namespace std;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);}vector<int> v2;for(int i = 10; i < 20; i++){v2.push_back(i);}v1.swap(v2);PrintVector(v1);PrintVector(v2);
}// 巧用swap()可以收缩内存空间
void Test02()
{vector<int> v2;for(int i = 0; i < 100000; i++){v2.push_back(i);}cout << "v2 的容量 = " << v2.capacity() << endl;cout << "v2 的大小 = " << v2.size() << endl;// 重新指定容器的大小,但不会改变容器的容量v2.resize(3);    cout << "v2 的容量 = " << v2.capacity() << endl;cout << "v2 的大小 = " << v2.size() << endl;// 巧用swap()可以收缩内存空间,vector<int>(v2)创造了与v2大小一致的匿名对象(类似拷贝构造),匿名对象在当前行执行完之后会被编译器回收vector<int>(v2).swap(v2);cout << "v2 的容量 = " << v2.capacity() << endl;cout << "v2 的大小 = " << v2.size() << endl;
}int main() 
{Test01();Test02();return 0;
}输出:
-----------------------------------------------------------------------------------------
10 11 12 13 14 15 16 17 18 19 
0 1 2 3 4 5 6 7 8 9
v2 的容量 = 131072
v2 的大小 = 100000
v2 的容量 = 131072
v2 的大小 = 3
v2 的容量 = 3
v2 的大小 = 3

3.2.8. vector预留空间

功能描述:减少vector在动态扩展容量时的扩展次数

函数原型:

  • reserve(int len); //容器预留len个长度的元素,预留位置不初始化,元素不可访问
#include<iostream>
#include<vector>
using namespace std;void PrintVector(vector<int> & v)
{for(vector<int>::iterator it = v.begin(); it != v.end(); it++){cout << *it << " ";}cout << endl;
}void Test01()
{vector<int> v;  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 << endl;vector<int> v1;  //利用reserve预留内存空间,减少内存分配次数v1.reserve(100000);int num1 = 0;int * p1 = NULL;for(int i = 0; i < 100000; i++){v1.push_back(i);if(p1 != &v1[0]){p1 = &v1[0];num1++;}}cout << num1 << endl;
}int main() 
{Test01();return 0;
}输出:
-----------------------------------------------------------------------------------------
18
1

3.3. deque容器

3.3.1. deque容器基本概念

功能:

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

deque与vector区别:

  • vector对于头部的插入删除效率低,数据量越大,效率越低
  • deque相对而言,对头部的插入删除速度回比vector快
  • vector访问元素时的速度会比deque快,这和两者内部实现有关

deque内部工作原理:

  • deque内部有个中控器,维护每段缓冲区中的内容,缓冲区中存放真实数据
  • 中控器维护的是每个缓冲区的地址,使得使用deque时像一片连续的内存空间
  • deque容器的迭代器也是支持随机访问的

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

3.3.2. deque构造函数

函数原型:

  • deque<T> deqT; //默认构造形式
  • deque(beg, end); //构造函数将[beg, end)区间中的元素拷贝给本身
  • deque(n, elem); //构造函数将n个elem拷贝给本身
  • deque(const deque &deg); //拷贝构造函数
#include<iostream>
#include<deque>
using namespace std;//如果要限制容器中的数据为只读状态,迭代器也要改变:const_iterator
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;
}输出:
-----------------------------------------------------------------------------------------
0 1 2 3 4 5 6 7 8 9 
0 1 2 3 4 5 6 7 8 9
100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100

3.3.3. deque赋值操作

函数原型:

  • deque& operator=(const deque &deq); //重载等号操作符
  • assign(beg, end); //将[beg,end)区间中的数据拷贝赋值给本身
  • assign(n,elem); //将n个elem拷贝赋值给本身。
#include<iostream>
#include<deque>
using namespace std;void PrintDeque(const deque<int> & d)
{for(deque<int>::const_iterator it = d.begin(); it != d.end(); it++){cout << *it << " ";}cout << endl;
}void Test01()
{deque<int> d1;for(int i = 0; i < 10; i++){d1.push_back(i);}PrintDeque(d1);deque<int> d2 = d1;PrintDeque(d2);deque<int> d3;d3.assign(d1.begin(), d1.end());PrintDeque(d3);deque<int> d4;d4.assign(10, 100);PrintDeque(d4);
}int main() 
{Test01();return 0;
}输出:
-----------------------------------------------------------------------------------------
0 1 2 3 4 5 6 7 8 9 
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
100 100 100 100 100 100 100 100 100 100

3.3.4. deque大小操作

deque没有容量的概念

函数原型:

#include<iostream>
#include<deque>
using namespace std;void PrintDeque(const deque<int> & d)
{for(deque<int>::const_iterator it = d.begin(); it != d.end(); it++){cout << *it << " ";}cout << endl;
}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);PrintDeque(d1);d1.resize(20, 100);PrintDeque(d1);d1.resize(5);PrintDeque(d1);
}int main() 
{Test01();return 0;
}输出
-----------------------------------------------------------------------------------------
0 1 2 3 4 5 6 7 8 9 
d1不为空
d1的大小为:10
0 1 2 3 4 5 6 7 8 9 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 100 100 100 100 100
0 1 2 3 4

3.3.5. deque插入和删除

#include<iostream>
#include<deque>
using namespace std;void PrintDeque(const deque<int> & d)
{for(deque<int>::const_iterator it = d.begin(); it != d.end(); it++){cout << *it << " ";}cout << endl;
}void Test01()
{deque<int> d1;// 尾插d1.push_back(10);d1.push_back(20);// 头插d1.push_front(30);d1.push_front(40);PrintDeque(d1);// 尾删d1.pop_back();// 头删d1.pop_front();PrintDeque(d1);
}void Test02()
{deque<int> d2;d2.push_back(10);d2.push_back(20);d2.push_back(100);d2.push_back(200);PrintDeque(d2);// insert插入d2.insert(d2.begin(), 1000);PrintDeque(d2);d2.insert(d2.begin(), 2, 10000);PrintDeque(d2);// 按照区间进行插入deque<int> d3;d3.push_back(1);d3.push_back(2);d3.push_back(3);d2.insert(d2.begin(), d3.begin(), d3.end());PrintDeque(d2);
}void Test03()
{deque<int> d4;d4.push_back(40);d4.push_back(80);d4.push_back(160);d4.push_back(240);PrintDeque(d4);// erase删除deque<int>::iterator it = d4.begin();it++;d4.erase(it);PrintDeque(d4);// 按区间方式删除d4.erase(d4.begin(), d4.begin() + 2);PrintDeque(d4);// 清空d4.clear();PrintDeque(d4);
}int main() 
{Test01();Test02();Test03();return 0;
}输出:
----------------------------------------------------------------------------------------
40 30 10 20 
30 10
10 20 100 200
1000 10 20 100 200
10000 10000 1000 10 20 100 200
1 2 3 10000 10000 1000 10 20 100 200
40 80 160 240
40 160 240
240

3.3.6. deque数据存取

#include<iostream>
#include<deque>
using namespace std;void PrintDeque(const deque<int> & d)
{for(deque<int>::const_iterator it = d.begin(); it != d.end(); it++){cout << *it << " ";}cout << endl;
}void Test01()
{deque<int> d1;d1.push_back(10);d1.push_back(20);d1.push_back(30);d1.push_front(100);d1.push_front(200);d1.push_front(300);// 通过[]访问元素for(int i = 0; i < d1.size(); i++){cout << d1[i] << " ";}cout << endl;// 通过at访问元素for(int i = 0; i < d1.size(); i++){cout << d1.at(i) << " ";}cout << endl;cout << "d1.front(): " << d1.front() << endl;cout << "d1.back(): " << d1.back() << endl;
}int main() 
{Test01();return 0;
}输出:
-----------------------------------------------------------------------------------------
300 200 100 10 20 30 
300 200 100 10 20 30
d1.front(): 300
d1.back(): 30

3.3.7. deque排序

功能描述:利用算法实现对deque容器进行排序

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

#include<iostream>
#include<deque>
#include<algorithm>    // 标准算法头文件
using namespace std;void PrintDeque(const deque<int> & d)
{for(deque<int>::const_iterator it = d.begin(); it != d.end(); it++){cout << *it << " ";}cout << endl;
}void Test01()
{deque<int> d;d.push_back(10);d.push_back(20);d.push_back(30);d.push_front(100);d.push_front(200);d.push_front(300);PrintDeque(d);// 排序(默认升序)// 对于支持随机访问的迭代器的容器,都可以使用sort算法直接进行排序,例如vector容器sort(d.begin(), d.end());cout << "排序后:";PrintDeque(d);
}int main() 
{Test01();return 0;
}输出:
-----------------------------------------------------------------------------------------
300 200 100 10 20 30 
排序后:10 20 30 100 200 300

3.4. stack容器

3.4.1. stack基本概念

概念:stack是一种先进后出(First In Last Out, FILO)的数据结构,只有一个出口,栈中只有顶端的元素才可以被外界使用,因此栈不允许有遍历行为

  • 栈可以判断该容器是否为空:empty()
  • 可以返回栈中元素个数,在入栈时统计:size()
  • 栈中进入数据称为 -- 入栈push(elm)
  • 栈中弹出数据称为 -- 出栈pop()

3.4.2. stack常用接口

构造函数:

  • 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 Test()
{stack<int> s;s.push(10);s.push(20);s.push(30);s.push(40);cout << "Size of stack is " << s.size() << endl;while(!s.empty()){cout << s.top() << endl;s.pop();}cout << "Size of stack is " << s.size() << endl;
}int main()
{Test();return 0;
}输出:
-----------------------------------------------------------------------------------------
Size of stack is 4
40
30
20
10
Size of stack is 0

3.5. queue容器

3.5.1. queue基本概念

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

队列容器允许从一端新增元素,从另一端移除元素

队列中只有队头和队尾才可以被外界使用,因此队列不允许有遍历行为

队列在队尾进数据,称为 --- 入队push

队列在队头出数据,称为 --- 出队pop

3.5.2. queue常用接口

构造函数:

queue<T> 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>class Person
{
public:Person(int age, string name){this->m_Age = age;this->m_Name = name;}int m_Age;string m_Name;
};void Test()
{queue<Person> q;Person p1(18, "唐僧");Person p2(1000, "孙悟空");Person p3(800, "猪八戒");Person p4(600, "沙僧");q.push(p1);q.push(p2);q.push(p3);q.push(p4);cout << "Size of queue is " << q.size() << endl;while( !q.empty() ){cout << "队头元素 -- 姓名:" << q.front().m_Name << "年龄:" << q.front().m_Age << endl;cout << "队尾元素 -- 姓名:" << q.back().m_Name << "年龄:" << q.back().m_Age << endl;cout << "-----------------------------------" <<endl;q.pop();}cout << "Size of queue is " << q.size() << endl;
}int main()
{Test();return 0;
}输出:
-----------------------------------------------------------------------------------------
Size of queue is 4
队头元素 -- 姓名:唐僧年龄:18
队尾元素 -- 姓名:沙僧年龄:600
-----------------------------------
队头元素 -- 姓名:孙悟空年龄:1000
队尾元素 -- 姓名:沙僧年龄:600
-----------------------------------
队头元素 -- 姓名:猪八戒年龄:800
队尾元素 -- 姓名:沙僧年龄:600
-----------------------------------
队头元素 -- 姓名:沙僧年龄:600
队尾元素 -- 姓名:沙僧年龄:600
-----------------------------------
Size of queue is 0

3.6. list容器

3.6.1. list基本概念

功能:将数据进行链式存储

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

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

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

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

链表和数组的对比:

STL中的链表:

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

list的优点:

  • 采用动态存储分配,不会造成内存浪费和溢出
  • 链表执行插入和删除操作十分方便,修改指针即可,不需要移动大量元素

list的缺点:

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

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

总结:STL中Listvector是两个最常被使用的容器,各有优缺点

3.6.2. list构造函数

功能描述:创建list容器

函数原型:

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

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

list<T> lst(n,elem); //构造函数将n个elem拷贝给本身

list<T> lst(const list &lst); //拷贝构造函数

总结:

  • list构造方式同其他几个STL常用容器,熟练掌握即可
#include<iostream>
using namespace std;
#include<list>
#include <typeinfo>void PrintList(const list<int>&L)
{for(list<int>::const_iterator it = L.begin(); it != L.end(); it++){cout << *it << " ";}cout << endl;
}void Test()
{list<int> lst1;lst1.push_back(10);lst1.push_back(20);lst1.push_back(30);lst1.push_back(40);PrintList(lst1);list<int> lst2(lst1.begin(), lst1.end());PrintList(lst2);list<int> lst3(lst2);PrintList(lst3);list<int> lst4(8, 100);PrintList(lst4);
}int main()
{Test();return 0;
}输出:
-----------------------------------------------------------------------------------------
10 20 30 40 
10 20 30 40
10 20 30 40
100 100 100 100 100 100 100 100

3.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 Test()
{list<int> lst1;lst1.push_back(10);lst1.push_back(20);lst1.push_back(30);lst1.push_back(40);PrintList(lst1);list<int> lst2;lst2 = lst1;PrintList(lst2);list<int> lst3;lst3.assign(lst2.begin(), lst2.end());PrintList(lst3);list<int> lst4;lst4.assign(5, 100);PrintList(lst4);
}void Test1()
{list<int> lst1;lst1.push_back(10);lst1.push_back(20);lst1.push_back(30);lst1.push_back(40);list<int> lst5;lst5.push_back(101);lst5.push_back(102);lst5.push_back(103);lst5.push_back(104);cout << "交换前: " << endl; PrintList(lst1);PrintList(lst5);lst5.swap(lst1);cout << "交换后: " << endl; PrintList(lst1);PrintList(lst5);
}int main()
{Test();cout << "---------------------------------------------------------" << endl;Test1();return 0;
}输出:
-----------------------------------------------------------------------------------------
10 20 30 40 
10 20 30 40
10 20 30 40
100 100 100 100 100
---------------------------------------------------------
交换前:
10 20 30 40
101 102 103 104
交换后:
101 102 103 104
10 20 30 40

3.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 Test()
{list<int> lst1;lst1.push_back(10);lst1.push_back(20);lst1.push_back(30);lst1.push_back(40);PrintList(lst1);if(lst1.empty()){cout << "lst1 is empty" << endl;}else{cout << "lst1 is not empty" << endl;cout << "size of lst1 = " << lst1.size() << endl;}lst1.resize(10);PrintList(lst1);lst1.resize(3);PrintList(lst1);lst1.resize(8, 100);PrintList(lst1);
}int main()
{Test();return 0;
}输出:
-----------------------------------------------------------------------------------------
10 20 30 40 
lst1 is not empty
size of lst1 = 4
10 20 30 40 0 0 0 0 0 0
10 20 30
10 20 30 100 100 100 100 100

3.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值匹配的元素

注意:

eraseinsert的输入必须为迭代器

#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 Test()
{list<int> lst1;lst1.push_back(10);lst1.push_back(20);lst1.push_back(30);lst1.push_back(40);lst1.push_front(100);lst1.push_front(200);lst1.push_front(300);lst1.push_front(400);PrintList(lst1);lst1.pop_back();PrintList(lst1);lst1.pop_front();PrintList(lst1);list<int>::iterator it = lst1.begin();lst1.insert(++++it, 1000);PrintList(lst1);it = lst1.begin();lst1.erase(++it);PrintList(lst1);lst1.push_back(10000);lst1.push_back(10000);lst1.push_back(10000);PrintList(lst1);lst1.remove(10000);PrintList(lst1);lst1.clear();PrintList(lst1);
}int main()
{Test();return 0;
}输出:
-----------------------------------------------------------------------------------------
400 300 200 100 10 20 30 40 
400 300 200 100 10 20 30
300 200 100 10 20 30
300 200 1000 100 10 20 30
300 1000 100 10 20 30
300 1000 100 10 20 30 10000 10000 10000
300 1000 100 10 20 30

3.6.6. list数据存取

功能描述:

  • 对list容器中数据进行存取

函数原型:

  • front(); //返回第一个元素。
  • back(); //返回最后一个元素。
#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 Test()
{list<int> lst1;lst1.push_back(10);lst1.push_back(20);lst1.push_back(30);lst1.push_back(40);// list的本质是链表,不是用连续的线性空间存储数据,迭代器不支持随机访问,所以不能使用[]和at()// lst1[0]        报错// lst1.at(0)     报错// 验证:迭代器不支持随机访问list<int>::iterator it = lst1.begin();// ++ 和 -- 都不报错,说明该容器的迭代器支持双向it++;cout << *it << endl;it--;cout << *it << endl;// it = it + 1;   报错,不支持随机访问cout << "front: " << lst1.front() << endl;cout << "back: " << lst1.back() << endl;
}int main()
{Test();return 0;
}输出:
-----------------------------------------------------------------------------------------
20
10
front: 10
back: 40

3.6.7. list反转和排序

功能描述:

将容器中的元素反转,以及将容器中的数据进行排序

函数原型:

  • lst.reverse(); //反转链表
  • lst.sort(); //链表排序

#include<iostream>
using namespace std;
#include<list>
#include<algorithm>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 v1, int v2)
{// 降序,就让第一个数大于第二个数return v1 > v2;
}void Test()
{list<int> lst1;lst1.push_back(20);lst1.push_back(10);lst1.push_back(50);lst1.push_back(90);PrintList(lst1);lst1.reverse();PrintList(lst1);// 所有不支持随机访问迭代器的容器,都不可以用标准算法,因为标准算法都是随机访问迭代器算法// 不支持随机访问迭代器的容器,内部会提供对应的算法// sort(lst1.begin(), lst1.end());    报错lst1.sort();       // 默认升序PrintList(lst1);lst1.sort(MyCompare);    // 改为降序PrintList(lst1);
}int main()
{Test();return 0;
}输出:
-----------------------------------------------------------------------------------------
20 10 50 90 
90 50 10 20
10 20 50 90
90 50 20 10

3.7. set/multiset容器

3.7.1. set基本概念

简介:

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

本质:

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

set和multiset的区别:

  • set不允许容器中有重复的元素
  • multiset允许容器中有重复的元素

补充:set容器的迭代器是不支持随机访问的双向迭代器

3.7.2. set构造和赋值

功能描述:创建set容器以及赋值

构造:

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

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

赋值:

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

#include<iostream>
using namespace std;
#include<set>
#include<algorithm>void PrintSet(set<int> &s)
{for(set<int>::iterator it = s.begin(); it != s.end(); it++){cout << *it << " ";}cout << endl;
}void Test()
{set<int> s1;// set容器插入数据,只有insert方式s1.insert(50);s1.insert(10);s1.insert(20);s1.insert(40);s1.insert(30);PrintSet(s1);set<int> s2(s1);PrintSet(s2);set<int> s3(s2.begin(), s2.end());PrintSet(s3);set<int> s4;s4 = s3;PrintSet(s4);
}int main()
{Test();return 0;
}输出:
-----------------------------------------------------------------------------------------
10 20 30 40 50 
10 20 30 40 50
10 20 30 40 50
10 20 30 40 50

3.7.3. set的大小和交换

功能描述:

  • 统计set容器大小以及交换set容器

函数原型:

  • size(); //返回容器中元素的数目
  • empty(); //判断容器是否为空
  • swap(st); //交换两个集合容器
#include<iostream>
using namespace std;
#include<set>
#include<algorithm>void PrintSet(set<int> &s)
{for(set<int>::iterator it = s.begin(); it != s.end(); it++){cout << *it << " ";}cout << endl;
}void Test()
{set<int> s1;// set容器插入数据,只有insert方式s1.insert(50);s1.insert(10);s1.insert(20);s1.insert(40);s1.insert(30);PrintSet(s1);if(s1.empty()){cout << "s1 is empty" << endl;}else{cout << "s1 is not empty" << endl;cout << "s1的大小为:" << s1.size() << endl;}set<int> s2;s2.insert(10);s2.insert(20);s2.insert(30);// 合并两个set容器s1.swap(s2);cout << "交换后:" << endl;PrintSet(s1);PrintSet(s2);
}int main()
{Test();return 0;
}输出:
-----------------------------------------------------------------------------------------
10 20 30 40 50 
s1 is not empty
s1的大小为:5
交换后:
10 20 30
10 20 30 40 50

3.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>
#include<algorithm>void PrintSet(set<int> &s)
{for(set<int>::iterator it = s.begin(); it != s.end(); it++){cout << *it << " ";}cout << endl;
}void Test()
{set<int> s1;// set容器插入数据,只有insert方式s1.insert(50);s1.insert(10);s1.insert(20);s1.insert(40);s1.insert(30);PrintSet(s1);s1.erase(s1.begin());PrintSet(s1);s1.erase(30);PrintSet(s1);// s1.erase(s1.begin(), s1.end());s1.clear();PrintSet(s1);
}int main()
{Test();return 0;
}输出:
-----------------------------------------------------------------------------------------
10 20 30 40 50 
20 30 40 50
20 40 50

3.7.5. set查找和统计

功能描述:

  • 对set容器进行查找数据以及统计数据

函数原型:

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

count(key); //统计key的元素个数,对于set而言,统计结果为0或1

#include<iostream>
using namespace std;
#include<set>
#include<algorithm>void PrintSet(set<int> &s)
{for(set<int>::iterator it = s.begin(); it != s.end(); it++){cout << *it << " ";}cout << endl;
}void Test()
{set<int> s1;// set容器插入数据,只有insert方式s1.insert(50);s1.insert(10);s1.insert(20);s1.insert(40);s1.insert(30);PrintSet(s1);set<int>::iterator pos = s1.find(50);if(pos != s1.end()){cout << "找到了元素:" << *pos << endl;}else{cout << "未找到元素" << endl;}int num = s1.count(30);cout << "30出现了" << num << "次" << endl;
}int main()
{Test();return 0;
}输出:
-----------------------------------------------------------------------------------------
10 20 30 40 50 
找到了元素:50
30出现了1次

3.7.6. set和multiset区别

学习目标:

  • 掌握setmultiset的区别

区别:

  • set不可以插入重复数据,而multiset可以
  • set插入数据的同时会返回插入结果,表示插入是否成功
  • multiset不会检测数据,因此可以插入重复数据
#include<iostream>
using namespace std;
#include<set>
#include<algorithm>void PrintSet(set<int> &s)
{for(set<int>::iterator it = s.begin(); it != s.end(); it++){cout << *it << " ";}cout << endl;
}void Test()
{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> s2;s2.insert(10);s2.insert(10);s2.insert(10);for(multiset<int>::iterator it = s2.begin(); it != s2.end(); it++){cout << *it << " ";}cout << endl;
}int main()
{Test();return 0;
}输出:
-----------------------------------------------------------------------------------------
插入成功
插入失败
10 10 10

3.7.7. pair对组创建

功能描述:

  • 成对出现的数据,利用对组可以返回两个数据

两种创建方式:

  • pair<type,type> p ( value1,value2 );
  • pair<type,type> p = make pair( value1,value2 );
#include<iostream>
using namespace std;
#include<set>
#include<algorithm>void Test()
{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()
{Test();return 0;
}输出:
-----------------------------------------------------------------------------------------
姓名:Tom 年龄:20
姓名:Jerry 年龄:30

3.7.8. set容器排序

学习目标:

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

主要技术点:

  • 利用仿函数(重载函数调用运算符,返回值为bool类型 ---()),可以改变排序规则

示例一:set容器存放内置数据类型

#include<iostream>
using namespace std;
#include<set>
#include<algorithm>class MyCompare
{
public:bool operator()(int v1, int v2){   return v1 > v2;}
};void Test()
{set<int> s1;s1.insert(10);s1.insert(40);s1.insert(30);s1.insert(20);s1.insert(50);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(30);s2.insert(20);s2.insert(50);for(set<int,MyCompare>::iterator it = s2.begin(); it != s2.end(); it++){cout << *it << " ";}
}int main()
{Test();return 0;
}输出:
-----------------------------------------------------------------------------------------
10 20 30 40 50 
50 40 30 20 10

示例二:set容器存放自定义的数据类型

#include<iostream>
using namespace std;
#include<set>
#include<algorithm>class Person
{
public:Person(string name,int age){this->m_Name = name;this->m_Age = age;}string m_Name;int m_Age;
};class MyCompare
{
public:bool operator()(Person v1, Person v2){return v1.m_Age > v2.m_Age;}
};void Test()
{// 自定义的数据类型,要指定排序规则set<Person, MyCompare> 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, MyCompare>::iterator it = s.begin(); it != s.end(); it++){cout << "姓名:" << it->m_Name << " 年龄:" << it->m_Age << endl;}
}int main()
{Test();return 0;
}输出:
-----------------------------------------------------------------------------------------
姓名:关羽 年龄:28
姓名:张飞 年龄:25
姓名:刘备 年龄:24
姓名:赵云 年龄:21

3.8. map/multmap容器

3.8.1. map基本概念

简介:

  • map中所有元素都是pair对组
  • pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)
  • 所有元素都会根据元素的键值key自动排序

本质:

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

优点:

  • 可以根据key值快速找到value

mapmultimap区别:

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

补充:map容器与set容器的的迭代器类型一致,都是不支持随机访问的双向迭代器

3.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>
#include<algorithm>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 Test()
{// 自定义的数据类型,要指定排序规则map<int, int> m;m.insert(pair<int, int>(1, 10));m.insert(pair<int, int>(2, 20));m.insert(pair<int, int>(4, 40));m.insert(pair<int, int>(3, 30));PrintMap(m);map<int, int> m2(m);PrintMap(m2);map<int, int> m3(m.begin(), m.end());PrintMap(m3);map<int, int> m4;m4 = m;PrintMap(m4);
}int main()
{Test();return 0;
}输出:
-----------------------------------------------------------------------------------------
key = 1 value = 10
key = 2 value = 20
key = 3 value = 30
key = 4 value = 40key = 1 value = 10
key = 2 value = 20
key = 3 value = 30
key = 4 value = 40key = 1 value = 10
key = 2 value = 20
key = 3 value = 30
key = 4 value = 40key = 1 value = 10
key = 2 value = 20
key = 3 value = 30
key = 4 value = 40

3.8.3. map大小和交换

功能描述:

  • 统计map容器大小以及交换map容器。

函数原型:

  • size(); //返回容器中元素的数目
  • empty(); //判断容器是否为空
  • swap(st); //交换两个集合容器
#include<iostream>
using namespace std;
#include<map>
#include<algorithm>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 Test()
{// 自定义的数据类型,要指定排序规则map<int, int> m;m.insert(pair<int, int>(1, 10));m.insert(pair<int, int>(2, 20));m.insert(pair<int, int>(4, 40));m.insert(pair<int, int>(3, 30));if(m.empty()){cout << "map is empty" << endl;}else{cout << "map is not empty" << endl;cout << "size = " << m.size() << endl;PrintMap(m);}map<int, int> m2;cout << "交换后:" << endl;m2.insert(pair<int, int>(5, 4890));m2.insert(pair<int, int>(7, 1544));m2.insert(pair<int, int>(6, 1186));m2.swap(m);PrintMap(m);PrintMap(m2);
}int main()
{Test();return 0;
}输出:
-----------------------------------------------------------------------------------------
map is not empty
size = 4
key = 1 value = 10
key = 2 value = 20
key = 3 value = 30
key = 4 value = 40交换后
key = 5 value = 4890
key = 6 value = 1186
key = 7 value = 1544key = 1 value = 10
key = 2 value = 20
key = 3 value = 30
key = 4 value = 40

3.8.4. map插入和删除

功能描述:

  • map容器进行插入数据和删除数据

函数原型:

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

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

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

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

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

#include<iostream>
using namespace std;
#include<map>
#include<algorithm>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 Test()
{// 自定义的数据类型,要指定排序规则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));// 第四种插入方法(虽然简单,但不建议使用,可以通过这种方法用key来访问对应的value)m[4] = 40;cout << m[5] << endl;PrintMap(m);m.erase(m.begin());PrintMap(m);m.erase(3);    PrintMap(m);// m.clear();m.erase(m.begin(), m.end());PrintMap(m);
}int main()
{Test();return 0;
}输出:
0
key = 1 value = 10
key = 2 value = 20
key = 3 value = 30
key = 4 value = 40
key = 5 value = 0
----------------------------------------------
key = 2 value = 20
key = 3 value = 30
key = 4 value = 40
key = 5 value = 0
----------------------------------------------
key = 2 value = 20
key = 4 value = 40
key = 5 value = 0
----------------------------------------------
----------------------------------------------

3.8.5. map查找和统计

功能描述:

  • map容器进行查找数据以及统计数据

函数原型:

  • find(key); //查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end()
  • count(key); //统计key的元素个数
#include<iostream>
using namespace std;
#include<map>
#include<algorithm>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 Test()
{// 自定义的数据类型,要指定排序规则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));map<int, int>::iterator pos = m.find(3);if (pos != m.end()){cout << "key = " << pos->first << " value = " << pos->second << endl;}else{cout << "没有找到..." << endl;}cout << "num = " << m.count(2) << endl;
}int main()
{Test();return 0;
}输出:
-----------------------------------------------------------------------------------------
key = 3 value = 30
num = 1

3.8.6. map容器排序

学习目标:

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

主要技术点:

  • 利用仿函数,可以改变排序规则
#include<iostream>
using namespace std;
#include<map>
#include<algorithm>class MyCompare
{
public:bool operator()(int v1, int v2){return v1 > v2;}
};void PrintMap(map<int, int, MyCompare> m)
{for(map<int, int, MyCompare>::iterator it = m.begin(); it != m.end(); it++){cout << "key = " << it->first << " value = " << it->second << endl;}
}void Test()
{// 自定义的数据类型,要指定排序规则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));PrintMap(m); 
}int main()
{Test();return 0;
}输出:
-----------------------------------------------------------------------------------------
key = 5 value = 50
key = 4 value = 40
key = 3 value = 30
key = 2 value = 20
key = 1 value = 10

相关文章:

  • Elastic 索引结构-倒排索引
  • 【AI绘画】Stable Diffusion 3开源
  • Linux3(进程 编辑文件 用户管理 网络)
  • C#A类调用B类的方法,在方法中更新B类的控件
  • c#中上传超过30mb的文件,接口一直报404,小于30mb的却可以上传成功
  • Java多线程-StampedLock(原子读写锁)
  • 小学生期刊知网//《小学生》评职称认可吗?
  • 笔记-前端
  • Android 列表视频滑动自动播放(实现思路)
  • 如何舒适的使用VScode
  • win10 双显卡,双显示器,VGA那个经常出现息屏(待机后无法唤醒),必须重启才能解决,(图文)手把手教你如何处理简单愉快的解决。
  • WinForm之TCP服务端
  • Centos上部署Node服务和MongoDB
  • 【CT】LeetCode手撕—200. 岛屿数量
  • Windows中LoadLibrary加载动态库失败,详细解释(解决思路)
  • [译]Python中的类属性与实例属性的区别
  • 【跃迁之路】【585天】程序员高效学习方法论探索系列(实验阶段342-2018.09.13)...
  • 2017-09-12 前端日报
  • 2017年终总结、随想
  • niucms就是以城市为分割单位,在上面 小区/乡村/同城论坛+58+团购
  • Object.assign方法不能实现深复制
  • Python语法速览与机器学习开发环境搭建
  • Vue官网教程学习过程中值得记录的一些事情
  • Zsh 开发指南(第十四篇 文件读写)
  • 每个JavaScript开发人员应阅读的书【1】 - JavaScript: The Good Parts
  • 让你的分享飞起来——极光推出社会化分享组件
  • 如何打造100亿SDK累计覆盖量的大数据系统
  • 如何进阶一名有竞争力的程序员?
  • 深入浏览器事件循环的本质
  • 王永庆:技术创新改变教育未来
  • 微信端页面使用-webkit-box和绝对定位时,元素上移的问题
  • 一起来学SpringBoot | 第十篇:使用Spring Cache集成Redis
  • 做一名精致的JavaScripter 01:JavaScript简介
  • 格斗健身潮牌24KiCK获近千万Pre-A轮融资,用户留存高达9个月 ...
  • 进程与线程(三)——进程/线程间通信
  • 我们雇佣了一只大猴子...
  • ​ 轻量应用服务器:亚马逊云科技打造全球领先的云计算解决方案
  • ​香农与信息论三大定律
  • #define 用法
  • (BAT向)Java岗常问高频面试汇总:MyBatis 微服务 Spring 分布式 MySQL等(1)
  • (C语言版)链表(三)——实现双向链表创建、删除、插入、释放内存等简单操作...
  • (二)Linux——Linux常用指令
  • (三) prometheus + grafana + alertmanager 配置Redis监控
  • (一)appium-desktop定位元素原理
  • (一)Mocha源码阅读: 项目结构及命令行启动
  • *p=a是把a的值赋给p,p=a是把a的地址赋给p。
  • .apk 成为历史!
  • .bat批处理(十一):替换字符串中包含百分号%的子串
  • .Mobi域名介绍
  • .naturalWidth 和naturalHeight属性,
  • .net CHARTING图表控件下载地址
  • .NET 漏洞分析 | 某ERP系统存在SQL注入
  • .NET/C# 解压 Zip 文件时出现异常:System.IO.InvalidDataException: 找不到中央目录结尾记录。
  • .NET面试题解析(11)-SQL语言基础及数据库基本原理
  • @requestBody写与不写的情况