STL map assignment_sort_size_delete

map / multimap sort

  1. map <T1, T2, less> mapA; // The container is arranged elements in ascending order of keys. If no function object is specified, the less function object is used by default.
  2. map <T1, T2, greater> mapB; // The container is arranged elements in descending order of keys.
  3. less and greater can be replaced with other function object functors.
  4. You can write custom function objects to compare custom types, using the same methods as the function objects used in set construction.

Copy construction and assignment of map object

map(const map &mp);		     //拷贝构造函数
map& operator=(const map &mp);	//重载等号操作符
map.swap(mp);				//交换两个集合容器

E.g:

		map<int, string> mapA;
		mapA.insert(pair<int,string>(2, "李四"));	
		mapA.insert(pair<int,string>(1, "张三"));	
		mapA.insert(pair<int,string>(3, "王五"));	
		mapA.insert(pair<int,string>(4, "赵六"));	

		map<int ,string> mapB(mapA);	//拷贝构造,此时mapB 和mapA中元素一致
		
		map<int, string> mapC;
		mapC = mapA;			    //赋值,此时mapC 和mapA中元素一致

		mapC[3] = "老张";            //mapC中,此时包含 张三, 李四, 老张, 赵六
		mapC.swap(mapA);		    //mapA 和mapC交换

map size

        map.size();	//返回容器中元素的数目
		map.empty();//判断容器是否为空
		map<int, string> mapA;
		mapA.insert(pair<int,string>(2, "李四"));	
		mapA.insert(pair<int,string>(1, "张三"));	
		mapA.insert(pair<int,string>(3, "王五"));	
		mapA.insert(pair<int,string>(4, "赵六"));	

	if (!mapA.empty())
		{
			int size = mapA.size();		//size 为 4
		}

deletion of map

1) map.clear (); // Delete all elements
2) map.erase (pos); // Delete the element pointed by the pos iterator and return the iterator of the next element.
3) map.erase (beg, end); // Delete all elements in the interval [beg, end) and return the iterator of the next element. (
Have you learned math in high school!) 4) map.erase (key); // Delete the pair with the key in the container, and return the number of deleted pairs
5) Map.erase (key_type * first, key_type * last) // Delete
all team groups corresponding to a specific key in the half-closed and half-open range specified by the array

example:

#include <iostream>
#include <functional>
#include <algorithm>
#include <map>
#include <string>

using namespace std;

int main()
{
	map<int, string, greater<int>> mapStu;

	mapStu.insert(pair<int, string>(2, "李四"));
	mapStu.insert(pair<int, string>(1, "张三"));
	mapStu.insert(pair<int, string>(3, "王五"));
	mapStu.insert(pair<int, string>(4, "赵六"));

	//返回值: 如果大于0, 返回的值为删除元素的个数, 小于0则失败.
	map<int, string, greater<int>>::size_type ret = mapStu.erase(5);
	cout<<"ret = "<<ret<<endl;

	int range[] = {1, 2, 3, 4};
	mapStu.erase(range+1, range+3); //删除数组指定的半闭半的区间中指定的key对应的所有队组

	for(map<int, string, greater<int>>::iterator it = mapStu.begin(); it != mapStu.end(); it++)
	{
		cout<<"key: "<<(*it).first<<" value: "<<(*it).second<<endl;
	}

	system("pause");
	return 0;
}

Operating environment: vc ++ 2010 learning version
Operating results:
Insert picture description here

Published 14 original articles · Like1 · Visits 119

Guess you like

Origin blog.csdn.net/m0_45867846/article/details/105465464