Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) Implode

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppImplode/CppImplode.pro

 

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.cpp

QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++

unix {
  QMAKE_CXXFLAGS += -Werror
}

 

 

 

 

 

./CppImplode/main.cpp

 

#include <cassert>
#include <string>
#include <vector>

const std::string Implode(
  const std::string& seperator,
  const std::vector<std::string>& v)
{
  std::string s;
  if (v.empty()) return s;
  s += v[0];
  const std::size_t sz = v.size();
  for (std::size_t i=1; i!=sz; ++i)
  {
    s += seperator + v[i];
  }
  return s;
}

int main()
{
  {
    const std::vector<std::string> v { "a", "b", "c" };
    const std::string s = Implode(",",v);
    const std::string expected { "a,b,c" };
    assert(expected == s);
  }
  {
    const std::vector<std::string> v {};
    const std::string s = Implode(",",v);
    const std::string expected { "" };
    assert(expected == s);
  }

}

 

 

 

 

 

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