-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode_704_Binary_Search.cpp
56 lines (42 loc) · 1.05 KB
/
leetcode_704_Binary_Search.cpp
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
55
56
// https://leetcode.com/problems/binary-search/
// https://www.educative.io/answers/how-to-iterate-through-a-vector-in-cpp
// https://stackoverflow.com/questions/57586221/what-is-the-difference-between-auto-and-auto
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int search(vector<int>& nums, int target) {
vector<int>::iterator iV;
int i;
int result = -1;
for(i=0, iV=nums.begin(); iV<nums.end(); iV++, i++)
{
if(*iV == target){
result = i;
break;
}
}
return result;
}
};
void iterate_print(vector<int>& rVect)
{
vector<int>::iterator iV; // iV is a pointer
for ( iV = rVect.begin(); iV<rVect.end(); iV++)
{
cout << *iV << "\n";
}
cout << "----------- \n";
for (auto& eachElem : rVect) // eachElem is a reference
{
cout << eachElem << "\n";
}
}
int main(void)
{
vector<int> mVect = {1,2,3,4,5,6};
Solution mSln;
cout << mSln.search(mVect, 3) << "\n\r";
return 0;
}