STL中的模板类map的简单例子

STL中的模板类map的简单例子 佟强 2008.11.5

    map的元素是由key和value两个分量组成的对偶(key,value)。key是键,value是与键key相关联的映射值。元素的键key是唯一的,给定一个key,就能唯一地确定与其相关联的另一个分量value。下面代码举例说明如何使用map,其中key和value的类型都是string类。

  1. #include <map>
  2. #include <string>
  3. #include <iostream>
  4. using namespace std;
  5. int main(int argc,char* argv[]){
  6.     map<string,string> m;
  7.     m["hello"]="world";
  8.     m["haha"] = "wokk";
  9.     cout<<m["hello"]<<endl;
  10.     map<string,string>::const_iterator iterator ;
  11.     for(iterator=m.begin();iterator!=m.end();iterator++){
  12.         cout<<"/t"<<iterator->first<<" = "<<iterator->second<<endl;
  13.     }
  14.     return 0;
  15. }
原文地址:https://www.cnblogs.com/zhangyunlin/p/6168122.html