Segmentation fault using max_element() and min_element() in C++
<p>When I try to run this program there is a segmentation fault that is caused in this section:</p>

<pre><code>      std::vector&lt;double&gt; dist1(dist);

      dist1.erase(std::remove(dist1.begin(), dist1.end(), 0), dist1.end());

      max = *std::max_element(dist1.begin(),dist1.end());

      min = *std::min_element(dist1.begin(),dist1.end());
</code></pre>

<p>It is the use of the max_element() and min_element() that cause the segmentation fault but I do not see why. What I doing here is copying a vector "dist" to "dist1", removing all occurrences of "0" in the new vector and then searching for the minimum and maximum values from the remaining items in "dist" "max" and "min" are double type variables previously declared. "dist" is previously declared as</p>

<pre><code> std::vector&lt;double&gt; dist; //vector used to hold the distances between adjacent vertices

 dist.resize(size);
</code></pre>

<p>The code compiles otherwise with g++ on a Linux server. Please advice.</p>