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:
- SumStringLength using C++98
- SumStringLength using Boost
- SumStringLength using C++11
SumStringLength using C++98
#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 ) ); }
SumStringLength using Boost
#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) ) ) ); }
SumStringLength using C++11
#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()); } ); }
Operating system(s) or programming environment(s)
- Lubuntu 12.10 (quantal)
- Qt Creator 2.5.2
- G++ 4.7.2
Libraries used:
- 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
#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); } }