分类: C/C++
2008-11-27 15:13:56
声明
//count number of times each word occurs in the input
map
//get an iterator to an element in word_count
map
//*map_it is a refoerence to a pair
cout<
cout<<" "<
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 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
//equal
word_count.insert(make_pair(“Anna”,1));
//equal
typedef map
word_count.insert(valType(“Anna”,1));
读取元素而又不插入该元素
int occurs=0;
if(word_count.count(“foobar”))
occurs=word_count[“foobar”];
//equal
int occurs=0;
map
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”<
else cout<<”opps: “<
map对象的迭代遍历
//get iterator positioned on the first element
map
//for each element in the map
while(map_it!=word_count.end){
cout<
<
++map_it; //increment iterator to denote the next element
}
map综合应用
单词转换map对象
#include
#include
#include
#include
#include
#include
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 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
//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<
}
cout<
}
return 0;
}