STL-map/multimap容器

1.map

  • map容器中所有的元素都是键值对,即pair
  • pair中的第一个元素为键值,第二个元素为value
  • 所有的元素会根据元素的键值自动排序
  • 不允许容器中有重复的key值,multimap容器允许有重复的key值
  • 属于关联式容器,底层结构为二叉树

1.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 (auto item : m)
	{
    
    
		cout << "item.first  = " << item.first << endl;
		cout << "item.second = " << item.second << endl;
	}

#if 0
	for (auto it = m.begin();it != m.end(); ++it)
	{
    
    
		cout << "it->first  = " << it->first << endl;
		cout << "it->second = " << it->second << endl;
	}
#endif
}

int main()
{
    
    
	map<int, int> m;

	m.insert(pair<int, int>(1, 2));
	m.insert(pair<int, int>(3, 4));
	m.insert(pair<int, int>(5, 6));

	printMap(m);

	map<int, int>m2(m);
	cout << "++++++++++cout m2+++++++++" << endl;
	printMap(m2);
	cout << "++++++++++cout m3+++++++++" << endl;
	map<int, int>m3;
	m3 = m2;
	printMap(m3);
}

结果:

item.first  = 1
item.second = 2
item.first  = 3
item.second = 4
item.first  = 5
item.second = 6
++++++++++cout m2+++++++++
item.first  = 1
item.second = 2
item.first  = 3
item.second = 4
item.first  = 5
item.second = 6
++++++++++cout m3+++++++++
item.first  = 1
item.second = 2
item.first  = 3
item.second = 4
item.first  = 5
item.second = 6

2.map大小与交换

size():返回容器中元素的个数
empty():判断容器是否为空
swap(st):交换两个集合容器,空间不一样也可使用

例子:

#include <iostream>
#include <map>

using namespace std;

void printMap(map<int, int>& m)
{
    
    
	for (auto item : m)
	{
    
    
		cout << "item.first  = " << item.first << endl;
		cout << "item.second = " << item.second << endl;
	}

#if 0
	for (auto it = m.begin();it != m.end(); ++it)
	{
    
    
		cout << "it->first  = " << it->first << endl;
		cout << "it->second = " << it->second << endl;
	}
#endif
}

int main()
{
    
    
	map<int, int> m;

	m.insert(pair<int, int>(1, 2));
	m.insert(pair<int, int>(3, 4));
	m.insert(pair<int, int>(5, 6));

	cout << "m.size=" << m.size() << endl;
	if (m.empty())
	{
    
    
		cout << "m is empty" << endl;
	}
	else
	{
    
    
		cout << "m is not empty" << endl;
	}
	printMap(m);

	map<int, int>m2;
	m2.insert(pair<int, int>(7, 8));
	m.swap(m2);
	cout << "++++++++++cout m2+++++++++" << endl;
	printMap(m2);
	cout << "++++++++++cout m1+++++++++" << endl;
	printMap(m);

}

结果:

m.size=3
m is not empty
item.first  = 1
item.second = 2
item.first  = 3
item.second = 4
item.first  = 5
item.second = 6
++++++++++cout m2+++++++++
item.first  = 1
item.second = 2
item.first  = 3
item.second = 4
item.first  = 5
item.second = 6
++++++++++cout m1+++++++++
item.first  = 7
item.second = 8

3.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 (auto item : m)
	{
    
    
		cout << "item.first  = " << item.first << endl;
		cout << "item.second = " << item.second << endl;
	}

#if 0
	for (auto it = m.begin();it != m.end(); ++it)
	{
    
    
		cout << "it->first  = " << it->first << endl;
		cout << "it->second = " << it->second << endl;
	}
#endif
}

