Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Beyond <a href="https://stackoverflow.com/questions/3757899/sorting-strings-using-qsort/3757917#3757917">why qsort fails</a>, don't use it in C++.</p> <pre><code>#include &lt;algorithm&gt; #include &lt;iostream&gt; #include &lt;iterator&gt; #include &lt;string&gt; char const* const raw_data[5] = {"dat", "mai", "lik", "mar", "ana"}; std::vector&lt;std::string&gt; data (raw_data, raw_data + 5); // would rarely be a global // see below for code that needs to go here int main() { using namespace std; cout &lt;&lt; "before: " &lt;&lt; data &lt;&lt; "\n"; sort(data.begin(), data.end()); cout &lt;&lt; "after: " &lt;&lt; data &lt;&lt; "\n"; return 0; } </code></pre> <p>Boost has stream inserter overloads to output a vector directly, but here's one <a href="http://bitbucket.org/kniht/scraps/src/tip/cpp/kniht/streamutil.hpp" rel="nofollow noreferrer">simple version</a>. This goes into a header, rather than being copied and pasted continually:</p> <pre><code>template&lt;class Stream, class Iter, class Ch&gt; void write_sequence(Stream&amp; s, Iter begin, Iter end, Ch const* initial, Ch const* sep, Ch const* final) { if (initial) { s &lt;&lt; initial; } if (begin != end) { s &lt;&lt; *begin; ++begin; for (; begin != end; ++begin) { if (sep) { s &lt;&lt; sep; } s &lt;&lt; *begin; } } if (final) { s &lt;&lt; final; } } template&lt;class T, class A&gt; std::ostream&amp; operator&lt;&lt;(std::ostream&amp; s, std::vector&lt;T,A&gt; const&amp; value) { write_sequence(s, value.begin(), value.end(), "[", ", ", "]"); return s; } </code></pre>
 

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