STL之map

使用map统计次数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <map>
 
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
 
void main()
{
    typedef map<string,int>::iterator mit;
    map<string,int> ma;
    mit it;
 
    ifstream in("in.txt");
    int c=0;
    string str;
    string st;
    while( getline(in,str) )
    {
        istringstream ist(str);
        while( ist>>st )
        {
            it=ma.find(st);
            if( it!=ma.end() )
                ++(*it).second;
            else
                ma[st]=1;
        }
    }
    for( it=ma.begin(); it!=ma.end(); ++it )
        cout<<(*it).first<<" "<<(*it).second<<endl;
}

 

posted @ 2015-04-16 12:20  郑帅兵  阅读(100)  评论(0)    收藏  举报