如何使用' string::find '通过C++在文件中查找单词



我正在创建一个程序,该程序将打开一个文件并在文本中搜索所需的单词。我创建了以下单词bank。。。

Lawyer    
Smith Janes
Doctor    
Michael Zane
Teacher   
Maria Omaha

#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <string>
#include <sstream>   
using namespace std;   
int main ()    
{    
    // Declarations    
    string reply;    
    string inputFileName;    
    ifstream inputFile;    
    char character;
    cout << "Input file name: ";    
    getline(cin, inputFileName);
    // Open the input file.    
    inputFile.open(inputFileName.c_str());      
   // Check the file opened successfully.    
    if ( ! inputFile.is_open())
    {   
                  cout << "Unable to open input file." << endl;    
                  cout << "Press enter to continue...";    
                  getline(cin, reply);           
                  return 1;
    }

既然我把整个文件保存到一个字符串中,我怎么能在这个字符串中搜索呢对于我正在寻找的特定单词。。。

我正在从这个网站学习C++http://www.cprogramming.com/tutorial/lesson10.html

我想你用的是string::find,但我在这个网站旁边找不到太多关于如何搜索的参考资料。。

http://www.cplusplus.com/reference/string/string/find/

此部分将显示整个文件。

    string original;
    getline(inputFile, original, '');
    cout << original << endl;    
    cout << "nEnd of file reachedn" << endl;
    // Close the input file stream   
    inputFile.close();    
    cout << "Press enter to continue...";    
    return 0;      
}

这就是我认为这个程序应该采取的行动。。。

Please enter a word: Smith Janes
Smith Janes Lawyer
 another example.... 
Please enter a word: Doctor 
Michael Zane Doctor

find返回字符串中查找单词的位置(从零开始的偏移量)。如果没有找到单词,则返回npos

#include <string>
#include <iostream>
int main()
{
    std::string haystack("some string with words in it");
    std::string::size_type pos = haystack.find("words");
    if(pos != std::string::npos)
    {
        std::cout << "found "words" at position " << pos << std::endl;
    }
    else
    {
        std::cout << ""words" not found" << std::endl;
    }
}
#include <string>
#include <iostream>
#include <cstdlib>
int main() {
  std::string haystack = "LawyernSmith JanesnDoctornMichael ZanenTeachernMaria Omahan";
  std::string needle = "Janes";
  auto res = haystack.find(needle);
  if (std::string::npos == res) {
    std::cout << "Not foundn";
    std::exit(EXIT_FAILURE);
  }
  std::cout << res << 'n';
}

res是字符串中"Janes"所在点的索引(应为13)。

您所要求的功能似乎比在字符串中查找某些内容更为复杂。您显示的输出有一个用户输入,可以是名称或职业,输出是相关的职业或名称。

编写一个程序来显示"指针"所在的行很简单,或者显示总是显示前一行,或者总是显示下一行。但你所要求的是根据搜索到的内容显示其中一个或另一个。

我们可以实现这一点的一个简单方法是找出针是在偶数线上还是奇数线上,并以此为基础进行展示。

首先我们得到行号。

auto line_num = std::count(std::begin(haystack), std::begin(haystack) + res, 'n');

根据你展示的内容,职业是偶数行,名字是奇数行。我们可以很容易地获得我们想要的行号:

auto profession_line_num = line_num/2*2;
auto name_line_num = line_num/2*2 + 1;

接下来,我们可以将文本拆分成行,因为我们需要处理整行并按索引获取行。我下面展示的方法复制文本,效率很低,但很容易。

这里有一个分割函数:

std::vector<std::string> split(std::string const &s, std::string const &delims) {
    std::vector<std::string> res;
    std::string::size_type i = 0;
    auto found = s.find_first_of(delims, i);
    while (std::string::npos != found) {
        res.emplace_back(s, i, found-i);
        i = found+1;
        found = s.find_first_of(delims, i);
    }
    res.emplace_back(s, i);
    return res;
}

我们使用这样的分割函数:

auto lines = split(haystack, 'n');

现在,我们可以显示我们想要的线条。

std::cout << lines[name_line_num] << ' ' << lines[profession_line_num] << 'n';

一旦你把程序放在一起打印:

Smith Janes Lawyer

我认为这已经具备了您需要的所有信息。

http://www.cplusplus.com/reference/string/string/find/

最新更新