find chars/string in string from vector c++

c++, c++11, find

Solution

You could use `std::count_if`:

auto cnt = count_if(begin(vec), 
                    end(vec), 
                    [&](const string& str) {
                      return str.find(value) != std::string::npos;
                    });

Note that this only counts the number of strings containing `"Ace"`, not the total number of occurrences of `"Ace"` in the vector's elements.

Problem

I have a vector of strings, and I want to count all 'Ace' in the vector. Right now I can only find one... ``` int main() { std::vector<string> vec; vec.push_back("Ace of Spades"); vec.push_back("Ace"); string value = "Ace"; int cnt = 0; auto iter = find_if(begin(vec), end(vec), [&](const string &str) { return str.find(value) != str.npos; }); if(iter == end(vec)) cout << "no found" << endl; else { cout << *iter << endl; cnt++; cout << cnt++ << endl; } } ```

Original source