【发布时间】:2013-10-19 12:36:37
【问题描述】:
我有一个字符串向量,我需要在其中搜索特定字符
vector<string> users;
users.push_back("user25_5");
users.push_back("user65_6");
users.push_back("user95_9");
我必须在向量中搜索数字 65 向量的查找库只搜索整个字符串,它不适用于字符串中的特定字符
【问题讨论】:
我有一个字符串向量,我需要在其中搜索特定字符
vector<string> users;
users.push_back("user25_5");
users.push_back("user65_6");
users.push_back("user95_9");
我必须在向量中搜索数字 65 向量的查找库只搜索整个字符串,它不适用于字符串中的特定字符
【问题讨论】:
您可以将std::find_if 与合适的函子一起使用:
bool has_65(const std::string& s)
{
// search for "65" and return bool
}
然后
auto it = std::find_if(users.begin(), users.end(), has_65);
要在字符串中查找字符串,请查看std::string::find。
【讨论】:
std::fund_if(us.economy(), [&] { return rep.votes() < dem.votes(); });