Given two vectors of ints, determine whether one vector is a prefix of the other Given two vectors of ints, write a program to determine whether one vector is a prefix of the other.

For vectors of unequal length, compare the number of elements of the smaller vector.
For example, given the vectors containing 0, 1, 1, 2, and 0, 1, 1, 2, 3, 5, 8, respectively, your program should return true.


#include &lt;vector&gt;
#include &lt;iostream&gt;

int main()
{
    const std::vector&lt;int&gt; ivec1 = { 0, 1, 1, 2 };
    const std::vector&lt;int&gt; ivec2 = { 0, 1, 1, 2, 3, 5, 8 };

    const decltype(ivec1.size()) size = ivec1.size() &lt; ivec2.size() ? ivec1.size() : ivec2.size();
    const std::vector&lt;int&gt;&amp; smallVec = ivec1.size() &lt; ivec2.size() ? ivec1 : ivec2;
    const std::vector&lt;int&gt;&amp; bigVec = smallVec == ivec1 ? ivec2 : ivec1;

    bool bPrefix = true;
    for(decltype(ivec1.size()) i = 0; i &lt; size; ++i)
    {
        if(smallVec[i] != bigVec[i])
        {
            bPrefix = false;
            break;
        }
    }

    std::cout &lt;&lt; &quot;small vector:\n&quot;;
    for (const auto i : smallVec)
        std::cout &lt;&lt; i &lt;&lt; &quot;, &quot;;
    std::cout &lt;&lt; &quot;\nIs prefix of big vector:\n&quot;;
    for (const auto i : bigVec)
        std::cout &lt;&lt; i &lt;&lt; &quot;, &quot;;
    std::cout &lt;&lt; &quot;\n&quot; &lt;&lt; (bPrefix ? &quot;true&quot; : &quot;false&quot;) &lt;&lt; std::endl;
}
Embrace auto. Not only is it shorter, but avoids embarrassing bugs in choosing the wrong type.
decltype() is generally much more cumbersome.

Embrace abstraction. Solving the task itself deserves its own re-usable function. Good naming even reduces the need for comments, which can get out-of-sync.

Embrace the standard library. It is well-tested and well-known. Reinventing wheels for no reason is a poor use of time.

You refrained from using std::endl until your last line. No need to use it at all, ending the program flushed the output too.

std::boolalpha is the tool of choice for streaming a bool as &quot;true&quot; or &quot;false&quot;.

Combine std::copy() and std::ostream_iterator for streaming an iterator-range.


#include &lt;vector&gt;
#include &lt;iostream&gt;
#include &lt;algorithm&gt;

template &lt;class T, class U&gt;
constexpr bool is_shorter_prefix(const T&amp; a, const U&amp; b) noexcept {
    const auto n = std::min(std::size(a), std::size(b));
    return std::equal(std::begin(a), std::begin(a) + n, std::begin(b));
}

template &lt;class T&gt;
void output_range(std::ostream&amp; os, const T&amp; t) {
    using U = decltype(*std::begin(t));
    std::copy(std::begin(t), std::end(t), std::ostream_iterator&lt;U&gt;(os, &quot;, &quot;));
}

int main() {
    const std::vector&lt;int&gt; a = { 0, 1, 1, 2 };
    const std::vector&lt;int&gt; b = { 0, 1, 1, 2, 3, 5, 8 };

    const auto is_prefix = is_shorter_prefix(a, b);

    std::cout &lt;&lt; &quot;small vector:\n&quot;;
    output_range(std::cout, a.size() &lt;= b.size() ? a : b);
    std::cout &lt;&lt; &quot;\nIs prefix of big vector:\n&quot;;
    output_range(std::cout, a.size() &gt; b.size() ? a : b);
    std::cout &lt;&lt; &quot;\n&quot; &lt;&lt; std::boolalpha &lt;&lt; is_prefix &lt;&lt; &quot;\n&quot;;
}