title: Why "Iterator from different contaniner are used together" CppCheck error appear?
body: <p>In my code I have the CppCheck error <em>&quot;Iterator 'it' from different container 'polyline' are used together&quot;</em>.</p>
  <p>Do you understand why it appears at the line with <code>.erase(it)</code> please ?</p>
  <pre><code>for (QList&lt;QPointF&gt; &amp;polyline : _polylinesPoints) {
          // Start loop with 2nd element and finish it before last element.
          for (auto it = polyline.begin() + 1; it &lt; polyline.end() - 1;) {
              if (*it != newP1 &amp;&amp; *it != newP2 &amp;&amp; DesignUtils::areCollinear(*(it - 1), *it, *(it + 1)))
                  it = polyline.erase(it); --&gt; message here
              else
                  ++it;
          }
      }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/PuoVA.png" rel="nofollow noreferrer">my code</a></p>
  <p>I have a solution with an iterator based for loop, but I would like to understand why this error appears while my code seems to work when I test it.</p>
  <p>My solution is:</p>
  <pre><code>for (auto itPolyline = _polylinesPoints.begin(); itPolyline != _polylinesPoints.end(); ++itPolyline) {
          // Start loop with 2nd element and finish it before last element.
          for (auto it = itPolyline-&gt;begin() + 1; it &lt; itPolyline-&gt;end() - 1;) {
              if (*it != newP1 &amp;&amp; *it != newP2 &amp;&amp; DesignUtils::areCollinear(*(it - 1), *it, *(it + 1)))
                  it = itPolyline-&gt;erase(it);
              else
                  ++it;
          }
      }
  </code></pre>
  <p>Thanks for your help.</p>
  <p>Baptiste</p>