• 黑马C++ 03 提高4 —— STL常用容器_string容器/vector容器/deque容器


    一、string容器

    1. string基本概念

    本质:string是C++风格的字符串,而string本质上是一个类
    string和char * 区别:

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

    特点:

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

    2. string构造函数

    构造函数原型:

    • string(); 创建一个空的字符串 例如: string str
    • string(const char* s); 使用字符串s初始化
    • string(const string& str); 使用一个string对象初始化另一个string对象
    • string(int n, char c); 使用n个字符c初始化
    #include
    #include//算法头文件
    #include
    #include
    using namespace std;
    
    /*
    string是C++风格的字符串,而string本质上是一个类
    string和char *区别:
    1、char* 是一个指针
    2、string是一个类,类内部封装了char* ,管理这个字符串,是一个char* 型的容器
    */
    
    //string构造函数
    /*
    string();         		无参,默认构造。创建一个空的字符串 例如: string str;
    string(const char* s); 	     传入C语言的字符串构造C++字符串,使用字符串s初始化
    string(const string& str);	 拷贝构造,使用一个string对象初始化另一个string对象
    string(int n, char c);       使用n个字符c初始化
    */
    
    void test01()
    {
    	// 默认构造
    	string s1;
    
    	// 用C语言的字符串初始化C++的string
    	const char* str = "hello world";
    	string s2(str);
    	cout << "s2 = " << s2 << endl;
    
    	// 拷贝构造
    	string s3(s2);
    	cout << "s3 = " << s3 << endl;
    
    	// n个字符串
    	string s4(10, 'a');
    	cout << "s4 = " << s4 << endl;
    }
    
    int main()
    {
    	test01();
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    在这里插入图片描述

    • 总结:string的多种构造方式没有可比性,灵活使用即可

    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赋给当前字符串

    总结:string的赋值方式很多,operator= 这种方式是比较实用的

    #include
    #include// 算法头文件
    #include
    #include
    using namespace std;
    /*
    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赋给当前字符串
    */
    
    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;// 使用assign函数进行赋值
    	str4.assign("hello world");// 把字符串s赋给当前的字符串
    	cout << "str4 = " << str4 << endl;
    
    	string str5;
    	str5.assign("hello world", 5);// 拷贝前5个字符
    	cout << "str5 = " << str5 << endl;
    
    	string str6;
    	str6.assign(str5);
    	cout << "str6 = " << str6 << endl;
    
    	string str7;
    	str7.assign(10, 'w');// (int n, char c)n个字符c赋给当前字符串
    	cout << "str7 = " << str7 << endl;//wwwwwwwww
    }
    
    int main()
    {
    	test01();
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    在这里插入图片描述

    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); 把字符串s的前n个字符连接到当前字符串结尾
    • string& append(const string &s); 同operator+=(const string& str)
    • string& append(const string &s, int pos, int n); 字符串s中从pos开始的n个字符连接到字符串结尾
    #include
    #include//算法头文件
    #include
    #include
    using namespace std;
    
    //字符串拼接
    /*
    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);                 把字符串s的前n个字符连接到当前字符串结尾
    string& append(const string& s);                      同operator+=(const string& str)
    string& append(const string& s, int pos, int n);      字符串s中从pos开始的n个字符连接到字符串结尾
    */
    
    void test01()
    {
    	string str1 = "我";
    	str1 += "爱玩游戏";
    	cout << "str1 = " << str1 << endl;// 我爱完游戏
    
    	str1 += ':';
    	cout << "str1 = " << str1 << endl;// 我爱完游戏:
    
    	string str2 = "LOL DNF";
    	str1 += str2;
    	cout << "str1 = " << str1 << endl;// 我爱完游戏:LOL DNF
    
    	string str3 = "I";
    	str3.append(" love ");
    	cout << "str3 = " << str3 << endl;// I love
    
    	str3.append("game abcde", 4);// 把前4个字符拼接到尾处
    	cout << "str3 = " << str3 << endl;// I love game
    
    	str3.append(str2);
    	cout << "str3 = " << str3 << endl;// I love gameLOL DNF
    
    	// 参数2表示从哪个位置开始截取,参数3表示截取字符的个数
    	str3.append(str2, 0, 3);// 只截取到LOL
    	cout << "str3 = " << str3 << endl;// I love gameLOL DNFLOL
    	str3.append(str2, 4, 6);// 只截取到DNF
    	cout << "str3 = " << str3 << endl;// I love gameLOL DNFLOLNDF
    }
    
    int main()
    {
    	test01();
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    在这里插入图片描述

    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 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
    #include
    #include
    #include
    using namespace std;
    
    //字符串的查找和替换
    /*
    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(c++格式)
    string& replace(int pos, int n, const char* s);               替换从pos开始n个字符为字符串s(c格式)
    */
    
    // 1.查找
    void test01()
    {
    	string str1 = "abcdefg";
    	int pos = str1.find("de");
    	if (pos == -1)
    	{
    		cout << "未找到字符串" << endl;
    	}
    	else
    	{
    		cout << "找到字符串,pos = " << pos << endl;//pos=3 
    	}
    
    	// rfind与find的区别:rfind从右往左查找,find是从左往右查找
    	pos = str1.rfind("de");
    	cout << "pos = " << pos << endl;
    }
    
    // 2.替换
    void test02()
    {
    	string str1 = "abcdefg";
    	// 从pos开始n个字符为字符串str
    	// 从1号位置起(从0开始算,b开始用替代),3个字符替换为"1111"
    	str1.replace(1, 3, "1111");
    	cout << "str1=" << str1 << endl;
    }
    
    int main()
    {
    	test01();
    	test02();
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    在这里插入图片描述

    6. string字符串比较

    功能描述:

    • 字符串之间的比较

    比较方式:

    • 字符串比较是按字符的ASCII码进行对比

    = 返回 0

    > 返回 1

    < 返回 -1

    函数原型:

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

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

    • 总结:字符串对比主要是用于比较两个字符串是否相等,判断谁大谁小的意义并不是很大

    #include
    #include
    #include
    #include
    using namespace std;
    
    //字符串比较:根据ASCII码一个一个顺序比较
    
    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();
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    7. string字符存取

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

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

    总结

    • string字符串中单个字符存取有两种方式,利用 [ ] 或 at
    #include
    #include
    #include
    using namespace std;
    
    //字符存取
    
    void test01()
    {
    	string str = "hello";
    	cout << "str = " << str << endl;
    
    	// 1.通过[]访问单个字符
    	for (int i = 0; i < str.size(); i++)
    	{
    		cout << str[i] << " ";
    	}
    	cout << endl;
    
    	// 2.通过at方式访问单个字符
    	for (int i = 0; i < str.size(); i++)
    	{
    		cout << str.at(i) << " ";
    	}
    	cout << endl;
    
    	// 修改单个字符
    	str[0] = 'x';
    	cout << "str = " << str << endl;// xello
    
    	str.at(1) = 'x';
    	cout << "str = " << str << endl;// xxllo
    }
    
    int main()
    {
    	test01();
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    在这里插入图片描述

    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个字符

    总结

    • 插入和删除的起始下标都是从0开始
    #include
    #include
    #include
    using namespace std;
    
    // 字符串的插入和删除
    void test01()
    {
    	string str = "hello";
    
    	// 插入
    	str.insert(1, "111");
    	cout << "插入后str = " << str << endl;// h111ello
    
    	// 删除
    	str.erase(1, 3);// 从第1个位置起,删除3个
    	cout << "删除后str = " << str << endl;// hello
    }
    
    int main()
    {
    	test01();
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    在这里插入图片描述

    9. string子串

    功能描述:

    • 从字符串中获取想要的子串

    函数原型:

    • string substr(int pos = 0, int n = npos) const; 返回由pos开始的n个字符组成的字符串
    #include
    #include
    #include
    using namespace std;
    
    // string求子串
    void test01()
    {
    	string str = "abcdef";
    	string subStr = str.substr(1, 3);// bcd
    	cout << "subStr = " << subStr << endl;
    }
    
    // 实用操作
    void test02()
    {
    	string email = "zhangsan@sina.com";
    	// 从邮件的地址中,获取用户名的信息
    	int pos = email.find("@");// pos=8
    	cout << "pos = " << pos << endl;
    
    	string userName = email.substr(0, pos);// 从第0个开始,截取8个字符
    	cout << "userName = " << userName << endl;
    }
    
    int main()
    {
    	test01();
    	test02();
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    在这里插入图片描述

    二、vector容器(尾插尾删)

    1. vector基本概念

    功能:

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

    vector与普通数组区别:

    • 不同之处在于数组是静态空间,而vector可以动态扩展

    动态扩展:

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

    在这里插入图片描述

    • vector容器的迭代器是支持随机访问的迭代器
    • 尾插和尾删,常用v.begin()和v.end()。

    2. vector构造函数

    功能描述:

    • 创建vector容器

    函数原型:

    • vector v; 采用模板实现类实现,默认构造函数
    • vector(v.begin(), v.end()); 将v[begin(), end())区间中的元素拷贝给本身,前闭后开
    • vector(n, elem); 构造函数将n个elem拷贝给本身。
    • vector(const vector &vec); 拷贝构造函数。
    #include
    #include
    #include
    #include
    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);
    
    	// 通过区间的方式进行构造,end值取不到
    	vector<int>v2(v1.begin(), v1.end());
    	printVector(v2);
    
    	// n个elem方式构造
    	vector<int>v3(10, 100);// 100 100 100 100 100 100 100 100 100 100
    	printVector(v3);
    
    	// 拷贝构造
    	vector<int>v4(v3);
    	printVector(v4);// 100 100 100 100 100 100 100 100 100 100
    }
    
    int main()
    {
    	test01();
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    在这里插入图片描述

    3. vector赋值操作

    功能描述:

    • 给vector容器进行赋值

    函数原型:

    • vector& operator=(const vector &vec); 重载等号操作符
    • assign(beg, end); 将[beg, end)区间中的数据拷贝赋值给本身。
    • assign(n, elem); 将n个elem拷贝赋值给本身
    #include
    #include
    #include
    #include
    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);
    
    	// 赋值 operator = 等号赋值
    	vector<int>v2;
    	v2 = v1;
    	printVector(v2);
    
    	// assign
    	vector<int>v3;
    	v3.assign(v1.begin(), v1.end());
    	printVector(v3);
    
    	// n个elem方式赋值
    	vector<int>v4;
    	v4.assign(10, 100);
    	printVector(v4);
    }
    
    int main()
    {
    	test01();
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    在这里插入图片描述

    4. vector容量与大小

    功能描述:

    • 对vector容器的容量和大小操作

    函数原型:

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

    总结:

    • 判断是否为空 — empty
    • 返回元素个数 — size
    • 返回容器容量 — capacity
    • 重新指定大小 — resize
    #include
    #include
    #include
    #include
    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);
    
    	if (v1.empty()) // 为真代表容器为空
    	{
    		cout << "v1为空" << endl;
    	}
    	else
    	{
    		cout << "v1不为空" << endl;
    		cout << "v1的容量为:" << v1.capacity() << endl;// capacity >= size
    		cout << "v1的大小为:" << v1.size() << endl; // v1的大小为10,容量为13
    	}
    
    	// 重新指定大小
    	// v1.resize(15);
    	// printVector(v1);// 如果重新指定的比原来长了,默认用0填充新的位置
    	v1.resize(15, 100);// 利用重载的版本,可以指定参数2为默认的填充值
    	printVector(v1);
    
    	v1.resize(5);
    	printVector(v1);// 如果重新指定的比原来短了,超出部分会删除掉
    }
    
    int main()
    {
    	test01();
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    在这里插入图片描述

    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
    #include
    #include
    #include
    using namespace std;
    
    // 插入和删除
    /*
    push_back(ele);                                     尾部插入元素ele
    pop_back();                                         删除最后一个元素
    insert(const_iterator pos, ele);                    迭代器指向位置pos插入元素ele
    insert(const_iterator pos, int count, ele);         迭代器指向位置pos插入count个元素eleerase(const_iterator pos);                      //删除迭代器指向的元素
    erase(const_iterator start, const_iterator end);    删除迭代器从start到end之间的元素
    clear();                                            删除容器中所有元素
    */
    
    // 打印函数
    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);// 50被删除,10 20 30 40
    
    	// 插入insert 第一个参数是迭代器
    	v1.insert(v1.begin(), 100);
    	printVector(v1);// 100 10 20 30 40
    
    	v1.insert(v1.begin(), 2, 1000);// 在开头多了两个1000
    	printVector(v1);// 1000 1000 100 10 20 30 40 
    
    	// 删除erase 参数也是迭代器
    	v1.erase(v1.begin());
    	printVector(v1);// 1000 100 10 20 30 40 
    
    	v1.erase(v1.begin(), v1.end());// 清空 同v1.clear()
    	printVector(v1);
    }
    
    int main()
    {
    	test01();
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64

    在这里插入图片描述

    6. vector数据存取

    功能描述:

    • 对vector中的数据的存取操作

    函数原型:

    • at(int idx); 返回索引idx所指的数据
    • operator[]; 返回索引idx所指的数据
    • front(); 返回容器中第一个数据元素
    • back(); 返回容器中最后一个数据元素
    #include
    #include
    #include
    #include
    using namespace std;
    
    //vector容器 数据存取
    
    void test01()
    {
    	vector<int>v1;
    	for (int i = 0; i < 10; i++)
    	{
    		v1.push_back(i);
    	}
    
    	// 利用[]方式访问数组中的元素
    	cout << "[]方式访问:";
    	for (int i = 0; i < 10; i++)
    	{
    		cout << v1[i] << " ";
    	}
    	cout << endl;
    
    	// 利用at方式访问元素
    	cout << "at方式访问:";
    	for (int i = 0; i < 10; i++)
    	{
    		cout << v1.at(i) << " ";
    	}
    	cout << endl;
    
    	// 获取第一个元素
    	cout << "第一个元素为:" << v1.front() << endl;
    
    	// 获取最后的一个元素为
    	cout << "最后一个元素为:" << v1.back() << endl;
    }
    
    int main()
    {
    	test01();
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    在这里插入图片描述

    7. vector互换容器

    功能描述:

    • 实现两个容器内元素进行互换

    函数原型:

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

    总结:

    • swap可以使两个容器互换,可以达到实用的收缩内存效果
    • 用v的大小初始化匿名对象的大小,然后互换空间,交换容器
    • vector(v)表示初始化和v大小相同的匿名对象
      在这里插入图片描述
    #include
    #include
    #include
    #include
    using namespace std;
    
    // vector容器互换
    
    // 打印函数
    void printVector(vector<int>& v)
    {
    	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    	{
    		cout << *it << " ";
    	}
    	cout << endl;
    }
    
    // 1.基本使用
    void test01()
    {
    	cout << "交换前: " << endl;
    
    	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);// 10 9 8 7 6 5 4 3 2 1
    	}
    	printVector(v2);
    
    	cout << "交换后: " << endl;
    	v1.swap(v2);
    	printVector(v1);// 10 9 8 7 6 5 4 3 2 1
    	printVector(v2);// 0 1 2 3 4 5 6 7 8 9
    }
    
    // 2.实际用途
    // 巧用swap可以收缩内存空间
    void test02()
    {
    	vector<int>v;
    	for (int i = 0; i < 100000; i++)
    	{
    		v.push_back(i);
    	}
    	cout << "v的容量为: " << v.capacity() << endl;// 138255
    	cout << "v的大小为: " << v.size() << endl;// 10000
    
    	v.resize(3);// 重新指定大小
    	cout << "v的容量为: " << v.capacity() << endl;// 138255
    	cout << "v的大小为: " << v.size() << endl;// 3
    
    	// 巧用swap收缩内存, 不会浪费内存
    	vector<int>(v).swap(v);// (v):用v的大小初始化匿名对象的大小,然后互换空间
    	cout << "v的容量为: " << v.capacity() << endl;// 3
    	cout << "v的大小为: " << v.size() << endl;// 3
    }
    
    int main()
    {
    	test01();
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71

    在这里插入图片描述

    8. vector预留空间

    功能描述:

    • 减少vector在动态扩展容量时的扩展次数

    函数原型:

    • reserve(int len); 容器预留len个元素长度,预留位置不初始化,元素不可访问
      在这里插入图片描述
    • 每次分配的内存满后就会重新分配内存,p指向新分配的内存的首地址。使用reserve预留空间
    #include
    #include
    #include
    using namespace std;
    
    // vector预留空间
    void test01()
    {
    	vector<int>v;
    	// 利用reserve预留空间,这样就不用开辟30次空间
    	v.reserve(100000);
    
    	int num = 0;// 统计开辟内存的次数
    	int* p = NULL;
    	for (int i = 0; i < 10000; i++)
    	{
    		v.push_back(i);
    		if (p != &v[0])// 如果p不等于首地址
    		{
    			p = &v[0];
    			num++;
    		}
    	}
    	cout << "num = " << num << endl;// 1, 分配1次内存,因为先预留了10000个内存空间
    	// v 中收元素地址变化次数,就是v在内存中的分配次数。
    	// 每次分配的内存满后就会重新分配内存,p指向新分配的内存的首地址
    }
    
    int main()
    {
    	test01();
    	system("pause"); 
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    三、deque容器(双端数组)

    1. deque基本概念

    功能:

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

    deque与vector区别:

    • vector对于头部的插入删除效率低,数据量越大,效率越低
    • deque相对而言,对头部的插入删除速度回比vector快
    • vector访问元素时的速度会比deque快,这和两者内部实现有关
    • 头部尾部的插入删除快,但访问数据效率低,因为需要重新找缓存区的地址,然后再进行访问
      在这里插入图片描述

    在这里插入图片描述
    deque内部工作原理:

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

    在这里插入图片描述

    • deque容器的迭代器也是支持随机访问的

    2. deque构造函数(必须使用const只读迭代器)

    功能描述:

    • deque容器构造

    函数原型:

    • deque deqT; 默认构造形式
    • deque(beg, end); 构造函数将[beg, end)区间中的元素拷贝给本身。
    • deque(n, elem); 构造函数将n个elem拷贝给本身。
    • deque(const deque &deq); 拷贝构造函数
    #include
    #include
    #include
    using namespace std;
    
    // deque构造函数
    
    // 只读迭代器
    void printDeque(const deque<int>& d)// const只读,引用方式传递进来
    {
    	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
    	{
    		// *it=100; 容器中的数据不可以修改
    		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);// 10个100
    	printDeque(d3);
    
    	deque<int>d4(d3);// 拷贝构造
    	printDeque(d4);
    }
    
    int main()
    {
    	test01();
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    在这里插入图片描述

    3. deque赋值操作

    功能描述:

    • 给deque容器进行赋值

    函数原型:

    • deque& operator=(const deque &deq); 重载等号操作符
    • assign(beg, end); 将[beg, end)区间中的数据拷贝赋值给本身。
    • assign(n, elem); 将n个elem拷贝赋值给本身。
    #include
    #include
    #include
    using namespace std;
    
    // deque容器赋值操作
    void printDeque(const deque<int>& d)// const只读,只读迭代器
    {
    	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);
    
    	// operator=赋值
    	deque<int>d2;
    	d2 = d1;
    	printDeque(d2);
    
    	// assign赋值
    	deque<int>d3;
    	d3.assign(d1.begin(), d1.end());
    	printDeque(d3);
    
    	// 将n个elem拷贝赋值给本身
    	deque<int>(d4);
    	d4.assign(10, 100);
    	printDeque(d4);
    }
    
    int main()
    {
    	test01();
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    在这里插入图片描述

    4. deque大小操作(没有容量说法只有个数)

    功能描述:

    • 对deque容器的大小进行操作

    函数原型:

    • deque.empty(); 判断容器是否为空
    • deque.size(); 返回容器中元素的个数
    • deque.resize(num); 重新指定容器的长度为num,若容器变长,则以默认值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除
    • deque.resize(num, elem); 重新指定容器的长度为num,若容器变长,则以elem值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除
    #include
    #include
    #include
    using namespace std;
    
    // deque容器大小操作
    void printDeque(const deque<int>& d)// const只读
    {
    	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;
    		// 个数就是大小,deque容器没有容量概念,可以无限放
    	}
    
    	// 重新指定大小
    	// d1.resize(15);重新指定容器的长度为num,若容器变长,则以默认值填充新位置
    	// deque.resize(num, elem);重新指定容器的长度为num,若容器变长,则以elem值填充新位置
        // 如果容器变短,则末尾超出容器长度的元素被删除
    	d1.resize(15, 1);// 超出部分为1
    	printDeque(d1);
    
    	d1.resize(5);
    	printDeque(d1);
    }
    
    int main()
    {
    	test01();
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    在这里插入图片描述

    5. deque插入和删除

    功能描述:

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

    函数原型
    两端插入操作:

    • 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位置的数据,返回下一个数据的位置。

    总结:

    • 插入和删除提供的位置是迭代器!
    • 尾插 — push_back
    • 尾删 — pop_back
    • 头插 — push_front
    • 头删 — pop_front
    #include
    #include
    #include
    using namespace std;
    
    // deque插入和删除
    // 打印函数
    void printDeque(const deque<int>& d)// const只读
    {
    	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);
    	printDeque(d1);// 10 20
    
    	// 头插
    	d1.push_front(100);
    	d1.push_front(200);
    	printDeque(d1);// 200 100 10 20
    
    	// 尾删
    	d1.pop_back();
    	printDeque(d1);// 200 100 10
    
    	// 头删
    	d1.pop_front();
    	printDeque(d1);// 100 10
    }
    
    void test02()
    {
    	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
    
    	// insert方式插入
    	d1.insert(d1.begin(), 1000);
    	printDeque(d1);// 1000 200 100 10 20
    
    	d1.insert(d1.begin(), 2, 10000);
    	printDeque(d1);// 10000 10000 1000 200 100 10 20
    
    	// 按照区间进行插入
    	deque<int>d2;
    	d2.push_back(1);
    	d2.push_back(2);
    	d2.push_back(3);
    
    	d1.insert(d1.begin(), d2.begin(), d2.end());// 在d1的begin位置插入d2
    	printDeque(d1);// 1 2 3 10000 10000 1000 200 100 10 20
    }
    
    void test03()
    {
    	deque<int>d1;
    	d1.push_back(10);
    	d1.push_back(20);
    	d1.push_front(100);
    	d1.push_front(200);// 200 100 10 20
    
    	// 删除
    	deque<int>::iterator it = d1.begin();// 方便删除其他地方的值
    	it++;
    	d1.erase(it);// 删除第2个元素
    	printDeque(d1);// 200 10 20; 删除100
    
    	// 按照区间的方式删除
    	d1.erase(d1.begin(), d1.end());// 本质就是清空,同clear
    
    	// 清空
    	d1.clear();
    	printDeque(d1);
    }
    
    int main()
    {
    	test01();
    	cout << endl;
    	test02();
    	cout << endl;
    	test03();
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97

    在这里插入图片描述

    6. deque数据存取

    功能描述:

    • 对deque 中的数据的存取操作

    函数原型:

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

    总结:

    • 除了用迭代器获取deque容器中元素,[ ]和at也可以
    • front返回容器第一个元素
    • back返回容器最后一个元素
    #include
    #include
    using namespace std;
    
    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);
    
    	// 通过[]方式访问元素
    	// 300 200 100 10 20 30 
    	for (int i = 0; i < d.size(); i++)
    	{
    		cout << d[i] << " ";
    	}
    	cout << endl;
    
    	// 通过at方式访问元素
    	for (int i = 0; i < d.size(); i++)
    	{
    		cout << d.at(i) << " ";
    	}
    	cout << endl;
    
    	cout << "第一个元素为:" << d.front() << endl;
    	cout << "最后一个元素为:" << d.back() << endl;
    }
    
    int main()
    {
    	test01();
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    在这里插入图片描述

    7. deque排序

    功能描述:

    • 利用算法实现对deque容器进行排序

    算法:

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

    总结:

    • sort算法非常实用,使用时包含头文件 algorithm即可
    #include
    #include
    #include
    using namespace std;
    
    //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_back(30);
    	d.push_front(100);
    	d.push_front(200);
    	d.push_front(300);
    	cout << "排序前:" << endl;
    	printDeque(d);// 300 200 100 10 20 30
    
    	// 排序 默认是从小到大 升序
    	// 对于支持随机访问的迭代器的容器,都可以利用sort算法直接进行排序
    	// vector容器也可以利用 sort进行排序
    	sort(d.begin(), d.end());
    	cout << "排序后:" << endl;
    	printDeque(d);
    }
    
    int main()
    {
    	test01();
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    在这里插入图片描述

    四、案例 —— 评委打分

    1 案例描述

    • 有5名选手:选手ABCDE,10个评委分别对每一名选手打分,去除最高分,去除评委中最低分,取平均分。

    2 实现步骤

    1. 创建五名选手,放到vector中
    2. 遍历vector容器,取出来每一个选手,执行for循环,可以把10个评分打分存到deque容器中
    3. sort算法对deque容器中分数排序,去除最高和最低分
    4. deque容器遍历一遍,累加总分
    5. 获取平均分
    #include
    #include
    #include
    #include
    #include
    #include
    using namespace std;
    
    // 需求:有5名选手ABCDE,10个评委分别对每一名选手打分,去除最高分,去除评委中最低分,取平均分
    /*
    1. 创建五名选手,放到vector中
    2. 遍历vector容器,取出来每一个选手,执行for循环,可以把10个评分打分存到deque容器中
    3. sort算法对deque容器中分数排序,去除最高和最低分
    4. deque容器遍历一遍,累加总分
    5. 获取平均分
    */
    
    // 创建对象—选手类
    class Person
    {
    public:
    	Person(string name, int score)
    	{
    		this->m_Name = name;
    		this->m_Score = score;
    	}
    	string m_Name;// 姓名
    	int m_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);
    
    		// 将创建的person对象放入容器中
    		v.push_back(p);
    	}
    }
    
    // 打分
    void createScore(vector<Person>& v)
    {
    	// 选手用it表示
    	for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
    	{
    		// 将评委的分数放入deque中
    		deque<int>d;
    		for (int i = 0; i < 10; i++)
    		{
    			int score = rand() % 41 + 60;//60~100
    			d.push_back(score);
    		}
    
    		// 排序
    		sort(d.begin(), d.end());
    
    		// 去除最高和最低分
    		d.pop_back();// 尾删
    		d.pop_front();// 头删
    
    		// 取平均分
    		int sum = 0;
    		for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)
    		{
    			// 累加每个评委分数
    			sum += *dit;
    		}
    		int avg = sum / d.size();
    
    		// 将平均分赋值给选手
    		it->m_Score = avg;
    	}
    }
    
    // 显示分数
    void showScore(vector<Person>& v)
    {
    	for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
    	{
    		cout << "姓名: " << it->m_Name << " 平均分: " << it->m_Score << endl;
    	}
    }
    
    int main()
    {
    	// 随机数种子
    	srand((unsigned int)time(NULL));
    
    	//1.创建5名选手
    	vector<Person>v; // 存放选手容器,将5名
    	createPerson(v);
    
    	// 测试
    	/*for (vector::iterator it = v.begin(); it != v.end(); it++)
    	{
    		cout << "姓名:" << (*it).m_Name << "分数:" << (*it).m_Score << endl;
    	}*/
    
    	// 2.给5名选手打分
    	createScore(v);
    
    	// 3.显示最后得分
    	showScore(v);
    
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116

    在这里插入图片描述

  • 相关阅读:
    利用OPNET进行网络任意源组播(ASM)仿真的设计、配置及注意点
    多线程解决需求
    TiDB 一栈式综合交易查询解决方案获“金鼎奖”优秀金融科技解决方案奖
    springnative让java应用脱离jvm
    自定义RBAC(1)
    单片机操作系统,按键与FIFO
    Linux服务器使用Redis作为数据缓存,并用log4j2进行日志记录
    博客系统(页面设计)
    Go函数介绍与一等公民
    【内存管理】C与C++的内存管理异同点
  • 原文地址:https://blog.csdn.net/qq_42731062/article/details/127616246