map

来源:互联网 发布:网络系统工程师考试 编辑:程序博客网 时间:2024/05/16 14:30

#include <iostream>
#include <fstream>
#include <string>
#include <map>
using namespace std;

void main()
{
 //定义map 对象
 map<string,int> word;
 //定义指针
 map<string,int>::iterator it;
 //向word 插入元素 ("a",9)
 word.insert (map<string,int>::value_type("a",9));

 //查找 键是"a"的元素,返回指向元素的指针。
 it=word.find ("a");
 //如果元素不存在,指针指向word.end().
 if(it!=word.end ())
  cout<<it->second<<endl; //输出元素的值

 //查找 键是"a"的元素,
 int result=word.count ("a");
 //如果键存在返回1,否则返回0
 if(result)
  cout<<word["a"]<<endl; //输出元素的值 

 cout<<endl;
}

0 0
原创粉丝点击