C++从入门到精通 第十四章(STL容器)【下】

 写在前面:

  1. 本系列专栏主要介绍C++的相关知识,思路以下面的参考链接教程为主,大部分笔记也出自该教程,笔者的原创部分主要在示例代码的注释部分。
  2. 除了参考下面的链接教程以外,笔者还参考了其它的一些C++教材(比如计算机二级教材和C语言教材),笔者认为重要的部分大多都会用粗体标注(未被标注出的部分可能全是重点,可根据相关部分的示例代码量和注释量判断,或者根据实际经验判断)。
  3. 由于C++基本继承了C语言的所有内容,建议读者先阅读C语言系列的专栏,有一些重点是仅在C语言系列专栏中有介绍的(比如二级指针、预处理等)。
  4. 如有错漏欢迎指出。

参考教程:黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难_哔哩哔哩_bilibili

六、list容器

1、list的基本概念

(1)list的功能是将数据进行链式存储,对应数据结构中的链表,链表是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接实现的。

(2)链表由一系列结点组成,结点用结构体实现,其中的成员一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域

(3)STL中的链表是一个双向循环链表。

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

(5)list的优缺点:

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

②list的缺点:链表灵活,但是空间(指针域)和时间(遍历)额外耗费较大;list有一个重要的性质,插入操作和删除操作都不会造成原有list迭代器的失效,这在vector是不成立的

2、list的构造函数

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

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

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

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

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

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;   //创建list容器
	L1.push_back(10);  //添加数据
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);
	printList(L1);     //遍历容器

	list<int>L2(L1.begin(), L1.end());   //区间方式构造
	printList(L2);

	list<int>L3(L2);   //拷贝构造
	printList(L3);

	list<int>L4(4, 3000);   //n个elem方式构造
	printList(L4);
}

int main() {

	test01();

	system("pause");

	return 0;
}

3、list的赋值和交换操作

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

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

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

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

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

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

void test01()     
{
	list<int>L1;
	L1.push_back(10); 
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);
	printList(L1);  

	list<int>L2;
	L2 = L1;     //operator=赋值
	printList(L2);

	list<int>L3;
	L3.assign(L2.begin(), L2.end());   //assign赋值
	printList(L3);
	
	list<int>L4;
	L4.assign(10, 100);                //n个elem赋值
	printList(L4);
}
void test02()
{
	list<int>L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);
	cout << "交换前:" << endl;
	printList(L1);
	list<int>L2;
	L2.assign(10, 100);
	printList(L2);
	cout << "交换后:" << endl;
	L1.swap(L2);
	printList(L1);
	printList(L2);
}

int main() {

	test01();
	test02();

	system("pause");

	return 0;
}

4、list的大小

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

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

resize(num);  //重新指定容器的长度为num,若容器变长,则以默认值填充新位置

​   //如果容器变短,则末尾超出容器长度的元素被删除

resize(num, elem);  //重新指定容器的长度为num,若容器变长,则以elem值填充新位置

   //如果容器变短,则末尾超出容器长度的元素被删除

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

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

void test01()     
{
	list<int>L1;
	L1.push_back(10); 
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);
	printList(L1);  
	if (L1.empty())
	{
		cout << "L1为空" << endl;
	}
	cout << "L1的元素个数(大小)为: " << L1.size() << endl;
	L1.resize(8);
	printList(L1);
	L1.resize(3);
	printList(L1);
	L1.resize(10, 80);
	printList(L1);
}

int main() {

	test01();

	system("pause");

	return 0;
}

5、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>
#include<list>
using namespace std;

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> L;
	//尾插
	L.push_back(10);
	L.push_back(20);
	L.push_back(30);
	//头插
	L.push_front(100);
	L.push_front(200);
	L.push_front(300);
	printList(L);

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

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

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

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

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

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

int main() {

	test01();

	system("pause");

	return 0;
}

6、list的数据存取

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

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

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

void test01()
{
	list<int> L;
	L.push_back(10);
	L.push_back(20);
	L.push_back(30);
	L.push_back(40);
	cout << "第一个元素为:" << L.front() << endl;
	cout << "最后一个元素为:" << L.back() << endl;
	//cout << "第一个元素为:" << L[0] << endl;       不可以用[]访问list中的元素
	//cout << "最后一个元素为:" << L.at(0) << endl;  不可以用at访问list中的元素
	list<int>::iterator it = L.begin();
	it++;  //支持递增和递减(双向)
	it--;
	//it = it + 1;   list本质是链表,不是用连续线性空间存储数据的,迭代器不支持随机访问
}

