简体   繁体   中英

How to use strstr on vector string items?

vector<string> filtered_items; filtered_items.push_back("test");

for (auto index_name : filtered_items) 
    strstr(index_name, "te");

This throws the error -no matching function for call to 'strstr(std::basic_string&, char*)' because of the way index_name is used in strstr. How can I fix this?

strstr is a C function and does not know about std::string . Use std::string::find :

std::vector<std::string> filtered_items{ "test" };

for (const auto& index_name : filtered_items) {
    auto i = index_name.find("te");
    if ( i != std::string::npos) {
       std::cout << "found";
    }
}

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