Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. int sub_vector(vector<int> source, vector<int> sub) {
  7.     if (sub.size() > source.size()) return -1;
  8.     for (int i = 0; i < source.size() - sub.size() + 1; ++i) {
  9.         bool found = true;
  10.         for (int j = 0; j < sub.size(); ++j) {
  11.             if (sub[j] != source[i + j]) {
  12.                 found = false;
  13.                 break;
  14.             }
  15.         }
  16.         if (found) {
  17.             return i;
  18.         }
  19.     }
  20.     return -1;
  21. }
  22.  
  23. int main() {
  24.     vector<int> source = {1,2,3,4,5};
  25.     vector<int> sub = {1,2,3,4,5};
  26.  
  27.     cout << sub_vector(source, sub) << endl;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement