Skip to content

Latest commit

 

History

History
206 lines (118 loc) · 11.7 KB

CppSumStringLength.md

File metadata and controls

206 lines (118 loc) · 11.7 KB

 

 

 

 

 

 

SumStringLength is a std::string and container code snippet to sum the length/size of all std::strings in a container.

 

There are multiple versions of SumStringLength:

 

 

 

 

 

 

 


#include <string> #include <numeric> #include <vector> int StringLengthAccumulator(const int sz = 0, const std::string& s = "") {   return sz + static_cast<int>(s.size()); } int SumStringLength(const std::vector<std::string>& v) {   return static_cast<int>(     std::accumulate(       v.begin(),       v.end(),       0,       StringLengthAccumulator     )   ); }

 

 

 

 

 

 


#include <string> #include <numeric> #include <vector> #include <boost/bind.hpp> #include <boost/lambda/lambda.hpp> int SumStringLength(const std::vector<std::string>& v) {   return static_cast<int>(     std::accumulate(       v.begin(),       v.end(),       0,       boost::bind(         std::plus<unsigned int>(),         boost::lambda::_1,         boost::bind<unsigned int>(&std::string::size, boost::lambda::_2)       )     )   ); }

 

 

 

 

 

 


#include <string> #include <numeric> #include <vector> int SumStringLengthCpp11(const std::vector<std::string>& v) {   return std::accumulate(v.begin(),v.end(),0,     [](int& init, const std::string& s)     {       return init + static_cast<int>(s.size());     }   ); }

 

 

 

 

 

Testing code

 

 

 

 

 

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

  • STL STL: GNU ISO C++ Library, version 4.7.2

 

 

 

 

 

Qt project file: CppSumStringLength.pro

 


TEMPLATE = app CONFIG += console CONFIG -= qt SOURCES += main.cpp QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++ -Werror

 

 

 

 

 

main.cpp

 


#include <string> #include <numeric> #include <vector> int SumStringLengthCpp11(const std::vector<std::string>& v) {   return std::accumulate(v.begin(),v.end(),0,     [](int& init, const std::string& s)     {       return init + static_cast<int>(s.size());     }   ); } int StringLengthAccumulator(const int sz = 0, const std::string& s = "") {   return sz + static_cast<int>(s.size()); } int SumStringLengthCpp98(const std::vector<std::string>& v) {   return static_cast<int>(     std::accumulate(       v.begin(),       v.end(),       0,       StringLengthAccumulator     )   ); } #include <boost/bind.hpp> #include <boost/lambda/lambda.hpp> int SumStringLengthBoost(const std::vector<std::string>& v) {   return static_cast<int>(     std::accumulate(       v.begin(),       v.end(),       0,       boost::bind(         std::plus<unsigned int>(),         boost::lambda::_1,         boost::bind<unsigned int>(&std::string::size, boost::lambda::_2)       )     )   ); } #include <cassert> int main() {   {     const std::vector<std::string> v = { "1","23","456","78","9" };     assert(SumStringLengthCpp11(v) == 9);     assert(SumStringLengthBoost(v) == 9);     assert(SumStringLengthCpp98(v) == 9);   }   {     const std::vector<std::string> v = { "1","23","456","78","9","01","23","456","78","9" };     assert(SumStringLengthCpp11(v) == 19);     assert(SumStringLengthBoost(v) == 19);     assert(SumStringLengthCpp98(v) == 19);   }   {     const std::vector<std::string> v = { "1" };     assert(SumStringLengthCpp11(v) == 1);     assert(SumStringLengthBoost(v) == 1);     assert(SumStringLengthCpp98(v) == 1);   }   {     const std::vector<std::string> v = { };     assert(SumStringLengthCpp11(v) == 0);     assert(SumStringLengthBoost(v) == 0);     assert(SumStringLengthCpp98(v) == 0);   } }