int main()
{
    
    
	map<int, int> m;

	//1.first insert method1
	m.insert(pair<int, int>(1, 2));
	//2.sencond 
	m.insert(make_pair(3, 4));
	//3.
	m.insert(map<int,int>::value_type(5, 6));

	cout<<"-----------------------------------m----------------------------------------"<<endl;
	printMap(m);

	//erase 
	cout<<"------------------------erase begin and 3------------------------------------"<<endl;
	m.erase(m.begin());
	m.erase(3);

	printMap(m);
	
	cout << "--------------------erase all----------------------------------------------"<<endl;
	m.erase(m.begin(),m.end());
	printMap(m);

}

编译:

g++ map_insert.cpp -std=c++11 -o map_insert

结果:

-----------------------------------m----------------------------------------
item.first  = 1
item.second = 2
item.first  = 3
item.second = 4
item.first  = 5
item.second = 6
------------------------erase begin and 3------------------------------------
item.first  = 5
item.second = 6
--------------------erase all----------------------------------------------

4.map容器的查找与统计

find(key)//m.find(key),查找key元素是否存在,返回改元素的迭代器,若不存在,则返回set.end()
count(key)//统计key的元素个数,对于map只有0和1

示例:

#include <iostream>
#include <map>

using namespace std;

void printMap(map<int, int>& m)
{
    
    
	for (auto item : m)
	{
    
    
		cout << "item.first  = " << item.first << endl;
		cout << "item.second = " << item.second << endl;
	}

#if 0
	for (auto it = m.begin();it != m.end(); ++it)
	{
    
    
		cout << "it->first  = " << it->first << endl;
		cout << "it->second = " << it->second << endl;
	}
#endif
}

int main()
{
    
    
	map<int, int> m;

	//1.first insert method1
	m.insert(pair<int, int>(1, 2));
	//2.sencond 
	m.insert(make_pair(3, 4));
	//3.
	m.insert(map<int,int>::value_type(5, 6));

	cout <<"-----------------------------------m----------------------------------------" <<endl;
	printMap(m);

	//查找 
	cout<<"------------------------find 3------------------------------------"<<endl;
	auto it=m.find(3);
	if(it != m.end())
	{
    
    
		cout<<"key 3 value is=" << it->second << endl;
	}	
	

	cout << "--------------------count 3----------------------------------------------"<<endl;
	int num = m.count(3);
	cout << "count 3 is=" << num << endl;	

}

结果:

-----------------------------------m----------------------------------------
item.first  = 1
item.second = 2
item.first  = 3
item.second = 4
item.first  = 5
item.second = 6
------------------------find 3------------------------------------
key 3 value is=4
--------------------count 3----------------------------------------------
count 3 is=1

5.map容器的排序

map容器默认为按照key值小到大进行排序,但是我们可以利用仿函数进行从大到小进行排序

示例:

#include <iostream>
#include <map>

using namespace std;


class BigToSmall
{
    
    
public:
	bool operator()(int a,int b)
	{
    
    
		return a > b;
	}

};

void printMap(map<int, int,BigToSmall>& m)
{
    
    
	
	cout << "-------------------sort---------------------"<<endl;
	for (auto item : m)
	{
    
    
		cout << "item.first  = " << item.first << endl;
		cout << "item.second = " << item.second << endl;
	}

#if 0
	cout << "---------------------sort----------------------"<<endl; 
	for (map<int,int,BigToSmall>:: iterator it= m.begin();it != m.end(); ++it)
	{
    
    
		cout << "it->first  = " << it->first << endl;
		cout << "it->second = " << it->second << endl;
	}
#endif
}

int main()
{
    
    
	map<int, int,BigToSmall> m;

	//1.first insert method1
	m.insert(pair<int, int>(1, 2));
	m.insert(pair<int,int>(7,8));
	//2.sencond 
	m.insert(make_pair(3, 4));
	//3.
	m.insert(map<int,int>::value_type(5, 6));

	printMap(m);
}

结果:

------------------sort---------------------
item.first  = 7
item.second = 8
item.first  = 5
item.second = 6
item.first  = 3
item.second = 4
item.first  = 1
item.second = 2

猜你喜欢

转载自blog.csdn.net/weixin_43824344/article/details/121091207