-1
//Add words from the file to the vector
while (inputFile >> word) {
    listWords.push_back(word);
    wordCount +=1;  //Count the words
}

for (int i = 0; i < listWords.size(); i++) {
    char *p;
    p = strchr(listWords.at(i), 'c');
    if ( p != NULL ) {
        cout << "null";
    }
}

我在这里的代码将一个相当大的单词文本文件添加到我声明为 string 的内容vector listWords中。我想使用strchr和其他各种 Cstring 函数来摆脱带有某些字母和字符的单个单词。我在尝试这样做时遇到错误,说“没有匹配的函数调用strchr。” 我已经包含了库<vector> <string> <cstring> <fstream>。很确定我的错误在于指针之间。

char *strchr(const char *str, int ch)

关于我应该在这里做什么以使 strchr 工作的任何提示?

4

1 回答 1

1

更好的

for (int i = 0; i < listWords.size(); i++) {
    const auto p = listWords.at(i).find('c');
    if ( p != std::string::npos ) {
        cout << "null";
    }
}

更差

for (int i = 0; i < listWords.size(); i++) {
    const char *p = strchr(listWords.at(i).c_str(), 'c');
    if ( p != NULL ) {
        cout << "null";
    }
}
于 2018-03-07T04:34:01.810 回答