c++中的单词搜索
Word search in C++
我编写了一个在文本文件中查找单词的代码。代码工作得很好,但问题是,我需要搜索是否存在确切的词,但它给出了相同的结果,即使它只是另一个词的一部分。例如,我正在寻找单词"Hello",但它说已经存在,因为txt文件有单词"Hello_World",这是不一样的,但包括它。我怎样才能检查准确的单词而忽略其他的呢?我想对单词的长度做点什么,忽略更长一些的东西,但不确定。代码在这里:
cout << "n Type a word: ";
getline(cin, someword);
file.open("myfile.txt");
if (file.is_open()){
while (!file.eof()){
getline(file, line);
if ((offset = line.find(someword, 0)) != string::npos){
cout << "n Word is already exist!! " << endl;
file.close();
}
}
file.close();
}
string line;
getline(file, line);
vector<string> words = TextToWords(line);
if (find(words.begin(), words.end(), someword) != words.end())
cout << "n Word already exists.n";
TextToWords
的实现取决于您。或者使用正则表达式库
使用以下代码拆分单词并搜索所需的单词:
#include <sstream>
stringstream ss(line);
while (getline(ss, tmp, ' ')){
if (tmp == someword){
//found
}
}
相关文章:
- 当您在此单词中搜索单词时调整字符数组的大小?
- 按单词搜索文件,然后在C++中打印特定行数
- 简单的二分搜索对我不起作用。我正在尝试从文本文件中搜索单词
- 如何搜索向单词添加字母的所有可能组合?
- 搜索单词时的字母模式逻辑
- Xapian - 搜索查询不检索匹配项,除非将""添加到每个单词
- Qt C++macOS问题。我正在使用函数 .find( "a word") 在多集中搜索单词,它适用于 Windows 但不适用于 mac
- 如何使用二叉搜索计算搜索单词所需的时间
- 读取每个单词的文件,并使用二叉搜索树(词典)对这些单词进行排序
- C++ 在 CSV 文件中搜索单词
- 从文件搜索搜索单词,然后将整个行复制到另一个文件
- C++,用于在大文件中的行上搜索单词的算法
- 使用 std 指令在文本文件中搜索单词
- 如何在文本文件中的字符串中搜索单词
- 在搜索单词时使用问号来代表任何字符
- 在一个非常大的文件中搜索单词
- 在字符串文本(1-D数组/c++)中搜索单词
- 在两个文本文件中搜索单词
- 如何在文本文件中搜索单词,如果找到,请打印出整行
- 使用C在文本中搜索单词,并显示该单词后面的信息