-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVectors.cpp
executable file
·189 lines (152 loc) · 4.91 KB
/
Vectors.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm> // For sort
using namespace std;
// Utility function to print a vector
template <typename T>
void printVector(T dataVecs)
{
cout << endl;
// VERY IMP
//for (auto data : dataVecs)
//for(size_t i = 0; i < dataVecs.size(); i++)
for(vector<string>::size_type i = 0; i < dataVecs.size(); i++)
{
//cout << data << endl;
cout << dataVecs[i] << endl;
}
cout << endl;
cout << endl;
}
struct MeetingTime
{
int startTime;
int endTime;
MeetingTime(double sT, double eT) : startTime(sT),
endTime(eT) { }
/*
bool operator < (const MeetingTime& meetTime) const
{
return (startTime < meetTime.startTime);
}
bool operator > (const MeetingTime& meetTime) const
{
return (areDoubleGreater(endTime, meetTime.endTime));
}
*/
bool operator < (const MeetingTime& meetTime) const
{
return (startTime < meetTime.startTime);
}
bool operator > (const MeetingTime& meetTime) const
{
return (endTime < meetTime.endTime);
}
};
vector<MeetingTime*> returnEmptyVector() {
return {};
}
// TODO:
struct MyMeetStrtEndComp
{
bool operator()( const MeetingTime& lx, const MeetingTime& rx ) const {
cout << "Comp: " << lx.startTime << "; " << rx.endTime << endl;
return lx.startTime >= rx.endTime;
}
};
void printMeetingVector(vector<MeetingTime> meetings)
{
cout << "List of Meetings: " << endl;
for (auto meeting : meetings)
{
cout << meeting.startTime << "-" << meeting.endTime << endl;
}
cout << endl;
}
int main()
{
// 1. VECTORS
// This will add 3 5's to the vector
std::vector<int> numsSimp (3, 5);
// Create a Vector from an Array
int arr[] = {1, 2, 3, 4, 5};
vector<int> nums(arr, arr + sizeof(arr) / sizeof(int));
// Tells the Version of _c++. Used to check if C++ 11 is being used.
cout << "__c++: " <<__cplusplus << endl;
// First way to iterate through a vector. Using AUTO
for (auto num : nums)
{
cout << num << " ";
}
cout <<endl;
// Second Way. Using Iterator
// We can move the iterator to a specific position as well.
for (std::vector<int>::iterator it = nums.begin() + 2; it != nums.end(); it++)
{
cout << *it << " ";
}
cout <<endl;
// Using .at() to access a specific element.
// Also we have use ARRAY representation
for (unsigned int i = 0; i < nums.size(); i++)
{
//cout << nums.at(i) << " ";
cout << nums[i] << " ";
}
cout <<endl;
// 1b. Inserting multiple elements into Vector
std::vector<int> v1{2,5,8,11,14};
std::vector<int> v2 = {2,5,8,11,14};
// 2. UNORDERED MAP
unordered_map<int, int> myHash;
myHash[5] = 0;
cout << myHash[5] << endl;
// 3. Vector of Vectors
// 3 Rows and 2 Cols
vector< vector<int> > matrix(3,vector<int>(2));
cout << "Vector of Vectors " << endl;
vector< vector<int> > vecOfVecs;
vector<int> vec2 = {5, 6};
vector<int> vec3 = {7};
vector<int> vec4 = {1, 2, 3};
/*
vecOfVecs[0] = vec2;
vecOfVecs[1] = vec3;
vecOfVecs[2] = vec4;
*/
vecOfVecs.push_back(vec2);
vecOfVecs.push_back(vec3);
vecOfVecs.push_back(vec4);
// Here is a simple explanation. vector< vector<int> > vecOfVecs(3); creates a vector with three empty vectors inside it. So you start with { {}, {}, {} }. Then you push back your three new vectors onto the end. So you end up with { {}, {}, {}, {5,6}, {7}, {1,2,3}}.
cout << vecOfVecs[0][0] << endl;
// Pass Vectors through recursion
{
vector<int> vecInts = {1, 2, 3};
cout << &vecInts << endl;
cout << &vecInts[0] << endl;
cout << &vecInts[1] << endl;
cout << &vecInts[2] << endl;
cout << (&vecInts + 1)<< endl;
}
// Struct objects sorted by two parameters
{
vector<struct MeetingTime> meetingsStrTimeSort = {{9, 10}, {14, 16}, {8, 12}, {12, 16}, {10, 12}};
vector<struct MeetingTime> meetingsEndTimeSort = meetingsStrTimeSort;
printMeetingVector(meetingsStrTimeSort);
sort(meetingsStrTimeSort.begin(), meetingsStrTimeSort.end());
printMeetingVector(meetingsStrTimeSort);
printMeetingVector(meetingsEndTimeSort);
sort(meetingsEndTimeSort.begin(), meetingsEndTimeSort.end(), greater<MeetingTime>());
printMeetingVector(meetingsEndTimeSort);
MeetingTime tmp = {11, 13};
auto itr = std::upper_bound(meetingsEndTimeSort.begin(), meetingsEndTimeSort.end(), tmp, MyMeetStrtEndComp());
cout << "Itr: " << (*itr).startTime << "-" << (*itr).endTime << endl;
}
{
cout << "Return empty vector: " << endl;
vector<MeetingTime*> a = returnEmptyVector();
cout << a.size() << endl;
}
cout << endl;
}