int main() {

	test01();

	system("pause");

	return 0;
}

7、list的反转和排序算法

reverse();  //反转链表(元素逆序存储)

sort();     //链表排序(给链表的元素排序)

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

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> L;
	L.push_back(10);
	L.push_back(50);
	L.push_back(40);
	L.push_back(30);
	L.push_back(20);
	printList(L);

	L.reverse();  //反转
	printList(L);
}
bool myCompare(int v1,int v2)
{
	//降序--第一个数>第二个数
	return v1 > v2;
	//升序--第二个数<第一个数
}

void test02()
{
	list<int> L;
	L.push_back(10);
	L.push_back(50);
	L.push_back(40);
	L.push_back(30);
	L.push_back(20);
	printList(L);

	//sort(L.begin(), L.end())     所有不支持随机访问迭代器的容器,都不可以用标准算法,但它们内部会提供对应的一些算法
	L.sort();           //默认升序(从小到大)
	printList(L);
	L.sort(myCompare);  //降序
	printList(L);   
}

int main() {

	test01();
	test02();

	system("pause");

	return 0;
}

8、案例

(1)案例描述:将Person自定义数据类型进行排序,Person中属性有姓名、年龄、身高,按照年龄进行升序排序,如果年龄相同,按照身高进行降序排序。

(2)代码:

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

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

void printList(const list<Person> &L)
{
	for (list<Person>::const_iterator it = L.begin(); it != L.end(); it++)
	{
		cout << "姓名: " << (*it).m_Name << " 年龄: " << (*it).m_Age << " 身高: " << it->m_Height << endl;
	}
	cout << endl;
}
bool myCompare(Person &v1, Person &v2)
{
	if (v1.m_Age != v2.m_Age)
	{
		return v1.m_Age < v2.m_Age;
	}
	else
	{
		return v1.m_Height > v2.m_Height;
	}
}

void test01()
{
	list<Person>P;
	Person p1("张三", 19, 173);
	Person p2("李四", 34, 183);
	Person p3("王五", 26, 163);
	Person p4("刘六", 26, 170);
	P.push_back(p1);
	P.push_back(p2);
	P.push_back(p3);
	P.push_back(p4);
	P.sort(myCompare);
	printList(P);
}

int main() {

	test01();

	system("pause");

	return 0;
}

七、set/multiset容器

1、set/multiset的基本概念

(1)所有元素都会在插入set/multiset容器时自动被排序。

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

(3)set和multiset的区别:set不允许容器中有重复的元素,multiset允许容器中有重复的元素。

2、set构造函数和赋值操作

//构造函数:

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

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

//赋值操作:

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

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

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

