Joining a Sequence of Strings, join them together into a single, long string. - C++ STL

C++ examples for STL:string

Description

Joining a Sequence of Strings, join them together into a single, long string.

Demo Code

#include <string>
#include <vector>
#include <iostream>

using namespace std;

void join(const vector<string>& v, char c, string& s) {
   s.clear();/*  w  w w. ja va  2  s  .co m*/
   for (vector<string>::const_iterator p = v.begin();
        p != v.end(); ++p) {
      s += *p;
      if (p != v.end() - 1)
        s += c;
   }
}

int main() {

   vector<string> v;
   vector<string> v2;
   string s;

   v.push_back(string("a"));
   v.push_back(string("b"));
   v.push_back(string("c"));
   v.push_back(string("d"));

   join(v, '/', s);

   cout << s << '\n';
}

Result


Related Tutorials