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


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

     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 }
  • 相关阅读:
    ArcGIS SilverLight/WPF API 2.0版本已正式发布,新特性
    ArcGIS Server Flex API 自定义缩放控件的级数[代码]
    First
    HTML和JavaScript代码分离、平稳退化(1)
    cocos2dx 仿射变换
    java数组创建
    第一次看CCControl
    从零开始のcocos2dx生活(四)ActionManager
    从零开始のcocos2dx生活(二)Node
    从零开始のcocos2dx生活(三)Scheduler
  • 原文地址:https://www.cnblogs.com/borter/p/9417959.html
Copyright © 2020-2023  润新知