Looking for feedback

Hey everyone. New to the forums and new to programming/c++ in general. I'm going through C++ Primer 5th edition and wanted to get some input on how my code turned out for one of the problems that had me thinking for a bit.

The problem from the book is exercise 5.17 and it asks:
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.

And here is my code for the problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <vector>

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


int main()
{
    vector<int> one = {0, 1, 1, 2};
    vector<int> two = {0, 1, 1, 2, 3, 5, 8};

    if (one == two)
    {
        cout << "The vectors are equal." << endl;
    }
    else
    {
        for (auto one_vec = one.begin(), two_vec = two.begin();
             one_vec != one.end() && two_vec != two.end(); ++one_vec, ++two_vec)
             {
                 if (one < two && *one_vec == *two_vec
                     && (one_vec == one.end() - 1 || two_vec == two.end() - 1))
                 {
                     cout << "Vector one is a prefix of vector two" << endl;
                     one_vec = one.end() - 1;
                     two_vec = two.end() - 1;
                 }
                 else
                 {
                     if (one > two && *one_vec == *two_vec
                         && (one_vec == one.end() - 1 || two_vec == two.end() - 1))
                     {
                         cout << "Vector two is a prefix of vector one" << endl;
                         one_vec = one.end() - 1;
                         two_vec = two.end() - 1;
                     }
                     else
                     {
                         if (*one_vec != *two_vec)
                         {
                             cout << "Neither vector is a prefix" << endl;
                             one_vec = one.end() - 1;
                             two_vec = two.end() - 1;
                         }
                     }
                 }
             }

    }

    return 0;
}  


I'm mainly looking for feedback on if I went about it the right way, and if you see any bad habits developing that I should fix. Your input will be greatly appreciated.
All those iterators, particularly the end()-1, scare me. There is nothing wrong in using iterators, but are they the best tool for this job?
What if you had a const size_t len = std::min(one.size(), two.size());?

You can break out from the loop. Way better than advancing iterators to back().

The way I see it, there are two possibilities:
1. Mismatch
2. Identity
For the latter, there are three sub-cases: identical and shorter being a prefix.
Last edited on
Topic archived. No new replies allowed.