【c/c++】map

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

//http://www.cplusplus.com/reference/map/map/

//This class couples together a pair of values, which may be of different types (T1 and T2). 
//The individual values can be accessed through its public members first and second.
typedef pair<string, int> PAIR;
//make_pair:Construct pair object

//ostream& operator<<(ostream& out, const PAIR & p) {
//return out << p.first << "\t" << p.second;
//}
int main()
{

	/*
	map<string, int> name_score_map;
	name_score_map["LiMin"] = 90;
	name_score_map["ZiLinMi"] = 79;
	name_score_map["BoB"] = 92;
	name_score_map.insert(make_pair("Bing", 99));
	name_score_map.insert(make_pair("Albert", 86));
	for (map<string, int>::iterator iter = name_score_map.begin();
	iter != name_score_map.end();
	++iter) {
	cout << iter->first <<":"<<iter->second<<endl; //如果是map中interator,那么其必须使用->,如果是pair中,则使用点号
	}
	*/
	/*
	pair <int, int> foo;
	pair <int, int> bar;

	foo = make_pair(10, 20);
	bar = make_pair(10.5, 'A'); // ok: implicit conversion from pair<double,char>  隐私转换

	std::cout << "foo: " << foo.first << ", " << foo.second << '\n';  //foo:10,20
	std::cout << "bar: " << bar.first << ", " << bar.second << '\n';   //bar:10,6
	*/


	map<char, int> mymap;

	// first insert function version (single parameter):
	//这里面其实是有两个步骤,第一个步骤就是map中可以存储键值对,这里运用pair来构建键值对
	//第二步:pair中可以使用make_pair()。也可以使用原本构造函数形式pair<char, int>('a', 100)
	mymap.insert(pair<char, int>('a', 100));
	mymap.insert(pair<char, int>('z', 200));

	//The single element versions  return a pair
	//with its member pair::first set to an iterator pointing to either the newly inserted element or to the element with an equivalent key in the map. 
	//The pair::second element in the pair is set to true if a new element was inserted or false if an equivalent key already existed.
	pair<map<char, int>::iterator, bool> ret;
	ret = mymap.insert(pair<char, int>('z', 500));
	if (ret.second == false) {
		std::cout << "element 'z' already existed";
		std::cout << " with a value of " << ret.first->second << '\n'; //ret.first中对应的是一个
	}

	// second insert function version (with hint position):
	map<char, int>::iterator it = mymap.begin();
	mymap.insert(it, pair<char, int>('b', 300));  // max efficiency inserting
	mymap.insert(it, pair<char, int>('c', 400));  // no max efficiency inserting

	// third insert function version (range insertion):
	map<char, int> anothermap;
	anothermap.insert(mymap.begin(), mymap.find('c'));

	// showing contents:it->first输出map中的key,it->second输出的是map中value
	std::cout << "mymap contains:\n";
	for (it = mymap.begin(); it != mymap.end(); ++it)
		std::cout << it->first << " => " << it->second << '\n';

	std::cout << "anothermap contains:\n";
	for (it = anothermap.begin(); it != anothermap.end(); ++it)
		std::cout << it->first << " => " << it->second << '\n';

	/*
	map<char, int> mymap;
	map<char, int>::iterator it;

	mymap['a'] = 50;
	mymap['b'] = 100;
	mymap['c'] = 150;
	mymap['d'] = 200;

	//find返回的是An iterator to the element, 因此找到以后,我们可以使用mymap.find('a')->second。(interator就好比是一个poniter)
	//if an element with specified key is found, or map::end otherwise.
	it = mymap.find('b');
	if (it != mymap.end())
	mymap.erase(it);//删除该元素

	// print content:
	std::cout << "elements in mymap:" << '\n';
	std::cout << "a => " << mymap.find('a')->second << '\n';
	std::cout << "c => " << mymap.find('c')->second << '\n';
	std::cout << "d => " << mymap.find('d')->second << '\n';
	*/
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值