[STL] structure map / multimap container, assignment, insert, delete. . . . And other operations (easy to learn) and Cases

map / multimap container:

Map with respect to the set difference:

and key map having a real value, all the elements automatically sorted according to the key value. Pair is referred to as a first key element, the second element is referred to as a real value. Map is also based on the underlying implementation mechanism for the red-black tree.

We can modify the keys of the map you through the map iterators?

The answer is no, the key rule related to the arrangement of elements within the container, any change will destroy the keys arranged in regular container, but can be changed in real terms.
** map and multimap difference is that there is not allowed the same key value map, multimap allow the same key value exists.

map / multimap container operations

Here Insert Picture DescriptionHere Insert Picture DescriptionHere Insert Picture DescriptionHere Insert Picture Description

Case (how to knock?)

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

void printmap(map<int, int>& v) {
	for (map<int, int>::iterator it = v.begin(); it != v.end(); ++it) {
		cout << (*it).first << " " << (*it).second << endl;
	}
}
//map容器的初始化
void  test01() {
	//map容器模板参数,第一个是key值类型,第二个是value值类型
	map<int, int>mymap;
	
	//插入数据 pair.first是key值;pair.second 是value值
	//第一种
	pair<map<int,int>::iterator,bool>ret=mymap.insert(pair<int,int>(10,10));
	if (ret.second) {
		cout << "第一次插入成功" << endl;//成功
	}
	else {
		cout << "第一次插入失败" << endl;
	}
	ret = mymap.insert(pair<int, int>(10, 20));
	if (ret.second) {
		cout << "插入成功" << endl;
	}
	else {
		cout << "插入失败" << endl;//失败,因为key值相同,无法插入
	}
	
	ret = mymap.insert(pair<int, int>(20, 10));
	//第二种
	mymap.insert(make_pair(20, 20));
	//第三种
	mymap.insert(map<int, int>::value_type(30, 30));
	//第四种
	mymap[40] = 40;
	mymap[10] = 20;//发现如果key不存在,创建pair插入到map容器中
	mymap[50] = 50;//如果发现key存在,那么会修改key对应的value值

	printmap(mymap);
	//如果通过[]方式去访问map中一个不存在的key,那么map会将这个访问的key值插入到map中
	//并且给value一个默认值。
	cout << mymap[60] << endl;
	printmap(mymap);

}

class MyKey {
public:
	MyKey(){}
	MyKey(int index, int id) {
		this->mIndex = index;
		this->mID = id;
	}
public:
	int mIndex;
	int mID;
};
class  mycompare{
public:
	bool operator()(MyKey key1, MyKey key2)const{
		return key1.mIndex > key2.mIndex;
	}

};
void test02() {
	map<MyKey, int, mycompare>mymap;
	mymap.insert(make_pair(MyKey(1, 2), 10));
	mymap.insert(make_pair(MyKey(4, 5), 20));


	for (map<MyKey, int, mycompare>::iterator it = mymap.begin(); it != mymap.end(); ++it) {
		cout << "ID:"<<(*it).first .mID<< " Index:" << (*it).first.mIndex<<" "<<(*it).second << endl;
	}
	
}

void test03() {
	map<int, int>mymap;
	mymap.insert(make_pair(1, 4));
	mymap.insert(make_pair(2, 5));
	mymap.insert(make_pair(3, 5));

	pair<map<int, int>::iterator, map<int, int>::iterator>ret = mymap.equal_range(2);
	if ((*(ret.first)).second) {
		cout << "找到lower_boud!" << endl;
	}
	else {
		cout << "未找到lower_boud!" << endl;
	}
	if ((*(ret.second)).second) {
		cout << "找到uper_boud!" << endl;
	}
	else {
		cout << "未找到uper_boud!" << endl;
	}
}

int main(void) {
	//test01();
	//test02();
	test03();
	return 0;
}
Published 57 original articles · won praise 28 · views 4125

Guess you like

Origin blog.csdn.net/weixin_41747893/article/details/102942265