我有一个字符串的2D矢量,想要计算某个单词重复的次数。例如:
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
vector< vector<string> > vec(4, vector<string>(4, "word") );
count( vec.begin(), vec.end(), "certain word" );
}
但上面给出了错误。我怎么能这样做?
答案 0 :(得分:2)
您需要对搜索单个向量运行计数并对结果求和:
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
vector< vector<string> > vec(4, vector<string>(4, "string of words") );
size_t sum = 0;
for(auto& v: vec) {
sum += count( v.begin(), v.end(), "certain word" );
}
}