Determine whether one vector is a prefix of the other Exercise 5.17:
  
  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, and 2 and 0, 1, 1, 2,
  3, 5, 8, respectively your program should return true.


This executes correctly, but I just want to get this double-checked.

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

using std::vector; using std::cout; using std::endl;

int main()
{
vector&lt;int&gt; v1{ 0, 1, 1, 2, 3, 5, 8 };
vector&lt;int&gt; v2{ 0, 1, 1, 2 };
if (v1.size() &lt; v2.size()){
    cout &lt;&lt; "Using v1 as the prefix to v2" &lt;&lt; endl;
    for (int smallIndex = 0, largeIndex = 1; smallIndex != v1.size(); ++smallIndex, ++largeIndex){
        if (v1[smallIndex] == v2[largeIndex])
            cout &lt;&lt; "v1 at position " &lt;&lt; smallIndex &lt;&lt; " is a prefix for v2 at postion " &lt;&lt; largeIndex &lt;&lt; endl;
    }
}
else if (v1.size() &gt; v2.size()){
    cout &lt;&lt; "Using v2 as the prefix to v1" &lt;&lt; endl;
    for (int smallIndex = 0, largeIndex = 1; smallIndex != v2.size(); ++smallIndex, ++largeIndex){
        if (v2[smallIndex] == v1[largeIndex])
            cout &lt;&lt; "v2 at position " &lt;&lt; smallIndex &lt;&lt; " is a prefix for v1 at position " &lt;&lt; largeIndex &lt;&lt; endl;
    }
}
else {      //  v1.size() == v2.size() only comparing the first as the prefix to the second
    cout &lt;&lt; "Size of v1 and v2 are the same, using v1 as the prefix to v2" &lt;&lt; endl;
    for (int smallIndex = 0, largeIndex = 1; largeIndex != v1.size(); ++smallIndex, ++largeIndex){
        if (v1[smallIndex] == v2[largeIndex])
                cout &lt;&lt; "v1 at position " &lt;&lt; smallIndex &lt;&lt; " is a prefix for v2 at postion " &lt;&lt; largeIndex &lt;&lt; endl;
        }
    }
}
For a prefix search, you can use std::mismatch() in &lt;algorithm&gt;.

bool isPrefix(const std::vector&lt;int&gt;&amp; v1, const std::vector&lt;int&gt;&amp; v2) {
    if (v1.size() &gt; v2.size()) {
        return v2.end() == std::mismatch(v2.begin(), v2.end(), v1.begin()).first;
    }
    return v1.end() == std::mismatch(v1.begin(), v1.end(), v2.begin()).first;
}