天天看點

C++ Map周遊和插入、其他API

Map周遊和插入

    //有序

    //關聯

    //映射

    //鍵值唯一

    //配置設定器allocator

map<int, string> MapHello; 
    map<int, string> MapHello1{ {1,"ddd"},{3,"asd"} };

	string ccc = MapHello[3]; //紅黑平衡二叉樹
	MapHello[3] = "Hello";

    MapHello.insert(make_pair(1, "CCCC"));
	MapHello.insert(pair<int, string>(2, "CCCC")); //插入
	MapHello.insert(map<int, string>::value_type(34, "CCCC"));
    MapHello.insert({ {1,"ddd"},{3,"asd"} });

	//for (const pair<int, string>& Tmp : MapHello)
	for (auto& Tmp : MapHello) //周遊
	{
		if (Tmp.first == 3)
		{
			cout << Tmp.second << endl;
			break;
		}
	}

	//周遊
	for (map<int, string>::iterator It = MapHello.begin(); It != MapHello.end(); It++)
	{
		if (It->first == 3)
		{
			cout << It->second << endl;
			break;
		}
	}

	cout << MapHello[3] << endl;
	cout << MapHello[1] << endl;
           

其他API

string ccc = MapHello.at(8);

	auto c1 = MapHello.equal_range(8); //pair

	MapHello.size();

	MapHello.erase(8);

	auto iterator111 = MapHello.find(8);

	//是否包含這個元素
	if (MapHello.count(7)) //判斷是否有
	{
		MapHello.insert(make_pair(7, "IIII"));
	}
           

繼續閱讀