CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C++ 语言

请教 map 的一些基本用法。

楼主B2China(海陆空天电磁)2004-09-02 15:42:23 在 C/C++ / C++ 语言 提问

我定义了一个map  
   
  #include   <string>  
  #include   <iostream>  
  #include   <map>  
  using   namespace   std;  
   
  void   main()  
  {  
      map<string,string>   MyMap;//   第一个string是key,第二个string是value  
        ...  
  }  
   
  问题是:  
  1)   怎么往   MyMap   里面添加元素?  
  2)   怎么删除   MyMap   里面已有的元素?  
  3)   怎么知道   MyMap   的元素个数?  
  4)   如何根据键(key)查找相应的值(value)?    
        如设:mymap[]  
  5)   如何根据值(value)查找相应的键(key)?  
  6)   如何根据序号索引来查找相应的键key   和   对应的值   value   ?  
  7)   如何为map分配新的空间?是否是每添加一个元素就必须先为它申请空间?  
   
  以上都是关于map使用的一些基本问题,请大家讨论一下吧。 问题点数:50、回复次数:7Top

1 楼B2China(海陆空天电磁)回复于 2004-09-02 15:45:30 得分 0

4)   例如:如果有一个元素的key   ="key1",   要查找它的value   是多少?  
  5)   例如:如果有一个元素的value   ="value1",   要查找它的key   是多少?  
  Top

2 楼maxcode(人弋石马)回复于 2004-09-02 16:01:18 得分 20

问题太多了  
  看看这个也许会有用  
  Using   a   Map   as   an   Associative   Array  
  The   following   example   shows   the   use   of   a   map   as   an   associative   array.   The   map   is   used   as   a   stock   chart.   The   elements   of   the   map   are   pairs   in   which   the   key   is   the   name   of   the   stock   and   the   value   is   its   price:  
   
        //   cont/mapl.cpp  
   
        #include   <iostream>  
        #include   <map>  
        #include   <string>  
        using   namespace   std;  
   
        int   main()  
        {  
                /*create   map/associative   array  
                  *-keys   are   strings  
                  *-values   are   floats  
                  */  
                typedef   map<string,float>   StringFloatMap;  
   
                StringFloatMap   stocks;             //   create   empty   container  
   
                //insert   some   elements  
                stocks["BASF"]   =   369.50;  
                stocks["VW"]   =   413.50;  
                stocks["Daimler"]   =   819.00;  
                stocks["BMW"]   =   834.00;  
                stocks["Siemens"]   =   842.20;  
   
                //print   all   elements  
                StringFloatMap::iterator   pos;  
                for   (pos   =   stocks.begin();   pos   !=   stocks.end();   ++pos)   {  
                        cout   <<   "stock:   "   <<   pos->first   <<   "\t"  
                                  <<   "price:   "   <<   pos->second   <<   endl;  
                }  
                cout   <<   endl;  
   
                //boom   (all   prices   doubled)  
                for   (pos   =   stocks.begin();   pos   !=   stocks.end();   ++pos)   {  
                        pos->second   *=   2;  
                }  
   
                //print   all   elements  
                for   (pos   =   stocks.begin();   pos   !=   stocks.end();   ++pos)   {  
                        cout   <<   "stock:   "   <<   pos->first   <<   "\t"  
                                  <<   "price:   "   <<   pos->second   <<   endl;  
                }  
                cout   <<   endl;  
   
                /*rename   key   from   "VW"   to   "Volkswagen"  
                  *-only   provided   by   exchanging   element  
                  */  
                stocks["Volkswagen"]   =   stocks["VW"];  
                stocks.erase("VW");  
   
                //print   all   elements  
                for   (pos   =   stocks.begin();   pos   !=   stocks.end();   ++pos)   {  
                        cout   <<   "stock:   "   <<   pos->first   <<   "\t"  
                                  <<   "price:   "   <<   pos->second   <<   endl;  
                }  
        }  
  The   program   has   the   following   output:  
   
        stock:   BASF   price:   369.5  
        stock:   BMW   price:   834  
        stock:   Daimler   price:   819  
        stock:   Siemens   price:   842.2  
        stock:   VW   price:   413.5  
   
        stock:   BASF   price:   739  
        stock:   BMW   price:   1668  
        stock:   Daimler   price:   1638  
        stock:   Siemens   price:   1684.4  
        stock:   VW   price:   827  
   
        stock:   BASF   price:   739  
        stock:   BMW   price:   1668  
        stock:   Daimler   price:   1638  
        stock:   Siemens   price:   1684.4  
        stock:   Volkswagen   price:   827  
  Using   a   Multimap   as   a   Dictionary  
  The   following   example   shows   how   to   use   a   multimap   as   a   dictionary:  
   
        //   cont/mmap1.cpp  
   
        #include   <iostream>  
        #include   <map>  
        #include   <string>  
        #include   <iomanip>  
        using   namespace   std;  
   
        int   main()  
        {  
                //define   multimap   type   as   string/string   dictionary  
                typedef   multimap<string,string>   StrStrMMap;  
   
                //create   empty   dictionary  
                StrStrMMap   dict;  
   
                //insert   some   elements   in   random   order  
                dict.insert(make_pair("day","Tag"));  
                dict.insert(make_pair("strange","fremd"));  
                dict.insert(make_pair("car","Auto"));  
                dict.insert(make_pair("smart","elegant"));  
                dict.insert(make_pair("trait","Merkmal"));  
                dict.insert(make_pair("strange","seltsam"));  
                dict.insert(make_pair("smart","raffiniert"));  
                dict.insert(make_pair("smart","klug"));  
                dict.insert(make_pair("clever","raffiniert"));  
   
                //print   all   elements  
                StrStrMMap::iterator   pos;  
                cout.setf   (ios::left,   ios::adjustfield);  
                cout   <<   '   '   <<   setw(10)   <<   "english   "  
                          <<   "german   "   <<   endl;    
                cout   <<   setfil('-')   <<   setw(20)   <<   ""  
                          <<   setfil('   ')   <<   endl;  
                for   (pos   =   dict.begin();   pos   !=   dict.end();   ++pos)   {  
                        cout   <<   '   '   <<   setw(10)   <<   pos>first.c_str()  
                                  <<   pos->second   <<   endl;  
                }  
                cout   <<   endl;  
   
                //print   all   values   for   key   "smart"  
                string   word("smart");  
                cout   <<   word   <<   ":   "   <<   endl;  
   
                for   (pos   =   dict.lower_bound(word);  
                          pos   !=   dict.upper_bound(word);   ++pos)   {  
                                cout   <<   "   "   <<   pos->second   <<   endl;  
                }  
   
                //print   all   keys   for   value   "raffiniert"  
                word   =   ("raffiniert");  
                cout   <<   word   <<   ":   "   <<   endl;  
                for   (pos   =   dict.begin();   pos   !=   dict.end();   ++pos)   {  
                        if   (pos->second   ==   word)   {  
                                cout   <<   "   "   <<   pos->first   <<   endl;  
                        }  
                }  
        }  
  The   program   has   the   following   output:  
   
        english       german  
        --------------------  
          car               Auto  
          clever         raffiniert  
          day               Tag  
          smart           elegant  
          smart           raffiniert  
          smart           klug  
          strange       fremd  
          strange       seltsam  
          trait           Merkmal  
   
        smart:  
                elegant  
                raffiniert  
                klug  
        raffiniert:  
                clever  
                smart  
  Top