void test01()
{
	set<int>s1;
	s1.insert(10);             //只有insert能插入数据
	s1.insert(20);
	s1.insert(40);
	s1.insert(30);
	s1.insert(10);
	printSet(s1);              //插进set容器的数据会自动排序,且不会存在重复值

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

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

int main() {

	test01();

	system("pause");

	return 0;
}

3、set大小和交换操作

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

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

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

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

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

void test01()
{
	set<int>s1;
	s1.insert(10);
	s1.insert(20);
	s1.insert(40);
	s1.insert(30);
	printSet(s1);
	if (s1.empty())
	{
		cout << "s1为空" << endl;
	}
	else
	{
		cout << "s1的大小为:" << s1.size() << endl;
	}
}
void test02()
{
	set<int>s1;
	s1.insert(10);
	s1.insert(20);
	s1.insert(40);
	s1.insert(30);
	printSet(s1);
	set<int>s2;
	s2.insert(11);
	s2.insert(21);
	s2.insert(41);
	s2.insert(31);
	printSet(s2);
	s1.swap(s2);
	printSet(s1);
	printSet(s2);
}

int main() {

	test01();
	test02();

	system("pause");

	return 0;
}

4、set的插入和删除

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

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

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

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

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

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

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

void test01()
{
	set<int> s1;
	//插入
	s1.insert(10);
	s1.insert(30);
	s1.insert(20);
	s1.insert(40);
	printSet(s1);

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

	s1.erase(30);
	printSet(s1);

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

int main() {

	test01();

	system("pause");

	return 0;
}

5、set查找和统计操作

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

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

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

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

void test01()
{
	set<int> s1;
	s1.insert(10);
	s1.insert(30);
	s1.insert(20);
	s1.insert(40);
	printSet(s1);

	set<int>::iterator pos = s1.find(30);
	if (pos != s1.end())
	{
		cout << "找到元素" << *pos << endl;
	}
	else
	{
		cout << "未找到元素" << endl;
	}
	pos = s1.find(50);
	if (pos != s1.end())
	{
		cout << "找到元素" << *pos << endl;
	}
	else
	{
		cout << "未找到元素" << endl;
	}
}
void test02()
{
	set<int> s1;
	s1.insert(10);
	s1.insert(30);
	s1.insert(30);
	s1.insert(20);
	s1.insert(40);
	printSet(s1);

	int num = s1.count(30);
	cout << "“30”元素有" << num << "个" << endl;  //结果要么是0,要么是1
	num = s1.count(300);
	cout << "“300”元素有" << num << "个" << endl;
}

int main() {

	test01();
	test02();

	system("pause");

	return 0;
}

6、set和multiset的区别

(1)set不可以插入重复数据,而multiset可以。

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

(3)multiset不会检测数据,因此可以插入重复数据。

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

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

void test01()
{
	set<int> s1;
	s1.insert(30);
	s1.insert(30);
	s1.insert(20);
	s1.insert(40);
	printSet(s1);
	pair<set<int>::iterator, bool>ret = s1.insert(10);
	if (ret.second)
	{
		cout << "第一次插入成功" << endl;
	}
	else
	{
		cout << "第一次插入失败" << endl;
	}
	printSet(s1);
	ret = s1.insert(10);
	if (ret.second)
	{
		cout << "第二次插入成功" << endl;
	}
	else
	{
		cout << "第二次插入失败" << endl;
	}
	printSet(s1);
	multiset<int> ss1;
	ss1.insert(30);
	ss1.insert(30);
	ss1.insert(20);
	ss1.insert(40);
	ss1.insert(30);
	ss1.insert(30);
	ss1.insert(20);
	ss1.insert(40);
	for (multiset<int>::iterator it = ss1.begin(); it != ss1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

7、set容器的排序规则

(1)set容器默认排序规则为从小到大,利用仿函数可以改变排序规则.

(2)举例:

①例1:

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

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

void test01()
{
	set<int>s1;
	s1.insert(10);
	s1.insert(30);
	s1.insert(20);
	s1.insert(40);
	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(30);
	s2.insert(20);
	s2.insert(40);
	s2.insert(50);
	for (set<int,MyCompare>::iterator it = s2.begin(); it != s2.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

②例2:

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

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

class MyCompare
{
public:
	bool operator()(const Person &v1,const Person &v2)
	{
		return v1.m_Age > v2.m_Age;
	}
};

void test01()
{
	set<Person,MyCompare>p;
	Person p1( 19 ,"张三");
	Person p2( 34 ,"李四");
	Person p3( 26 ,"王五");
	Person p4( 28 ,"刘六");
	p.insert(p1);
	p.insert(p2);
	p.insert(p3);
	p.insert(p4);
	for (set<Person, MyCompare>::iterator it = p.begin(); it != p.end(); it++)
	{
		cout << "姓名:" << it->m_Name << " 年龄:" << it->m_Age;
		cout << endl;
	}
}

int main() {

	test01();

	system("pause");

	return 0;
}

③例3:

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

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

class MyCompare
{
public:
	bool operator()(const Person &v1,const Person &v2)
	{
		return v1.m_Age > v2.m_Age;
	}
};

void test01()
{
	multiset<Person,MyCompare>p;
	Person p1( 19 ,"张三");
	Person p2( 34 ,"李四");
	Person p3( 26 ,"王五");
	Person p4( 26 ,"刘六");
	p.insert(p1);
	p.insert(p2);
	p.insert(p3);
	p.insert(p4);
	for (multiset<Person, MyCompare>::iterator it = p.begin(); it != p.end(); it++)
	{
		cout << "姓名:" << it->m_Name << " 年龄:" << it->m_Age;
		cout << endl;
	}
}

int main() {

	test01();

	system("pause");

	return 0;
}

八、map/multimap容器

1、map的基本概念

(1)map中所有元素都是pair,pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)。

(2)map中的所有元素都会根据元素的键值自动排序。

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

(4)map的特点是可以根据key值快速找到value值。

(5)map和multimap的区别:map不允许容器中有重复key值的元素,multimap允许容器中有重复key值的元素。

2、pair对组创建

(1)成对出现的数据,利用对组可以返回两个数据。

(2)两个数据的数据类型可以不同。

//对组的定义

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

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

//访问对组的元素

p.first    //第一个元素

p.second  //第二个元素

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

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

	system("pause");

	return 0;
}

3、map的构造函数和赋值操作

//构造函数:

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

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

//赋值操作:

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

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

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>(3, 20));
	m.insert(pair<int, int>(2, 30));
	m.insert(pair<int, int>(2, 30));  //key值重复,无法插入
	m.insert(pair<int, int>(2, 20));  //key值重复,无法插入
	m.insert(pair<int, int>(4, 40));
	printMap(m);

	map<int, int> m2(m);
	printMap(m2);

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

int main() {

	test01();

	system("pause");

	return 0;
}

4、map的大小和交换操作

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

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

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

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

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>(3, 20));
	m.insert(pair<int, int>(2, 30));
	m.insert(pair<int, int>(4, 40));
	if (m.empty())
	{
		cout << "map容器为空" << endl;
	}
	else
	{
		cout << "map容器的元素个数为:" << m.size() << endl;
	}
}
void test02()
{
	map<int, int> m;
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(3, 20));
	m.insert(pair<int, int>(2, 30));
	m.insert(pair<int, int>(4, 40));
	printMap(m);
	map<int, int> m2;
	m2.insert(pair<int, int>(5, 100));
	m2.insert(pair<int, int>(6, 200));
	m2.insert(pair<int, int>(2, 300));
	m2.insert(pair<int, int>(8, 400));
	printMap(m2);
	m.swap(m2);
	printMap(m);
	printMap(m2);
}

