Chinaunix首页 | 论坛 | 博客
  • 博客访问: 159544
  • 博文数量: 30
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 290
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-25 19:05
文章分类

全部博文(30)

文章存档

2009年(3)

2008年(27)

我的朋友

分类: C/C++

2008-11-27 15:13:56

声明

//count number of times each word occurs in the input

map word_count; //emptymap from string to int

 

//get an iterator to an element in word_count

map::iterator map_it=word_count.begin();

//*map_it is a refoerence to a pair object

cout<first;       //prints the key for this element

cout<<" "<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 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::value_type(“Anna”,1));

//equal

word_count.insert(make_pair(“Anna”,1));

//equal

typedef map::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::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”<

else cout<<”opps: “<

 

map对象的迭代遍历

//get iterator positioned on the first element

map::const_iterator map_it=word_count.begin();

//for each element in the map

while(map_it!=word_count.end){

       cout<first<<”occurs: “

<second<<”times”<

       ++map_it;  //increment iterator to denote the next element

}

 

map综合应用

单词转换map对象

#include

#include

#include//string

#include//ifstream

#include//istringstream

#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 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::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<

              }

              cout<    //done with this line of input

       }

       return 0;

}

 

 

 

 

阅读(3230) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~