Note that there are some explanatory texts on larger screens.

plurals
  1. PORemoving '#include <algorithm>' doesn't break the code
    primarykey
    data
    text
    <p>Maybe this is a very silly question, but the book I'm reading instructed me to write a piece of code that uses algorithms to scramble and order the elements in a vector. To do this the book tells me to use the algorithms library from the main C++ library. Alright, so far I understand it, but after writing the code I wanted to see what would break if I would remove this library from the top-part of my code, and it surprised me that everything still worked.</p> <p>This is the code I'm talking about. When I remove '#include algorithm' from the top-part of the code, nothing breaks. How can this be? Isn't the 'random_shuffle' part supposed to break when not using this library?</p> <pre><code>#include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;algorithm&gt; #include &lt;ctime&gt; #include &lt;cstdlib&gt; using namespace std; int main() { vector&lt;int&gt;::const_iterator iter; cout &lt;&lt; "Creating a list of scores."; vector&lt;int&gt; scores; scores.push_back(1500); scores.push_back(3500); scores.push_back(7500); cout &lt;&lt; "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout &lt;&lt; *iter &lt;&lt; endl; } cout &lt;&lt; "\nFinding a score."; int score; cout &lt;&lt; "\nEnter a score to find: "; cin &gt;&gt; score; iter = find(scores.begin(), scores.end(), score); if (iter != scores.end()) { cout &lt;&lt; "Score found.\n"; } else { cout &lt;&lt; "Score not found.\n"; } cout &lt;&lt; "\nRandomizing scores."; srand(static_cast&lt;unsigned int&gt;(time(0))); random_shuffle(scores.begin(), scores.end()); cout &lt;&lt; "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout &lt;&lt; *iter &lt;&lt; endl; } cout &lt;&lt; "\nSorting scores."; sort(scores.begin(), scores.end()); cout &lt;&lt; "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout &lt;&lt; *iter &lt;&lt; endl; } system("pause"); return 0; } </code></pre>
    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.
 

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