fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. int main(void)
  8. {
  9. std::vector<std::string> str;
  10. str.push_back("Vector");
  11. str.push_back("of");
  12. str.push_back("four");
  13. str.push_back("words");
  14.  
  15. int chars = accumulate(str.begin(), str.end(), 0, [](int sum, const string& elem) {return sum + elem.size();});
  16.  
  17. std::cout << "Number of characters: " << chars; // 17 characters
  18.  
  19. // Are there any STL methods that allows me to find 'chars'
  20. // without needing to write multiple for loops?
  21.  
  22. }
Success #stdin #stdout 0s 2984KB
stdin
Standard input is empty
stdout
Number of characters: 17