STL中map映照容器由一个键值和一个映照数据组成,具有一一对应的关系。
结构为:键值--映照数据
例: aaa -- 111
bbb -- 222
ccc -- 333
键值不允许重复
使用map容器需要头文件#incude<map>
map的创建:map<键值类型,数据类型>
插入元素:
按照键值输出元素:
ps:map容器值得注意的是它会根据键值从小到大插入。
a.begin()为第一个键值地址
a.end()为最后一个空地址
a.find()的用法:find()用了搜索某个键值,如果搜索到了,和返回该键值所在的迭代器位置,
否则,返回end()迭代器位置。搜索速度极快。
一道关于搜索的例题体现find的用法:
You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.
Input
Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.
Output
Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as "eh".
Sample Input
Sample Output
具体代码如下:
用map实现数字分离:
sum等于15;
虽然博主没用过,但是感觉很高级
还可以让
虽然不知道有啥用吧!