Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在搜索代表任何字符的单词时使用问号_C++_String_Dictionary - Fatal编程技术网

C++ 在搜索代表任何字符的单词时使用问号

C++ 在搜索代表任何字符的单词时使用问号,c++,string,dictionary,C++,String,Dictionary,对于我试图编写的程序,先读入一个.txt文件,然后计算字数(我使用map)。程序会提示用户输入要查找的单词,然后显示该单词在文件中出现的次数。我应该有一个包含“?”的部分来代替单词中的字母,并打印文件中单词符合描述的次数。“?”可以代表另一个字母或空白。我不知道如何使用这个,任何帮助都将不胜感激 例如: 请输入一个单词:a?? “和”一词在文档中出现4次。单词as在文档中出现1次。单词a在文档中出现一次 问号字符由用户输入,可以代表任何其他字符。该程序应该搜索该文件,并找到和计算任何适合的可能性

对于我试图编写的程序,先读入一个.txt文件,然后计算字数(我使用map)。程序会提示用户输入要查找的单词,然后显示该单词在文件中出现的次数。我应该有一个包含“?”的部分来代替单词中的字母,并打印文件中单词符合描述的次数。“?”可以代表另一个字母或空白。我不知道如何使用这个,任何帮助都将不胜感激

例如: 请输入一个单词:a?? “和”一词在文档中出现4次。单词as在文档中出现1次。单词a在文档中出现一次

问号字符由用户输入,可以代表任何其他字符。该程序应该搜索该文件,并找到和计算任何适合的可能性。例如,如果用户输入了?d,则单词“and”可能是计算的内容

#include <iostream>
#include <map>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char *argv[]) {
    if (argc != 2)
        cout << "invalid usage" ;
    //open file
    ifstream infile;
    infile.open(argv[1]);
    //variables
    map<string, unsigned int> wCount;
    int count = 0;
    int uCount = 0;
    //lowercase
    while (!infile.eof()) {
        string text;
        infile >> text;
        for (int i=0; i < text.length(); i++) {
            text[i] = tolower(text[i]);
        }

        //count words
        if (wCount.find(text) == wCount.end()) {
            wCount[text] = 1;
            count++;
            uCount++;
        }
        else {
            wCount[text]++;
            count++;
        }
    }
    cout << "The total number of word found in the file was " << count << endl;
    cout << "The number of unique words found was " << uCount << "." << endl;
    cout << "Enter the word you want to find: " << "";
    //find(infile, word);
    string in = "";
    getline(cin, in);
    while(in != "EOF") {
            cout << "The word " << in << " appears " << wCount[in] << " times in the file" << endl;
            cout << "Please enter a word: " << "";
            getline(cin, in);
    }

    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[]){
如果(argc!=2)
cout>文本;
对于(int i=0;i我喜欢这样的问题。它们在计算机科学的许多不同领域都有应用;尤其是生物信息学。这应该让你开始。按照Galik的建议,您可以:

1) 从字符串中删除问号字符

std::string strip_wild_card(const std::string &pattern) {
    int q_index = pattern.find("?");
    return pattern.substr(0, q_index);
}
2) 在文本中查找模式的引用

bool wild_card_compare(const std::string &pattern, const std::string &text) {
    //implement this
    if(pattern is found in text)
        return true
    else
        return false
}

提示:要检查这一点,我们可以使用STL中的函数。希望这有帮助。祝你好运

我喜欢这样的问题。它们在计算机科学的许多不同领域都有应用;尤其是生物信息学。这应该让你开始。按照Galik的建议,您可以:

1) 从字符串中删除问号字符

std::string strip_wild_card(const std::string &pattern) {
    int q_index = pattern.find("?");
    return pattern.substr(0, q_index);
}
2) 在文本中查找模式的引用

bool wild_card_compare(const std::string &pattern, const std::string &text) {
    //implement this
    if(pattern is found in text)
        return true
    else
        return false
}

提示:要检查这一点,我们可以使用STL中的函数。希望这有帮助。祝你好运

你的例子不清楚。你能举一个更具体的例子吗?@ChrisD希望这能有所帮助!我想你必须把你列表中的每个单词和输入的单词一次一个字母地进行比较。如果输入的字母是“?”,则不管您匹配的是哪个字母,它都会通过,否则字母必须相同。。首先,我将编写一个函数来比较两个字符串,并考虑特殊的
“?”
字符:
bool通配符\u比较(字符串通配符,字符串字){/*…*/}/code>。返回
true
如果它们匹配,则返回
false
。然后考虑将该函数集成到您当前的代码中。您的示例并不清楚。你能举一个更具体的例子吗?@ChrisD希望这能有所帮助!我想你必须把你列表中的每个单词和输入的单词一次一个字母地进行比较。如果输入的字母是“?”,则不管您匹配的是哪个字母,它都会通过,否则字母必须相同。。首先,我将编写一个函数来比较两个字符串,并考虑特殊的
“?”
字符:
bool通配符\u比较(字符串通配符,字符串字){/*…*/}/code>。返回
true
如果它们匹配,则返回
false
。然后考虑将该函数集成到当前代码中。