LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 709|回复: 4

Map的问题。

[复制链接]
发表于 2003-8-28 21:10:07 | 显示全部楼层 |阅读模式
下面是我的关于map的程序,

  1. #include<iostream>
  2. #include<istream>
  3. #include<map>

  4. using namespace std;

  5. int main()
  6. {
  7.    map<string,int> phone_book;
  8.    phone_book[string("freedom")]=101;
  9.    phone_book[string("xiangbuidler")]=102;
  10.    cout<<phone_book["xiangbuilder"]<<'\n';
  11. }
复制代码

下面是我的问题:
一, 为什么程序运行时,"freedom"的值是101,而"xiangbuilder"的是0?
如果我不通过下面的方法
  1. phone_book.insert(
  2.    map<string,int>::
  3.       value_type(string("xiangbuilder"),102)
  4. );
复制代码
来对map赋值的话,怎么避免这种现象,即"liuxiangbaio"的值是0的出现?
二,如果不使用class和pointer,能不能通过把string的值,比如102,告诉机器,来得到string的内容?
我想做的是当我输入一些代码,类似于
  1. cont<<phone_book[101];
复制代码
,时,机器输出"freedom".当然我上面的这句代码是不合法的的。.
三,怎么只在一句代码中对map进行赋值,就象对这个structure赋值似的
  1. Entry phone_book[3]={ {"freedom",1010101}, {"xiangbuidler",1000000}, {"liuxiangbiao",1234567} };
复制代码
?

谢谢。
发表于 2003-8-28 21:27:33 | 显示全部楼层
第一个问题:
phone_book[string("xiangbuidler")]=102;
cout<<phone_book["xiangbuilder"]<<'\n';
两句里的xiangbuilder不一样,l和d反了。
 楼主| 发表于 2003-8-28 22:44:13 | 显示全部楼层
抱歉, 我的输入错误.

当phone_book[string("xiangbuidler")]=102;
cout<<phone_book["xiangbuilder"]<<'\n';
两句里的xiangbuilder一样时,仍会出现我提出的三个问题.
谢谢。
发表于 2003-8-28 23:33:32 | 显示全部楼层
你这个l和d还是反的,我调过来以后编译了,输出是102
 楼主| 发表于 2003-8-29 09:39:12 | 显示全部楼层
quote from dakensta:
关于第二个问题,
Well, maps are sorted containers based on the <key>, not <value>. You could iterate through the map comparing iter->second to the number you provide until a value is found (or return this from a function).

  1. typedef map<string, int>::iterator PhoneIter;   
  2. for( PhoneIter pos = phone_book.begin();
  3. pos != phone_book.end();
  4. ++pos )
  5. {
  6. if( pos->second == 101 )
  7.   cout << pos->first << '\n';
  8. }
复制代码

If you derive your own class from std::map you could probably get the syntax you are looking for.
第三个问题,
I am not sure about the exact syntax for this but I used inline initialisation of a vector something like this (adapted for use with a map!) :

  1. using namespace std;
  2. template <typename Key, typename Value>
  3. class InitMap<Key, Value>
  4. {
  5.   public:
  6.   InitMap& operator<< ( const typename map<Key, Value>::value_type& v )
  7.    {
  8.       this->insert( v );
  9.       return *this;
  10.    }
  11. };

  12. int main()
  13. {
  14.   InitMap<string, int> phone_book =
  15.         InitMap<string, int>() << make_pair( "Freedom", 101 )
  16.                                << make_pair( "liuxiangbiao", 105 );
  17. }
复制代码

I don't think this is especially pretty and I only used it for in line initialisation of classes taking a vector in the constructor but maybe it will help you.
第二个问题中的代码,编译成功,第三个问题中的代码,编译失败, 我刚接触程序设计, dakensta给出的第三个问题中的代码中,有许多对我来说是高深的东西, 先copy下来, 以后再体会吧.

Hope this is of help to someone.
Thank you libinary for your patience and earnest circumspection.
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表