变量(指针、数组)被创建之后应当及时把它们初始化

变量(指针、数组)被创建之后应当及时把它们初始化,以防止把 未被初始化的变量当成右值使用。

 1 #include <iostream>
 2 #include <string>
 3 #include <map>
 4 
 5 using namespace std;
 6 
 7 //创建map的实例,整数(int)映射字符串(string)
 8 typedef map<int, string> INT2STRING;
 9 
10 //测试map容器
11 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
12 
13 int main(int argc, char** argv) {
14     //创建map对象theMap
15     INT2STRING theMap;
16     INT2STRING::iterator theIterator,it;
17 
18     //向theMap容器中添入数据,数字和字符串配对
19     //每个元素是一个映射对
20     theMap.insert(INT2STRING::value_type(0,"Zero"));
21     theMap.insert(INT2STRING::value_type(2,"Two"));
22     theMap.insert(INT2STRING::value_type(4,"Four"));
23     theMap.insert(INT2STRING::value_type(6,"Six"));
24     theMap.insert(INT2STRING::value_type(8,"Eight"));
25 
26     //显示map容器的所有对象
27     cout<<"theMap.begin()--theMap.end():"<<endl;
28     for (theIterator=theMap.begin();theIterator!=theMap.end();++theIterator){
29         cout<<(*theIterator).first;
30         cout<<","<<(*theIterator).second<<" ";
31     }
32     cout<<endl;
33 
34     //测试map容器key的惟一性
35     theMap.insert(INT2STRING::value_type(0,"Zero"));
36     theMap.insert(INT2STRING::value_type(1,"One"));
37     theMap.insert(INT2STRING::value_type(2,"Two"));
38     theMap.insert(INT2STRING::value_type(3,"Three"));
39     theMap.insert(INT2STRING::value_type(4,"Four"));
40     theMap.insert(INT2STRING::value_type(5,"Five"));
41     theMap.insert(INT2STRING::value_type(6,"Six"));
42     theMap.insert(INT2STRING::value_type(7,"Seven"));
43     theMap.insert(INT2STRING::value_type(8,"Eight"));
44     theMap.insert(INT2STRING::value_type(9,"Nine"));
45     //下列语句将不能插入到map容器中
46     theMap.insert(INT2STRING::value_type(5,"AAA"));
47 
48     //显示map容器的所有对象
49     cout<<"theMap.begin()--theMap.end():"<<endl;
50     for (theIterator=theMap.begin();theIterator!=theMap.end();++theIterator){
51         cout<<(*theIterator).first;
52         cout<<","<<(*theIterator).second<<" ";
53     }
54     cout<<endl;
55 
56     //按键给定的区间显示序列中的元素
57     cout<<"[theMap.lower_bound(3),theMap.upper_bound(8)] :"<<endl;
58     for (it=theMap.lower_bound(3);it!=theMap.upper_bound(8);it++) {
59         cout<<(*it).first;
60         cout<<","<<(*it).second<<" ";
61     }
62     cout<<endl;
63 
64     //显示theMap的状态信息
65     cout<<"theMap.size():"<<theMap.size()<<endl;
66     cout<<"theMap.max_size():"<<theMap.max_size()<<endl;
67     cout<<"theMap.count(15):"<<theMap.count(15)<<endl;
68 
69     // 从键盘上输入数字,显示对应的字符串
70     string theString = "";
71     int index;
72     for( ; ; )
73     {
74         cout << "Enter "q" to quit, or enter a Number: ";
75         cin >> theString;
76         if(theString == "q")
77             break;
78 
79         for(index = 0; index < theString.length(); index++){
80             theIterator = theMap.find(theString[index] - '0');
81             if(theIterator != theMap.end() ) 
82                 cout << (*theIterator).second << " ";
83             else    
84                 cout << "[err] ";
85         }
86         cout << endl;
87     }
88     return 0;
89 }
原文地址:https://www.cnblogs.com/borter/p/9417959.html