int main() {

	test01();
	test02();

	system("pause");

	return 0;
}

5、map的插入和删除

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

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

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

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

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

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

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

void test01()
{
	map<int, int> m;
	m.insert(pair<int, int>(1, 10));  //建议
	m.insert(make_pair(2, 30));       //建议
	m.insert(map<int, int>::value_type(3, 20));   //不建议
	m[4] = 40;                                    //不建议用于插入,不过[]可以用于访问value
	printMap(m);
	cout << m[5] << endl;   //不存在key=5,直接输出0,顺便插入了pair<int, int>(5, 0)
	cout << m[4] << endl;
	printMap(m);
	m.erase(--m.end());
	printMap(m);
	m.erase(2);    //删除 key = 2 一组的数据
	printMap(m);
	//清空
	//m.erase(m.begin(), m.end());
	m.clear();
	printMap(m);
}

int main() {

	test01();

	system("pause");

	return 0;
}

6、map查找和统计操作

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

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

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

void test01()
{
	map<int, int> m;
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(3, 20));
	m.insert(pair<int, int>(3, 30));
	m.insert(pair<int, int>(2, 30));
	m.insert(pair<int, int>(4, 40));
	map<int, int>::iterator pos = m.find(3);
	if (pos != m.end())
	{
		cout << "查到了元素key = " << (*pos).first << " value = " << pos->second << endl;
	}
	else
	{
		cout << "未找到元素" << endl;
	}
	int num = m.count(3);
	cout << "num = " << num << endl;  //map不允许插入重复key元素,num要么为0,要么为1,不过multimap就不一定了
}

int main() {

	test01();

	system("pause");

	return 0;
}

7、map容器排序规则

(1)map容器默认排序规则为按照key值进行从小到大排序,利用仿函数可以改变排序规则。

(2)举例:

①例1:

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

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

void test01()
{
	map<int, int,MyCompare> m;
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(3, 20));
	m.insert(pair<int, int>(2, 30));
	m.insert(pair<int, int>(4, 40));
	m.insert(pair<int, int>(5, 50));
	printMap(m);

}

int main() {

	test01();

	system("pause");

	return 0;
}

②例2:

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

