小编Luc*_*dyt的帖子

减少字符串比较的时间复杂度

我有一个字典 .txt文件,可能有超过一千个单词及其定义.我已经编写了一个程序来从该文件中获取每行的第一个单词,并根据用户输入的字符串进行检查:

void checkWord(string input)
{

    std::ifstream inFile;
    inFile.open("Oxford.txt");
    if (inFile.is_open())
    {
        string line; //there is a "using std::string" in another file
        while (getline(inFile, line))
        {
            //read the first word from each line
            std::istringstream iss(line);
            string word;
            iss >> word;
            //make sure the strings being compared are the same case
            std::transform(word.begin(), word.end(), word.begin(), ::tolower);
            std::transform(input.begin(), input.end(), input.begin(), ::tolower);
            if (word == input)
            {
                //Do a thing with word
            }
        }
        inFile.close();
        return "End of file";
    }
    else
    {
        return …
Run Code Online (Sandbox Code Playgroud)

c++ string

2
推荐指数
1
解决办法
1198
查看次数

标签 统计

c++ ×1

string ×1