Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) SumStringLength

 

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:

 

 

 

 

 

 

C++98 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
    )
  );
}

 

 

 

 

 

Boost 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)
      )
    )
  );
}

 

 

 

 

 

C++11 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());
    }
  );
}

 

 

 

 

 

Testing code

 

 

 

 

 

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

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);
  }
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml