Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's one of my favorites, but it doesn't use <code>std::ostream_iterator</code>:</p> <pre><code>#include &lt;iterator&gt; #include &lt;string&gt; #include &lt;iosfwd&gt; template &lt;class C&gt; auto print(std::ostream&amp; os, const C&amp; c, const std::string&amp; delim = std::string(", "), const std::string&amp; open_brace = std::string("{"), const std::string&amp; close_brace = std::string("}") ) -&gt; decltype(std::begin(c), std::end(c), os) { os &lt;&lt; open_brace; auto i = std::begin(c); auto e = std::end(c); if (i != e) { os &lt;&lt; *i; for (++i; i != e; ++i) os &lt;&lt; delim &lt;&lt; *i; } os &lt;&lt; close_brace; return os; } #include &lt;list&gt; #include &lt;iostream&gt; int main() { std::list&lt;double&gt; x{1,2,3,4,5,6}; print(std::cout, x) &lt;&lt; '\n'; } {1, 2, 3, 4, 5, 6} </code></pre> <p><strong>Update</strong></p> <p>Oliver goaded me into a challenge I couldn't resist. :-)</p> <pre><code>#include &lt;iterator&gt; #include &lt;string&gt; #include &lt;iosfwd&gt; namespace my { template &lt;class C&gt; auto print(std::ostream&amp; os, const C&amp; c, const std::string&amp; delim = std::string(", "), const std::string&amp; open_brace = std::string("{"), const std::string&amp; close_brace = std::string("}") ) -&gt; decltype(std::begin(c), std::end(c), os); template &lt;class C, typename std::enable_if &lt; !std::is_same&lt;C, std::string&gt;::value, bool&gt;::type = false &gt; inline auto operator&lt;&lt; (std::ostream&amp; os, const C&amp; c) -&gt; decltype(print(os, c)) { return print(os, c); } template &lt;class C&gt; auto print(std::ostream&amp; os, const C&amp; c, const std::string&amp; delim, const std::string&amp; open_brace, const std::string&amp; close_brace ) -&gt; decltype(std::begin(c), std::end(c), os) { os &lt;&lt; open_brace; auto i = std::begin(c); auto e = std::end(c); if (i != e) { os &lt;&lt; *i; for (++i; i != e; ++i) os &lt;&lt; delim &lt;&lt; *i; } os &lt;&lt; close_brace; return os; } } #include &lt;list&gt; #include &lt;forward_list&gt; #include &lt;iostream&gt; int main() { std::forward_list&lt;std::list&lt;double&gt;&gt; x{{}, {3, 2, 1}, {1,2,3,4,5,6}}; my::print(std::cout, x) &lt;&lt; '\n'; } {{}, {3, 2, 1}, {1, 2, 3, 4, 5, 6}} </code></pre> <p>It isn't perfect, but it was fun. :-) There's probably a better way to do it that would propagate the custom delimiter and braces more faithfully.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload