C++中的map/multimap容器

1.

map中所有元素都是pai对组,pair中的第一个元素是key(键值),起到索引作用,第二个元素为value(实际值),并且map中的所有元素都会根据元素的键值自动排序,map/multimap本质上是一个关联式容器,底层结构是用二叉树实现的。
map/multimap具有使用key值快速查找到value值的有点,map与multimap的区别是前者不允许容器中有重复的key值元素,后者则恰恰相反它允许容器中有重复的key值元素。

  • map构造和赋值
    构造函数:
map <T1,T2>mp;	//默认构造函数	
map(const map &mp);	//拷贝构造函数

其中T1代表key,T2代表value
赋值:

map& operator=(const map &mp);

该容器重载了=号运算符支持赋值操作

  • 插入数据的操作:
	mp.insert(pair<T1,T2>(key,value));	
	mp.insert(make_pair(key,value));	
	mp.insert(map<T1,T2>::value_type(key,value))
	mp[key] = value; 

因为map/multimap中的元素是以对组pair出现的,所以插入的数据必须是pair对组;上面的插入插座也可以将pari<T1,T2>换成make_pair,也可以是map容器自带的方法value_type,还可以使用中括号,中括号里面是索引(key)右边是value,这里顺带提一下中括号,在使用自定义类型的时候(比如类)需要保留默认构造,具体如下:

class Person
{
public:
	Person(){}	//不加这里的话[]会报错
	Person(string nam,int ag)
	{
		this->name = nam;
		this->age = ag;
	}
	string name;
	int age;
};

...

	Person p1("张三",22);
	map <int,Person> mp;
	mp[1] = p2;

我之前就是忽略了这个默认构造所以使用[]的时候就报错,但是不建议使用这种方法来插入数据,有时候在使用中括号是运算符的时候如果没有它会自动创建出一个value,所以只用它来访问已经存在的元素,比如:

map <int,int> mp;
mp.insert(make_pair(0,10));
cout << mp[0] << endl;	//这里打印就是10
cout << mp[1] << endl;	//如果5对应的value没有那么它就会自动创建一个
  • 删除操作
mp.erase(key);

map<T1,T2>::iterator it = mp.begin();
mp.erase(++it,mp.end());

mp.clear();

第一种是传入以个key值,删除key值的对应对组元素,第二种是使用迭代器范围的,但是这里不支持随it+n操作(it指map的迭代器);第三种是清空整个map容器。

  • 同样它也支持自定义排序,这里需要用到仿函数,示例如下:
class Person
{
public:
	Person(){}
	Person(string nam,int ag)
	{
		this->name = nam;
		this->age = ag;
	}
	string name;
	int age;
};
class Mcompare
{
public:
	bool operator()(int v1,int v2)
	{
		return v1 > v2;
	}
};
void print(map<int,Person,Mcompare> &p)
{
	for(map<int,Person,Mcompare>::iterator it = p.begin();it != p.end();it++)
	{
		cout << " 编号:" <<it->first << "  姓名: " << (it->second).name << "  年龄: " <<(it->second).age << endl;
	}
	cout << endl;
}
...

	Person p1("张三",22);
	Person p2("李四",28);
	Person p3("王五",21);
	
	map <int,Person,Mcompare> mp5;
	mp5.insert(make_pair(0,p1));
	mp5.insert(make_pair(9,p2));
	mp5.insert(make_pair(3,p3));
  • 查找和统计:
find(key);

如果存在返回该键值所对应元素的迭代器,如果不存在返回end迭代器;
例如:

	if(mp.find(1) != mp.end())
	{
		cout << "have find key 1"<< "the value is"<< ((mp.find(1))->second).name << "  "<< ((mp.find(1))->second).age<<endl;
	}
	
	map <int,Person>::iterator it = mp.end();

	if(mp.find(4) == it)
	{
		cout << "can't have find key 4"<<endl;
	}

统计:

count(key)

统计对应key的元素个数,如果是map容器则返回0或者1(因为map不允许key值相同的元素),如果是multimap容器则会返回n

常用到的方法:

clear()	删除所有元素
count()	返回指定元素出现的次数
empty()	如果map为空则返回true
equal_range()	返回特殊条目的迭代器对
get_allocator()	返回map的配置器
key_comp()	返回比较元素key的函数
lower_bound()	返回键值>=给定元素的第一个位置
max_size()	返回可以容纳的最大元素个数
rbegin()	返回一个指向map尾部的逆向迭代器
rend()	返回一个指向map头部的逆向迭代器
size()	返回map中元素的个数
swap()	交换两个map
upper_bound()	返回键值>给定元素的第一个位置
value_comp()	返回比较元素value的函数

示例代码

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

class Person
{
public:
	Person(){}
	Person(string nam,int ag)
	{
		this->name = nam;
		this->age = ag;
	}
	string name;
	int age;
};
class Mcompare
{
public:
	bool operator()(int v1,int v2)
	{
		return v1 > v2;
	}
};


void print(map<int,Person> &p)
{
	for(map<int,Person>::iterator it = p.begin();it != p.end();it++)
	{
		cout << " 编号:" <<it->first << "  姓名: " << (it->second).name << "  年龄: " <<(it->second).age << endl;
	}
	cout<< endl;
}

void Mprint(map<int,Person,Mcompare> &p)//对print进行重载
{
	for(map<int,Person,Mcompare>::iterator it = p.begin();it != p.end();it++)
	{
		cout << " 编号:" <<it->first << "  姓名: " << (it->second).name << "  年龄: " <<(it->second).age << endl;
	}
	cout << endl;
}

