Closed ppuedom closed 4 years ago
About the proposed solution on https://github.com/PacktPublishing/The-Modern-Cpp-Challenge/blob/master/Chapter03/problem_26/main.cpp
Maybe a bit more compact solution could be:
#include <iostream> #include <vector> #include <sstream> #include <cassert> using namespace std; template <typename C> string join_strings(C const & c, const char delimiter) { ostringstream oss; for (const auto &i : c) { if(oss.tellp()) oss << delimiter; oss << i; } return oss.str(); } int main() { auto l1 = {"one"s, "two"s, "three"s}; assert(join_strings(l1,' ') == "one two three"s ); auto l2 = {""s}; assert(join_strings(l2,' ') == ""s ); auto l3 = vector<string>(); assert(join_strings(l3,' ') == ""s ); auto l4= vector<string> {"one"}; // C str assert(join_strings(l4,' ') == "one"s ); return 0; }
About the proposed solution on https://github.com/PacktPublishing/The-Modern-Cpp-Challenge/blob/master/Chapter03/problem_26/main.cpp
Maybe a bit more compact solution could be: