65,186
社区成员




#include <iostream>
#include <map>
//#include <utility>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main(void)
{
map <string, int> word_count;
string word;
map <string, int>::iterator num;
ifstream f(".\\54.txt", ios::in | ios::trunc);
if (!f) //open file fail
{
cerr << "54.txt file not open!" << endl;
exit(1);
}
while (f >> word)
{
//insert element with key equal to word and value 1;
//if word already in word_count, insert does nothing;
pair <map <string, int>::iterator, bool> ret = word_count.insert(make_pair(word, 1));
if (!ret.second)
{
++(ret.first->second);
}
}
num = word_count.begin();
while (num != word_count.end())
{
cout << num->first;
cout << " : " << num->second << endl;
num++;
}
f.close();
return 0;
}
#include <iostream>
#include <map>
//#include <utility>
#include <fstream>
#include <string>
using namespace std;
int main(void)
{
map <string, int> word_count;
string word;
map <string, int>::iterator num;
ifstream f(".\\54.txt", ios::in |ios::trunc);
if (!f) //open file fail
{
cerr << "54.txt file not open!" << endl;
exit(1);
}
while (f >> word)
{
//insert element with key equal to word and value 1;
//if word already in word_count, insert does nothing;
pair <map <string, int>::iterator,bool> ret = word_count.insert(make_pair(word, 1));
if (!ret.second)
{
++(ret.first->second);
}
}
num = word_count.begin();
while (num != word_count.end())
{
cout << num->first;
cout << " : " << num->second << endl;
num++;
}
f.close();
return 0;
}