简体   繁体   中英

C++ pointers and string vectors

//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";
    }
}

The code I have here adds a rather large text file of words to what I have declared as a string vector listWords . I would like to use strchr and various other Cstring functions to get rid of individual words with certain letters and characters. I'm coming across an error when trying to do so saying "No matching function call to strchr ." I have already included the libraries <vector> <string> <cstring> <fstream> . Pretty sure my error lies between the pointers.

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

Any tips to what I should do here to make strchr work?

Better

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

Worse

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM