-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlect9.cpp
More file actions
73 lines (57 loc) · 1.53 KB
/
lect9.cpp
File metadata and controls
73 lines (57 loc) · 1.53 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <vector>
#include <algorithm> // for reverse()
using namespace std;
// linear search
int LinearSearch(vector<int> nums, int target){
for (int value : nums) {
if (value==target)
{
return 1 ;
}
}
return -1;
}
void reverseCode(vector<int>& nums){
reverse(nums.begin(), nums.end());
}
int main(){
// similar to an Array but dynamic in nature means size is not fixed. dynamically resized.
// STL (Standard Template Library) -
//vector functions-
// 1. size
// 2. push_back
// 3. pop_back
// 4. front
// 5. back
// 6. at
// vector<int> vec;
// vector<int> vec = {1, 2, 3};
// vector<int> vec(3, 0);
// cout <<vec[0];
// cout << vec[3];
// vector<char> alph = {'m'};
// cout << "size= " << alph.size() << "\n";
// alph.push_back('d');
// alph.push_back('k');
// // alph.pop_back();
// cout << alph.front() << "\n";
// cout << alph.at(2) << "\n";
// Static vs dynamic memory allocation
// static - at compile time / inside stack /
// dynamic - at running time / inside heap /
// vector<int> score = {};
// score.push_back(12);
// score.push_back(26);
// score.push_back(33);
// score.push_back(63);
// cout << score.size() << "\n";
// cout <<score.capacity()<<"\n";
vector<int> nums = {1, 2, 3, 45};
cout << LinearSearch(nums, 3) <<"\n";
reverseCode(nums);
for (int i : nums){
cout << i<<" ";
}
return 0;
}