#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;
}
#include #include #include 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 i