to find the middle item in a vector, why use "mid = beg + (end - beg) / 2" instead of " mid = (beg + end) /2"
<p>I'm new to C++. I saw this code online, where it is trying to find a string within a vector. However, I have noticed right towards the end:</p>

<pre><code>mid = beg + (end - beg) / 2;
</code></pre>

<p>Why does it have to be written in this way, why can't it be written as:</p>

<pre><code>mid = (beg + end) /2
</code></pre>

<p>Is <code>mid = (beg + (end - 1)) / 2</code> a feasible alternative?</p>

<p>I'm struggling to understand the reason behind it.</p>

<pre><code>vector&lt;string&gt; text = {"apple", "beer", "cat", "dog"};
    string sought = "beer";

    auto beg = text.begin(), end = text.end();
    auto mid = text.begin() + (end - beg) / 2;
    while (mid != end &amp;&amp; *mid != sought){
        if(sought &lt; *mid){
            end = mid;
        } else {
            beg = mid + 1;
        }
        mid = beg + (end - beg) / 2;
    }
</code></pre>