2015-11-05 55 views
-2

是否可以使用算法方法来计算多少个字符串等于给定的字符串?使用算法搜索向量中的字符串

#include <algorithm> 
#include <vector> 
#include <string> 
int main(){ 
    vector<string> vectorPeople; 
    //assume that myVector isn't empty 
    string name; 
    cin >> name; 
    int total=std::count(myVector.begin(),myVector.end(),name); 

} 
+0

通过向量中的每个串循环使用'矢量 :: iterator',并比较字符串参数一个接一个 – 2015-11-05 23:08:53

+0

@willywonka_dailyblah有该方法。除此之外,该操作明确要求从算法API中提取方法。 – Paul

+0

@保罗哦,我明白了。 – 2015-11-05 23:41:14

回答

0

是。您可以使用std::count(您可以通过简单搜索轻松找到)。

#include <vector> 
#include <algorithm> 
#include <string> 

int count(std::vector<std::string> strings , std::string to_search){ 
    return std::count(strings.begin() , strings.end() , to_search); 
}