SHARE
TWEET

Untitled

a guest Dec 12th, 2019 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top