void test01(void)
{
	Person p1("张三",22);
	Person p2("李四",28);
	Person p3("王五",21);

	map <int,Person> mp;
	if(mp.empty())
	{
		cout << "mp is empty" << endl;
	}
	
	mp.insert(pair<int,Person>(1,p1));
	mp.insert(pair<int,Person>(0,p2));
	mp.insert(pair<int,Person>(2,p3));
	
	cout << "mp: "<< endl;
	print(mp);
	if(!mp.empty())
	{
		cout << "mp is not empty" << endl;
	}

	cout <<"mp1: "<< endl;
	map<int,Person> mp1(mp);
	print (mp1);

	cout << "mp2: "<<endl;
	map<int,Person> mp2= mp;
	print(mp2);

	cout << "mp2 size:" << mp2.size() << endl;

	Person p4("赵六",19);
	Person p5("老八",27);
	Person p6("漆九",21);
	map<int, Person> mp3;
	mp3.insert(pair<int ,Person>(8,p4));
	mp3.insert(make_pair(7,p5));
	mp3.insert(map<int,Person>::value_type(10,p6));
	cout <<"mp3 before swap:  " <<endl;
	print(mp3);
	cout <<"mp3 after swap(mp2):  " <<endl;
	mp3.swap(mp2);
	print(mp3);
	cout << "mp2 after swap: " <<endl;
	print(mp2);


	map <int,Person,Mcompare> mp5;
	
	mp5.insert(make_pair(0,p1));
	mp5.insert(pair<int,Person>(9,p2));
	mp5.insert(make_pair(3,p3));
	mp5.insert(make_pair(1,p4));
	cout << "mp5: (使用仿函数)"<< endl;
	Mprint(mp5);

//	cout << "earse 9: " << endl;
//	mp5.erase(9);
	cout << "对应的范围的删除" << endl;
	map<int,Person,Mcompare>::iterator it = mp5.begin();
	map<int,Person,Mcompare>::iterator it1 = mp5.end();
	mp5.erase(++it,--it1);
	Mprint(mp5);

	cout << "清空 mp3:" <<endl;
	mp3.clear();
	if(mp3.empty())
	{
		cout << "mp3 is empty" << endl;
	}
}

void print(const map<int,int> &p)
{
	for(map<int,int>::const_iterator it = p.begin();it != p.end();it++)
	{
		cout << it->first << "  " << it->second << endl;
	}	
	cout << endl;
}
void test02(void)
{
	cout << "------------------------------------------"<<endl;
	map<int,int> mp;
	mp[0] = 1;
	print(mp);

}


void test03(void)
{
	Person p1("张三",22);
	Person p2("李四",28);
	Person p3("王五",21);

	map <int,Person> mp;
	mp.insert(make_pair(0,p1));
	mp.insert(make_pair(0,p1));
	mp.insert(make_pair(1,p2));
	mp.insert(make_pair(2,p3));
	mp[1] = p2;
	print(mp);

	cout << "mp key 0 numbers: " << mp.count(0) << endl;

	cout << "find 0" << endl;
	if(mp.find(1) != mp.end())
	{
		cout << "have find key 1"<< "the value is"<< ((mp.find(1))->second).name << "  "<< ((mp.find(1))->second).age<<endl;
	}
	
	map <int,Person>::iterator it = mp.end();

	if(mp.find(4) == it)
	{
		cout << "can't have find key 4"<<endl;
	}

	multimap<int,Person> mp2;
	mp2.insert(make_pair(0,p1));
	mp2.insert(make_pair(0,p1));
	mp2.insert(make_pair(0,p1));

	cout << "mp2 key 0 numbers: " << mp2.count(0) << endl;
}
int main(void)
{
	test01();
	test02();

	test03();

	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ STLmapmultimap是关联容器,用于存储键值对(key-value pairs),其每个键(key)唯一对应一个值(value)。 map是一个有序容器,根据键的大小进行自动排序,默认按照键的升序进行排序。每个键只能在map出现一次,如果尝试插入具有相同键的元素,新元素将替代旧元素。 multimap也是一个有序容器,与map不同的是,它允许多个具有相同键的元素存在。多个具有相同键的元素将按照插入的顺序进行存储,而不会自动排序。 这两个容器都提供了一系列的操作函数,如insert、erase、find等,用于插入、删除和查找元素。 以下是一个使用map的简单示例: ```cpp #include <iostream> #include <map> int main() { std::map<std::string, int> scores; scores.insert(std::make_pair("Alice", 90)); scores.insert(std::make_pair("Bob", 80)); scores.insert(std::make_pair("Charlie", 70)); // 查找并输出Bob的分数 std::cout << "Bob's score: " << scores["Bob"] << std::endl; // 遍历并输出所有键值对 for (const auto& pair : scores) { std::cout << pair.first << ": " << pair.second << std::endl; } return 0; } ``` 上述示例,我们创建了一个存储string类型键和int类型值的map容器scores。通过insert函数依次插入了三个键值对。然后我们通过scores["Bob"]来获取Bob的分数,并输出结果为80。 接着我们使用范围-based for循环遍历map的所有键值对,并输出每个键值对的键和值。 multimap的用法与map类似,只是它允许多个具有相同键的元素存在。 这些关联容器查找和插入操作上具有较高的效率,特别适用于需要根据键进行快速查找的场景。在实际应用,你可以根据自己的需求选择适合的容器类型。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值