3 楼B2China(海陆空天电磁)回复于 2004-09-02 16:31:47 得分 0

//定义  
  map<int,   string>   mykey;   //第一个是Key,第二个是Value  
   
  //插入  
  mykey.insert(map<int,   string>::value_type(1,   "hello"));  
   
  //访问  
  cout   <<   mykey[1]   <<   endl;   //方括号里面是Key的值,如果Key是字符串,加引号就好了  
   
  //叠代  
  mykey::iterator   it;  
  for   (it   =   mykey.begin();   it!   =   mykey.end();   ++it)  
      cout   <<   "Key:   "   <<   it->first   <<   "\tValue:   "   <<   it->second;  
   
  //删除  
  mykey::iterator   it1;  
  for   (it1   =   mykey.begin();   it1!   =   mykey.end();   ++it1)  
      mykey.erase(it1);  
   
   
  我现在不知道怎么查找。根据key查找value或根据value查找key,或根据索引查找Top

4 楼Rave(胡言)回复于 2004-09-02 17:10:50 得分 10

[quote]  
  //删除  
  mykey::iterator   it1;  
  for   (it1   =   mykey.begin();   it1!   =   mykey.end();   ++it1)  
      mykey.erase(it1);  
  [/quote]  
   
  对it1所指元素实施erase(),   会导致it1无效,   不再是一个合法的iterator  
   
  删除要特别小心.  
   
  这样改:  
  mykey::iterator   it1;  
  for   (it1   =   mykey.begin();   it1!   =   mykey.end();)  
      mykey.erase(it1++);  
   
  it1++   会将   it1移向下一个元素,但返回其原始值的一个副本,   所以不用担心it1是否合法.  
   
   
  Top

5 楼pc2s(火麒麟)回复于 2004-09-02 21:28:55 得分 0

to:B2China(海陆空天电磁)   (   )   信誉:100    
  从我的回复里面粘过来~~~~~~~  
   
  //查找Map中是否存在inKey这个Key  
  for   (map<int,   string>::iterator   it   =   mykey.begin();   it!=mykey.end();   ++it)  
      if   (it->first   ==   inKey)   cout   <<   "Found"   <<   endl;  
   
  查找Value,就把it->first换成it->second就可以了Top

6 楼pc2s(火麒麟)回复于 2004-09-02 21:29:52 得分 10

to:B2China(海陆空天电磁)   (   )   信誉:100    
  我的意思是,居然从我给别人的回复中粘贴过来回复别人~~~~  
  还一点儿都不改动Top

7 楼daylove(爱晶如梦)(昨夜西风调碧树,独上高楼,望尽天涯路……)回复于 2004-09-03 10:04:21 得分 10

建议看看侯捷的:Effective   C++   2/e      
   
  你会豁然开朗。  
   
   
  ------------------------------------------------------《爱晶如梦》  
  『唯我独坐寒江』  
  『难为春城美景』  
  『秋黛依影随行』  
  『紫晶轻舞我心』  
  Top

相关问题

  • 关于map的基本用法
  • FastReport 的基本用法?
  • 简单问题 ListView基本用法
  • 基本控件的用法,请回答
  • 请教map的用法!!!
  • adodc的一些用法…
  • (新手)散20分!很高兴,又搞懂了一些基本控件的用法!睡觉前散20分,见者有份!
  • 请教使用TQuery的最基本的用法。
  • ☆NET命令的基本用法☆ (备份贴)
  • 求数据窗口基本用法的电子书,pb6.5???

关键词

  • b2c
  • smart
  • mykey
  • 查找
  • dict
  • pair
  • 元素
  • stocks
  • mymap
  • strstrmmap

得分解答快速导航

  • 帖主:B2China
  • maxcode
  • Rave
  • pc2s
  • daylove

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo