Advertisement
jasonpogi1669

Find the first occurrence (index) of x in a sequence using C++

Nov 3rd, 2021
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.26 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     int n;
  7.     cin >> n;
  8.     vector<int> a(n);
  9.     for (int i = 0; i < n; i++) {
  10.         cin >> a[i];
  11.     }
  12.     int x;
  13.     cin >> x;
  14.     auto it = find(a.begin(), a.end(), x);
  15.     cout << it - a.begin() << '\n';
  16.     return 0;
  17. }
  18.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement