-
Notifications
You must be signed in to change notification settings - Fork 0
/
vectors_lesson12.cpp
197 lines (136 loc) · 5.24 KB
/
vectors_lesson12.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
190
191
/*
We have been using the C++ standard function library throughout this course (recall I/O streams, string, etc.).
Well, there is actually another part of the Standard C++ library, the object oriented class.
The Object oriented library has many containers that are quite useful for programming.
We will create a vector of integers, called vectorInts.
You should notice the following:
We included a C++ library called vector
To instantiate a vector :
std::vector<int> vectorInts;
The vector has size 0 when we instantiate it.
We use 'resize' to change the size of the vector. */
// constructing vectors
#include <iostream>
#include <vector> //Need to include the vector library!
int main ()
{
//creating a vector of integers
std::vector<int> vectorInts;
std::cout<<"vectorInts has "<<vectorInts.size()<<" elements\n";
//Changing the size of vectorInts to 6
vectorInts.resize(6);
std::cout<<"\n\nvectorInts now has "<<vectorInts.size()<<" elements\n";
return 0;
}
//output: vectorInts has 0 elements ; vectorInts now has 6 elements.
//Note-2
//How to use assign and iterator?
/*Vectors are more versatile than arrays, and can be resized during runtime.
We can also insert elements into a vector.
This characteristic adds a lot of freedom to vectors that we don't have with arrays.
It also means we have to adjust how we access elements in a vector.
Since we can add elements anywhere in the vector,
we do not refer to the first element of a vector as the zero element, we call it the beginning.
The last element is called end. To keep track of where we are in the vector, we need an iterator. */
// Goal: pracitce constructing vectors
// and their iterators
// Call the vector vFloat
// Call the iterator it
//TODO: Add the necessary libraries
#include "main.hpp"
int main ()
{
//TODO: create a vector of floats
vector<float> vFloat;
std::cout<<"vFloat has "<<vFloat.size()<<" elements\n";
//TODO: add elements to the library
std::cout<<"\n\nAdding 10 elements to the vector\n";
//TODO: assign the value 8.8 to 10 elements of the vector
vFloat.assign(10, 8.8);
std::cout<<"vFloat has "<<vFloat.size()<<" elements\n";
//TODO: Complete the Print function in main.hpp
// Call the function here to print out each element of vFloat
printVector(vFloat);
return 0;
}
//A sample solution of main.hpp
// constructing vectors
// constructing vectors
#include <iostream>
#include <vector>
using namespace std;
void printVector(vector<float> vIn);
void printVector(vector<float> vIn)
{//printing the contents of vIns
//TODO: Complete the function
vector<float>::iterator it;
for (it = vIn.begin(); it != vIn.end(); ++it)
cout<<*it<<" ";
}
//But be careful!: ::Assign writes over the elements in the vector.
//Note-3:
//We can add elements to the end (or back) of a vector using ::push_back.
//for ex:
vectorInts.push_back(24);
vectorInts.push_back(25);
vectorInts.push_back(26);
//Note-4
/*
insert is another method to add elements to a vector.
insert adds elements to the location after the iterator.
To add elements using insert:
Set the iterator to one before the location where you would like to add an element.
Use insert to add the element.
Before we insert, we need to set the iterator to a known destination.
In this case we set it to the beginning of the vector plus one.
it = vectorInts.begin() + 1;
To insert the element we need to give the insert location and the value of the element.
In this case the location is the location of the iterator (begin + 1). The value is -1.
vectorInts.insert(it, -1);
Also note in the program below, I reset the iterator to the beginning of the vector before inserting the next value.
You need to do this after an insert because all iterator locations are invalid after an insert.*/
//Note-5:
/* emplace.
Emplace puts an element in a vector at the position pointed to by the iterator.
Insert copies the values of the vector while emplace does an in-place insertion.
*/
//Sample example
// constructing vectors
// constructing vectors
#include <iostream>
#include <vector>
using namespace std;
void printVector(vector<int> vIn);
void assignFunction(vector<int> vInts, int in);
void pushBackFunction(vector<int> vInts, int in);
void emplaceFunction(vector<int> vInts, int loc, int in);
void printVector(vector<int> vIn)
{//printing the contents of vIns
vector<int>::iterator it;
for (it = vIn.begin(); it != vIn.end(); ++it)
std::cout<<*it<<" ";
}
void assignFunction(vector<int> vInts, int in)
{
cout<<"\nassigning "<<in<<" and printing the vector\n";
vInts.assign(1, in);
printVector(vInts);
}
void pushBackFunction(vector<int> vInts, int in)
{
cout<<"\npush back "<<in<<" and printing the vector\n";
vInts.push_back(in);
printVector(vInts);
}
void emplaceFunction(vector<int> vInts, int loc, int in)
{
vector<int>::iterator it;
cout<<"\nemplacing "<<in<<" and printing the vector\n";
it = vInts.begin() + loc;
vInts.emplace(it, in);
printVector(vInts);
}
//Note-6
//We can remove all elements from the vector at the same time using the method ::clear.
//We can remove specific elements from a vector using ::erase.
//We can remove the last element of a vector using ::pop_back.