C++ 在文本文件中查找并标记单词
我需要阅读一个500字或更多的文本文件(报纸上的一篇真实文章等),然后像这样定位和标记,C++ 在文本文件中查找并标记单词,c++,C++,我需要阅读一个500字或更多的文本文件(报纸上的一篇真实文章等),然后像这样定位和标记,word,然后在屏幕上打印整个文章。我现在正在使用boostregex,它工作正常。我想尝试使用一个列表、数组或其他数据结构来创建一个州和主要城市的列表,并搜索这些州和主要城市,然后与Atile进行比较。现在我正在使用数组,但我愿意使用任何东西。有什么想法或线索吗 #include <boost/regex.hpp> #include <iostream> #include <s
word
,然后在屏幕上打印整个文章。我现在正在使用boostregex,它工作正常。我想尝试使用一个列表、数组或其他数据结构来创建一个州和主要城市的列表,并搜索这些州和主要城市,然后与Atile进行比较。现在我正在使用数组,但我愿意使用任何东西。有什么想法或线索吗
#include <boost/regex.hpp>
#include <iostream>
#include <string>
#include <boost/iostreams/filter/regex.hpp>
#include <fstream>
using namespace std;
int main()
{
string cities[389];
string states [60];
string filename, line,city,state;
ifstream file,cityfile, statefile;
int i=0;
int j=0;
cityfile.open("c:\\cities.txt");
while (!cityfile.eof())
{
getline(cityfile,city);
cities[i]=city;
i++;
//for (int i=0;i<500;i++)
//file>>cities[i];
}
cityfile.close();
statefile.open("c:\\states.txt");
while (!statefile.eof())
{
getline(statefile,state);
states[j]=state;
//for (int i=0;i<500;i++)
//cout<<states[j];
j++;
}
statefile.close();
//4cout<<cities[4];
cout<<"Please enter the path and file name "<<endl;
cin>>filename;
file.open(filename);
while (!file.eof())
{
while(getline(file, line)
{
}
while(getline(file, line))
{
//string text = "Hello world";
boost::regex re("[A-Z/]\.[A-Z\]\.|[A-Z/].*[:space:][A-Z/]|C........a");
//boost::regex re(
string fmt = "<locations>$&<locations\>";
if(boost::regex_search(line, re))
{
string result = boost::regex_replace(line, re, fmt);
cout << result << endl;
}
/*else
{
cout << "Found Nothing" << endl;
}*/
}
}
file.close();
cin.get(),cin.get();
return 0;
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
串城市[389];
字符串状态[60];
字符串文件名、行、城市、州;
ifstream文件、cityfile、statefile;
int i=0;
int j=0;
cityfile.open(“c:\\cities.txt”);
而(!cityfile.eof())
{
getline(城市文件,城市);
城市=城市;
i++;
//对于(int i=0;i>城市[i];
}
cityfile.close();
statefile.open(“c:\\states.txt”);
而(!statefile.eof())
{
getline(statefile,state);
状态[j]=状态;
//for(int i=0;i如果您追求渐进复杂性-提供线性时间复杂性(O(n+m)
)(n
和m
是输入字符串的长度)。用于在字符串中搜索字典
另一种方法是将标记化的单词放在映射中(其中值是每个字符串流中位置的列表),并在树中的数据中搜索每个字符串。复杂性将是O(|S|*(nlogn+mlogn))
(m
是搜索的字数,n
是字符串中的字数,S
是平均字长)您可以使用任何具有.find()
方法或支持std::find()
的容器。我会使用set
,因为set::find()
运行时间少于线性时间
下面是一个程序,它实现了您所说的功能。请注意,解析效果不太好,但这不是我要演示的。您可以继续使用解析器查找单词,并使用对set::find()
的调用来确定它们是否是位置
#include <set>
#include <string>
#include <iostream>
#include <sstream>
const std::set<std::string> locations { "Springfield", "Illinois", "Pennsylvania" };
int main () {
std::string line;
while(std::getline(std::cin, line)) {
std::istringstream iss(line);
std::string word;
while(iss >> word) {
if(locations.find(word) == locations.end())
std::cout << word << " ";
else
std::cout << "<location>" << word << "</location> ";
}
std::cout << "\n";
}
}
#包括
#包括
#包括
#包括
const std::设置位置{“斯普林菲尔德”、“伊利诺伊州”、“宾夕法尼亚州”};
int main(){
std::字符串行;
while(std::getline(std::cin,line)){
标准::istringstream iss(线);
字符串字;
while(iss>>word){
if(locations.find(word)=locations.end())
std::你能告诉我们到目前为止你有什么吗?我正在测试你给出的代码,并且>>和==有错误,我以前从未使用过istringstream,有什么想法吗?我给出的例子。我猜你缺少了一个#include
。有什么错误?红色的曲线在>>和>>下面==