[STL] mapa de estructura / contenedor multimap, misiones, insertar, eliminar. . . . Y otras operaciones (fácil de aprender) y maletines

Mapa / contenedor multimap:

Mapa con respecto a la diferencia de conjuntos:

y mapa de llave que tiene un valor real, todos los elementos ordenados de forma automática de acuerdo con el valor de la clave. Par se conoce como un primer elemento clave, el segundo elemento se refiere como un valor real. Mapa también se basa en el mecanismo de aplicación subyacente para el árbol rojo-negro.

Podemos modificar las claves del mapa a través de los iteradores mapa?

La respuesta es no, la regla clave relacionada con la disposición de los elementos dentro del contenedor, cualquier cambio destruirá las teclas dispuestas en un contenedor normal, pero puede ser cambiado en términos reales.
** mapa y la diferencia multimap es que no se permite el mismo mapa de valor clave, MultiMap permitir existe el mismo valor de clave.

Mapa de Operaciones / contenedores multimap

Aquí Insertar imagen DescripciónAquí Insertar imagen DescripciónAquí Insertar imagen DescripciónAquí Insertar imagen Descripción

Caso (cómo golpear?)

#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;
}
Publicado 57 artículos originales · ganado elogios 28 · vistas 4125

Supongo que te gusta

Origin blog.csdn.net/weixin_41747893/article/details/102942265
Recomendado
Clasificación