class Person
{
public:
	int m_Age;
	string m_Name;
	Person(int age, string name)
	{
		this->m_Age = age;
		this->m_Name = name;
	}
};
class MyCompare
{
public:
	bool operator()(const Person &p1, const Person &p2)
	{
		return p1.m_Age > p2.m_Age;
	}
};

void test01()
{
	map<Person, int, MyCompare>m;
	Person p1(19, "张三");
	Person p2(34, "李四");
	Person p3(26, "王五");
	Person p4(28, "刘六");
	m.insert(make_pair(p1, 10));
	m.insert(make_pair(p2, 20));
	m.insert(make_pair(p3, 40));
	m.insert(make_pair(p4, 30));
	for (map < Person, int, MyCompare>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "姓名: " << it->first.m_Name << " 年龄:" << it->first.m_Age << endl;
	}
}

int main() {

	test01();

	system("pause");

	return 0;
}

九、终极案例

1、案例描述

(1)公司今天招聘了10个员工(ABCDEFGHIJ),10名员工进入公司之后,需要指派员工在哪个部门工作。

(2)员工信息有姓名和工资,部门分为策划、美术、研发三个部门。

(3)随机给10名员工分配部门和工资。

(4)通过multimap进行信息的插入key(部门编号)——value(员工)。

(5)分部门显示员工信息。

2、实现步骤

(1)创建10名员工,放到vector中。

(2)遍历vector容器,取出每个员工,进行随机分组。

(3)分组后,将员工部门编号作为key,具体员工作为value,放入到multimap容器中。

(4)分部门显示员工信息。

3、代码

#include<iostream>
#include<map>
#include<vector>
#include<string>
#include<time.h>
using namespace std;

class Worker
{
public:
	string m_Name;
	int m_Salary;
	/*Worker()
	{
		m_Name = name;
		m_Salary = Salary;
	}*/
};
void createWorker(vector<Worker>&v)
{
	string str = "ABCDEFGHIJ";
	for (int i = 0; i < 10; i++)
	{
		Worker worker;
		worker.m_Name = "员工";
		worker.m_Name += str[i];
		worker.m_Salary = rand() % 10000 + 10000;
		v.push_back(worker);
	}
}
void setGroup(multimap<string, Worker>&m , vector<Worker>&v)
{
	vector<Worker>::iterator it = v.begin();
	for (int i = 0; i < 10; i++)
	{
		int key = rand() % 3 + 1;
		string bumen;
		switch (key)
		{
		case 1:
			bumen = "策划";
			break;
		case 2:
			bumen = "美术";
			break;
		case 3:
			bumen = "研发";
			break;
		default:
			break;
		}
		m.insert(make_pair(bumen, *(it++)));
	}
}
void showWorkerByGourp(multimap<string, Worker>&m)
{
	multimap<string, Worker>::iterator it = m.begin();
	cout << "策划部门:" << endl;
	for (int i = 0 ; it != m.end() && i < m.count("策划"); it++ , i++)
	{
		cout << "  姓名:" << it->second.m_Name << "  工资:" << it->second.m_Salary << endl;
	}
	cout << "---------------------------" << endl;
	cout << "美术部门:" << endl;
	for (int i = 0; it != m.end() && i < m.count("美术"); it++, i++)
	{
		cout << "  姓名:" << it->second.m_Name << "  工资:" << it->second.m_Salary << endl;
	}
	cout << "---------------------------" << endl;
	cout << "研发部门:" << endl;
	for (int i = 0; it != m.end() && i < m.count("研发"); it++, i++)
	{
		cout << "  姓名:" << it->second.m_Name << "  工资:" << it->second.m_Salary << endl;
	}
}

void test01()
{
	vector<Worker>vWorker;
	createWorker(vWorker);
	/*for (vector<Worker>::iterator it = vWorker.begin(); it != vWorker.end(); it++)
	{
		cout << it->m_Name << "  ";
	}
	cout << endl;*/
	multimap<string, Worker>mGroup;
	setGroup(mGroup, vWorker);
	showWorkerByGourp(mGroup);
	/*for (multimap<string, Worker>::iterator it = mGroup.begin(); it != mGroup.end(); it++)
	{
		cout << "部门:" << it->first << "  姓名:" << it->second.m_Name << "  工资:" << it->second.m_Salary << endl;
	}*/
}

int main() {

	srand((unsigned int)time(NULL));
	test01();

	system("pause");

	return 0;
}
  • 19
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zevalin爱灰灰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值