title: How to concatenate strings with space into one string with STL in c++
body: <p>Given a vector of string ["one", "two", "three"].</p>
  
  <p>Question> How do I convert it into "one two three"?</p>
  
  <p>I know the manual way to do it with a loop and would like to know whether
  there is a simpler way with STL function.</p>
  
  <p>// Updated based on suggestion that I should use <code>accumulate</code></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;algorithm&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  using namespace std;
  
  struct BindTwoStrings
  {
      string operator()(const string&amp; s1, const string&amp; s2) const {
          return s1.empty() ? s2 : s1 + " " + s2;
      }    
  };
  
  int main()
  {    
     vector&lt;string&gt; vecString {"one", "two", "three"};   
     string ret2;   
  
     ret2 = accumulate(vecString.begin(), vecString.end(), ret2, 
             [] (const string&amp; s1, const string&amp; s2) -&gt; string { 
                return s1.empty() ? s2 : s1 + " " + s2; });
     cout &lt;&lt; "ret2:\"" &lt;&lt; ret2 &lt;&lt; "\"" &lt;&lt; endl;
  
     string ret;
     ret = accumulate(vecString.begin(), vecString.end(), ret, BindTwoStrings());
  
     cout &lt;&lt; "ret:\"" &lt;&lt; ret &lt;&lt; "\"" &lt;&lt; endl;
     return 0;
  }
  </code></pre>