Joining C-style strings with template - C++ STL

C++ examples for STL:string

Description

Joining C-style strings with template

Demo Code

#include <string>
#include <iostream>

const static int MAGIC_NUMBER = 4;

template<typename T>
void join(T* arr[], size_t n, T c, std::basic_string<T>& s) {
   s.clear();//from  w  w w.  ja v  a  2  s .  com

   for (int i = 0; i < n; ++i) {
      if (arr[i] != NULL)
         s += arr[i];
      if (i < n-1)
         s += c;
   }
}

int main() {
   std::wstring ws;

   wchar_t* arr[MAGIC_NUMBER];

   arr[0] = L"a";
   arr[1] = L"b";
   arr[2] = L"c";
   arr[3] = L"d";

   join(arr, MAGIC_NUMBER, L'/', ws);
}

Related Tutorials