C++ Primer笔记 从txt文件中查找某一个单词出现的次数/行号/该行自从出现位置的内容
2012-12-05 17:20
330 查看
p379
这里有个bug:需求是找出现的次数,当某行出现次数多于一次时,这个程序得到的就是一共出现在了多少行,
下面的运行结果为5次,实际element出现多过5次,只是出现在5行而已,所以这是个bug!
#pragma warning(disable: 4786)
#pragma warning(disable: 4503)
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <map>
#include <vector>
using namespace std;
//用来显示查找的该单词所在行的部分内容
void Show(vector<string>::const_iterator beg, vector<string>::const_iterator end)
{
while (beg != end)
{
cout<<*beg++<<" ";
}
}
int main()
{
ifstream infile("1.txt", ios::in);
if (!infile)
{
cerr<<"open file error!"<<endl;
exit(1);
}
string str;
unsigned int line = 0; //记录行号
string strFind("element"); //待查单词
map< unsigned int, vector<string> > element; //保存待查元素行号,每行部分内容
bool enter = false; //当某一行出现了待查元素后开始记录该行内容
while(getline(infile, str)) //读入一行至str
{
enter = false;
++ line;
istringstream stream(str); //用来从独到的一行内容中逐个取出单词
string word; //记录每行中的单独的每个单词
vector<string> sentence; //保存出现待查元素后该行的内容
while (stream >> word) //取出每行中每个单词
{
if (strFind == word) //是否为待查单词
{
enter = true;
}
if (true == enter) //开始记录待查单词所在行的内容
{
sentence.push_back(word);
}
}
if (true == enter)
{
element.insert(make_pair(line, sentence)); //把行号和内容保存起来
}
}
cout<<strFind<<"总共出现"<<element.size()<<"次!"<<endl;
for (map< unsigned int, vector<string> >::const_iterator iter = element.begin(); iter != element.end(); iter ++)
{
cout<<"第"<<iter->first<<"行: ";
Show((iter->second).begin(), (iter->second).end());
cout<<endl;
}
cout<<endl;
return 0;
}
1.txt和运行结果
这里有个bug:需求是找出现的次数,当某行出现次数多于一次时,这个程序得到的就是一共出现在了多少行,
下面的运行结果为5次,实际element出现多过5次,只是出现在5行而已,所以这是个bug!
#pragma warning(disable: 4786)
#pragma warning(disable: 4503)
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <map>
#include <vector>
using namespace std;
//用来显示查找的该单词所在行的部分内容
void Show(vector<string>::const_iterator beg, vector<string>::const_iterator end)
{
while (beg != end)
{
cout<<*beg++<<" ";
}
}
int main()
{
ifstream infile("1.txt", ios::in);
if (!infile)
{
cerr<<"open file error!"<<endl;
exit(1);
}
string str;
unsigned int line = 0; //记录行号
string strFind("element"); //待查单词
map< unsigned int, vector<string> > element; //保存待查元素行号,每行部分内容
bool enter = false; //当某一行出现了待查元素后开始记录该行内容
while(getline(infile, str)) //读入一行至str
{
enter = false;
++ line;
istringstream stream(str); //用来从独到的一行内容中逐个取出单词
string word; //记录每行中的单独的每个单词
vector<string> sentence; //保存出现待查元素后该行的内容
while (stream >> word) //取出每行中每个单词
{
if (strFind == word) //是否为待查单词
{
enter = true;
}
if (true == enter) //开始记录待查单词所在行的内容
{
sentence.push_back(word);
}
}
if (true == enter)
{
element.insert(make_pair(line, sentence)); //把行号和内容保存起来
}
}
cout<<strFind<<"总共出现"<<element.size()<<"次!"<<endl;
for (map< unsigned int, vector<string> >::const_iterator iter = element.begin(); iter != element.end(); iter ++)
{
cout<<"第"<<iter->first<<"行: ";
Show((iter->second).begin(), (iter->second).end());
cout<<endl;
}
cout<<endl;
return 0;
}
1.txt和运行结果
相关文章推荐
- C++ Primer笔记 从txt文件中查找某一个单词出现的次数/行号/该行自从出现位置的内容
- 统计文件中单词出现的行号及打印出该行内容
- 实现 从“c:\\test.txt”这个文件中查找 "mobent"字符串出现的次数,并且记录出现的位置
- C语言在文件中查找字符串是否在某行,显示行号和该行内容
- java 读取键盘输入到txt文件,统计每个单词出现的次数并输出
- 练习2-4:编一个程序统计文件中特定单词出现的次数(要求使用string类的运算符==来查找单词) .
- 从文件中查找出现次数最多的10个单词
- Java实现读取键盘输入保存到txt文件,再统计并输出每个单词出现次数的方法
- 查找文件中的某个内容出现的次数
- 从文件中查找出现次数最多的10个单词
- 读取一个.txt文件并计数每个单词出现的次数
- 练习2-4:编一个程序统计文件中特定单词出现的次数(要求使用string类的运算符==来查找单词)
- 文件夹下有子文件夹又有.txt,子文件夹又有子文件夹又有.txt和子文件夹,把txt文件上面的单词全打出来并统计相同单词出现次数
- 查找单词在文件中出现的位置及对应的行数
- linux查找某个文件中单词出现的次数
- map查找文件里面重复的行,并输出第一次出现的行号
- 查找单词序列在文章中出现的次数
- c语言统计从文件读取的文章中所有单词的出现次数
- 统计一篇英文文件中,单词出现的次数,并按单词的长度进行排序
- 从txt文件逐行读取数据到excel文件(包含POI的简单使用和excel文件的数据分列,出现次数统计)