title: The std::transform-like function that returns transformed container
body: <p>I'm trying to implement a function similar to <code>std::transform</code> algorithm but instead of taking the output iterator by an argument I want to create and return a container with transformed input elements. </p>
  
  <p>Let's say that it's named <code>transform_container</code> and takes two arguments: container and functor. It should return the same container type but possibly parametrized by a different element type (the Functor can return element of different type).</p>
  
  <p>I'd like to use my function as in the example below:</p>
  
  <pre><code>std::vector&lt;int&gt; vi{ 1, 2, 3, 4, 5 };
  auto vs = transform_container(vi, [] (int i) { return std::to_string(i); }); 
  //vs will be std::vector&lt;std::string&gt;
  assert(vs == std::vector&lt;std::string&gt;({"1", "2", "3", "4", "5"}));
  
  std::set&lt;int&gt; si{ 5, 10, 15 };
  auto sd = transform_container(si, [] (int i) { return i / 2.; }); 
  //sd will be of type std::set&lt;double&gt;
  assert(sd == std::set&lt;double&gt;({5/2., 10/2., 15/2.}));
  </code></pre>
  
  <p>I was able two write two functions — one for <code>std::set</code> and one for <code>std::vector</code> — that seem to work properly. They are identical, except of the container typename. Their code is listed below.</p>
  
  <pre><code>template&lt;typename T, typename Functor&gt;
  auto transform_container(const std::vector&lt;T&gt; &amp;v, Functor &amp;&amp;f) -&gt; std::vector&lt;decltype(f(*v.begin()))&gt;
  {
      std::vector&lt;decltype(f(*v.begin()))&gt; ret;
      std::transform(std::begin(v), std::end(v), std::inserter(ret, ret.end()), f);
      return ret;
  }
  
  template&lt;typename T, typename Functor&gt;
  auto transform_container(const std::set&lt;T&gt; &amp;v, Functor &amp;&amp;f) -&gt; std::set&lt;decltype(f(*v.begin()))&gt;
  {
      std::set&lt;decltype(f(*v.begin()))&gt; ret;
      std::transform(std::begin(v), std::end(v), std::inserter(ret, ret.end()), f);
      return ret;
  }
  </code></pre>
  
  <p>However, when I attempted to merge them into a single general function that works with any container, I encountered numerous issues. The <code>set</code> and <code>vector</code> are class templates, so my function template must take a template template parameter. Moreover, set and vector templates have a different number of type parameters that needs to be properly adjusted. </p>
  
  <p>What is the best way to generalize the two function templates above into a function that works with any compatible container type?</p>