用STL解决单词统计程序
2009-09-05 12:33
423 查看
以前看到有人出了一道单词统计题,意思大概是, 有一个words.txt文件的内容如下:
some
are
born
great
some
achieve
greatness
and
some
have
greatness
thrust
upon
them
用程序读取该文件,统计相同的单词数,并按字符顺序写入另一个文件,如result.txt
achieve 1
and 1
are 1
born 1
great 1
greatness 2
have 1
some 3
them 1
thrust 1
upon 1
现用两种STL方法(即vector和map)解如下:
/////////////////////////////////vector//////////////////////////
Code:
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Count
{
string name;
double val;
Count(string n="",double v=0):name(n),val(v){}
};
bool sort_name(const Count & m1, const Count & m2)
{
return strcmp(m1.name.c_str(),m2.name.c_str())<=0 ? true:false;
}
vector<Count> counts;
double& value(const string& s)
{
for (int i=0;i<counts.size();i++)
{
if (s==counts[i].name)
return counts[i].val;
}
counts.push_back(Count(s));
return counts[counts.size()-1].val;
}
int main()
{
ifstream ifs("words.txt");
string buf;
while(ifs)
{
if(getline(ifs,buf))
value(buf)++;
}
ifs.close();
sort(counts.begin(), counts.end(),sort_name);
ofstream ofs("result.txt");
for(vector<Count>::const_iterator p=counts.begin();p!=counts.end();++p)
{
cout<<p->name<<'/t'<<p->val<<'/n';
ofs<<p->name<<'/t'<<p->val<<'/n';
}
ofs.close();
return 0;
}
//////////////////////////////////////////////map/////////////////////////////////////////
Code:
#include <fstream>
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
typedef map<string,int>::const_iterator CI;
void Insert(map<string,int>& m)
{
string word;
int val=1;
ifstream ifs("words.txt");
while (ifs)
{
if(getline(ifs,word))
m[word]+=val;
}
ifs.close();
}
int main()
{
map<string,int> tbl;
Insert(tbl);
ofstream ofs("result.txt");
for (CI p=tbl.begin();p!=tbl.end();++p)
{
cout<<p->first<<'/t'<<p->second<<'/n';
ofs<<p->first<<'/t'<<p->second<<'/n';
}
ofs.close();
return 0;
}
some
are
born
great
some
achieve
greatness
and
some
have
greatness
thrust
upon
them
用程序读取该文件,统计相同的单词数,并按字符顺序写入另一个文件,如result.txt
achieve 1
and 1
are 1
born 1
great 1
greatness 2
have 1
some 3
them 1
thrust 1
upon 1
现用两种STL方法(即vector和map)解如下:
/////////////////////////////////vector//////////////////////////
Code:
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Count
{
string name;
double val;
Count(string n="",double v=0):name(n),val(v){}
};
bool sort_name(const Count & m1, const Count & m2)
{
return strcmp(m1.name.c_str(),m2.name.c_str())<=0 ? true:false;
}
vector<Count> counts;
double& value(const string& s)
{
for (int i=0;i<counts.size();i++)
{
if (s==counts[i].name)
return counts[i].val;
}
counts.push_back(Count(s));
return counts[counts.size()-1].val;
}
int main()
{
ifstream ifs("words.txt");
string buf;
while(ifs)
{
if(getline(ifs,buf))
value(buf)++;
}
ifs.close();
sort(counts.begin(), counts.end(),sort_name);
ofstream ofs("result.txt");
for(vector<Count>::const_iterator p=counts.begin();p!=counts.end();++p)
{
cout<<p->name<<'/t'<<p->val<<'/n';
ofs<<p->name<<'/t'<<p->val<<'/n';
}
ofs.close();
return 0;
}
//////////////////////////////////////////////map/////////////////////////////////////////
Code:
#include <fstream>
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
typedef map<string,int>::const_iterator CI;
void Insert(map<string,int>& m)
{
string word;
int val=1;
ifstream ifs("words.txt");
while (ifs)
{
if(getline(ifs,word))
m[word]+=val;
}
ifs.close();
}
int main()
{
map<string,int> tbl;
Insert(tbl);
ofstream ofs("result.txt");
for (CI p=tbl.begin();p!=tbl.end();++p)
{
cout<<p->first<<'/t'<<p->second<<'/n';
ofs<<p->first<<'/t'<<p->second<<'/n';
}
ofs.close();
return 0;
}
相关文章推荐
- 用STL解决单词统计程序
- 单词统计程序
- 练习2-4:编一个程序统计文件中特定单词出现的次数(要求使用string类的运算符==来查找单词) .
- HNUOJ 12609 统计单词 2013年“程序之星”ACM程序设计大赛(水题一A纪念)
- 第六篇:Eclipse上运行第一个Hadoop实例 - WordCount(单词统计程序)
- [原创]c++,自己写的一个统计输入的单词个数,行数和字符总数的小程序
- 统计文本文件中单词出现频率,自己编写的Java小程序
- 英文单词统计程序
- C#中HashTable和快速排序的用法,从单词频率统计小程序写起
- bit-map程序(解决已知某个文件内包含一些电话号码,每个号码为8位数字,统计不同号码的个数)
- Python函数式编程解决单词中字符频统计问题
- C++STL--map和set 词频统计和单词转换
- 【hadoop】 3002-mapreduce程序统计单词个数示例
- 一个利用map统计一段英文文章中每个单词出现次数的小程序
- STL统计英文中单词出现频率的问题
- 《C++ Primer》P314中使用insert重写单词统计程序的扩展
- Hadoop示例程序之单词统计MapReduce
- 利用visual VM 分析统计单词频率程序
- Hadoop示例程序之单词统计MapReduce