1. map::at
1 #include
2 #include
3 #include
4 using namespace std;
5
6 int main(){
7 map
8 {"alpha", 0},
9 {"beta", 0},
10 {"gamma", 0}};
11
12 mymap.at("alpha") = 10;
13 mymap.at("beta") = 20;
14 mymap.at("gamma") = 30;
15
16 for (auto& x:mymap){
17 cout< 18 } 19 20 return 0; 21 } 2. make_pair example 1 // make_pair example 2 #include 3 #include 4 5 int main () { 6 std::pair 7 std::pair 8 9 foo = std::make_pair (10,20); 10 bar = std::make_pair (10.5,'A'); // ok: implicit conversion from pair 11 12 std::cout << "foo: " << foo.first << ", " << foo.second << '\n'; 13 std::cout << "bar: " << bar.first << ", " << bar.second << '\n'; 14 15 return 0; 16 } 3. map::begin/end 1 // map::begin/end 2 #include 3 #include 4 5 int main () 6 { 7 std::map 8 9 mymap['b'] = 100; 10 mymap['a'] = 200; 11 mymap['c'] = 300; 12 13 // show content: 14 for (std::map 15 std::cout << it->first << " => " << it->second << '\n'; 16 17 return 0; 18 } 4. map::insert(C++98) 1 // map::insert(C++98) 2 #include 3 #include 4 using namespace std; 5 int main () 6 { 7 map 8 9 // first insert function version (single parameter): 10 mymap.insert ( pair 11 mymap.insert ( pair 12 13 pair 14 ret = mymap.insert (pair 15 if (ret.second == false){ 16 cout<<"element 'z' already existed"; 17 cout<<"with a value of " << ret.first->second << '\n'; 18 } 19 20 //second insert function version (with hint position): 21 map 22 mymap.insert (it, pair 23 mymap.insert (it, pair 24 25 //third insert function version (range insertion): 26 map 27 anothermap.insert(mymap.begin(),mymap.find('c')); 28 29 // showing contents: 30 cout<<"mymap contains: \n"; 31 for (it = mymap.begin(); it!= mymap.end(); ++it) 32 cout< 33 34 cout<<"anothermap contains: \n"; 35 for(it=anothermap.begin(); it!=anothermap.end();++it) 36 cout< 37 38 return 0; 39 } 精彩内容
发表评论