Reputation: 313
I have a 2D vector of strings and want to count how many times a certain word is repeated. For example:
#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" );
}
But the above gives errors. How can I do this?
Upvotes: 0
Views: 2314
Reputation: 2019
You need to run count on search individual vector and sum the results:
#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" );
}
}
Upvotes: 3