|
楼主 |
发表于 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).
- typedef map<string, int>::iterator PhoneIter;
- for( PhoneIter pos = phone_book.begin();
- pos != phone_book.end();
- ++pos )
- {
- if( pos->second == 101 )
- cout << pos->first << '\n';
- }
复制代码
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!) :
- using namespace std;
- template <typename Key, typename Value>
- class InitMap<Key, Value>
- {
- public:
- InitMap& operator<< ( const typename map<Key, Value>::value_type& v )
- {
- this->insert( v );
- return *this;
- }
- };
- int main()
- {
- InitMap<string, int> phone_book =
- InitMap<string, int>() << make_pair( "Freedom", 101 )
- << make_pair( "liuxiangbiao", 105 );
- }
复制代码
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. |
|