【问题标题】:vector of string, need to search for a particular character in it字符串向量,需要在其中搜索特定字符
【发布时间】: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 向量的查找库只搜索整个字符串,它不适用于字符串中的特定字符

【问题讨论】:

    标签: c++ string vector find


    【解决方案1】:

    您可以将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(), [&amp;] { return rep.votes() &lt; dem.votes(); });
    • @KerrekSB 但至少你使用了 lambda :D
    • 所以我将 auto it = xxx 放在迭代器中,因为我想在向量中搜索数字列表
    • auto it = std::find_if(user.begin(), user.end(), has_65); cout
    • 那么,否决票是怎么回事?除了明显的错字之外,这个答案有什么严重错误吗?
    猜你喜欢
    • 2014-10-29
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    • 2018-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多