您的位置:首页 > 编程语言 > C语言/C++

C++中的关联容器map用法

2009-07-21 16:17 579 查看
声明
//count number of times each word occurs in the input
map<string,int> word_count; //emptymap from string to int

//get an iterator to an element in word_count
map<string,int>::iterator map_it=word_count.begin();
//*map_it is a refoerence to a pair<const string, int> object
cout<<map_it->first; //prints the key for this element
cout<<" "<<map_it->second; //prints the value of the element
map_it->first="new key"; //error: key is const
++map_it->second; //ok: we can change value through an iterator

下标行为编程应用:单词统计
//count number of times each word occurs in the input
map<string, int> word_count; //empty map from string to int
string word;
while(cint>>word)
++word_count[word];
这段程序创建一个map对象,用来记录每个单词出现的次数。while循环每次从标准输入读取一个单词,如果这是一个新的单词,则在word_count中添加以该单词为索引的新元素。如果读入的单词已经在map对象中,则将它所对应的值加1。
在单词第一次出现时,会在word_count中创建并插入一个以该单词位索引的新元素,同时将它的值初始化为0,然后其值立即加1,所以每次在map中添加新元素时,所统计的出现次数正好从1开始。

map::insert
//if Anna not already in word_count, insert new element with value 1
word_count.insert(map<string, int>::value_type(“Anna”,1));
//equal
word_count.insert(make_pair(“Anna”,1));
//equal
typedef map<string, int>::value_type valType;
word_count.insert(valType(“Anna”,1));

读取元素而又不插入该元素
int occurs=0;
if(word_count.count(“foobar”))
occurs=word_count[“foobar”];
//equal
int occurs=0;
map<string, int>::iterator it=word_count.find(“foobar”);
if(it!=word_count.end())
occurs=it->second;
count函数求出某键出现的次数,而find操作则返回一个迭代器,指向第一个拥有正在查找的实例。

从map对象中删除元素
//erase of a key returns number of elements removed
if(word_count.erase(removal_word))
cout<<”ok”<<removal_word<<”removed/n”;
else cout<<”opps: “<<removal_word<<” not found!/n”;

map对象的迭代遍历
//get iterator positioned on the first element
map<string, int>::const_iterator map_it=word_count.begin();
//for each element in the map
while(map_it!=word_count.end){
cout<<map_it->first<<”occurs: “
<<map_it->second<<”times”<<endl;
++map_it; //increment iterator to denote the next element
}

map综合应用
单词转换map对象
#include<iostream>
#include<map>
#include<string>//string
#include<fstream>//ifstream
#include<sstream>//istringstream
#include<iterator>
using namespace std;
ifstream& open_file(ifstream &in,const string &file) {
in.close();
in.clear();
in.open(file.c_str());
return in;
}
//A program to transform words.
//Takes two arguments: The first is name of the word transformation file
// The second is name of the input to transform
int main(int argn, char* argv[]){
if (argn!=3) throw runtime_error("wrong number of arguments!");
//open transformation file and check that open succeeded
ifstream map_file;
if(!open_file(map_file, argv[1])) throw runtime_error("no dictionary file!");
//map to hold the word transformation pairs:
//key is the word to look for in the input; value is word to use in the output
map<string,string> trans_map;
string key,val;
//read the transformation map and build the map
while(map_file>>key>>val)
trans_map.insert(make_pair(key,val));
//ok, now we're ready to do the transformations
//open the input file and check that the open succeeded
ifstream input;
if(!open_file(input,argv[2])) throw runtime_error("no input file");
string line; //hold each line from the input
//read the text to transform it a line at a time
while(getline(input,line)){
istringstream stream(line); //read the line a word at a time
string word;
bool firstword=true; //controls whether a space is printed
while(stream>>word){
//ok: the actual mapwork, this part is the heart of the program
map<string,string>::const_iterator map_it=trans_map.find(word);
//if this word is in the transformation map
if (map_it!=trans_map.end())
// replace it by the transformation value in the map
word=map_it->second;
if (firstword)
firstword=false;
else
cout<<" "; //print space between words
cout<<word;
}
cout<<endl; //done with this line of input
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: