Vectors
Topics
What Is a Vector?
- Essentially a resizable array, like an ArrayList in Java.
- Can get the size of the vector!
- Can add elements
- Can remove elements
- Ordered
- Requires
header: <vector>
Declaring
- Vectors can be declared as either
- Default size
- Initial size
- Initial size and initial value
- Initial values, determined size
vector <data type> name;
Declaring Example
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#include <vector>
using namespace std;
int main()
{
// Default size
vector <int> def;
// 10 integers, no default value
vector <int> init(10);
// 10 integers all set to value 1
vector <int> initv(10, 1);
// 3 integers set to 1, 2, and 3
vector <int> i3 = {1, 2, 3};
}
|
4 different ways to declare a vector of integers.
Adding Elements (push_back
)
1
2
3
4
5
6
7
8
|
#include <vector>
using namespace std;
int main()
{
vector <string> strings;
strings.push_back("New String"); // Resizes strings to 1
strings.push_back("Newer String"); // Resizes strings to 2
}
|
Resizing a Vector
Resizing a vector could either expand the vector or shrink the vector, depending on the size of the vector before .resize()
is called. If you want to change the vector to size n, then…
- If n is more than current size of the vector, then the vector expands, and extra elements are appended at the end of the vector.
- If n is less than the current size of the vector, then the vector shrinks, and extra elements are demolished.
Resizing (resize) with Expansion
1
2
3
4
5
6
7
8
|
#include <vector>
using namespace std;
int main()
{
vector<char> values;
values.resize(2); // Resize vector to 2 elements with default value.
values.resize(15, 'Z'); // Resize same vector to 15 elements. Adds 13 new elements with default value 'Z'. Doesn't touch first 2 values.
}
|
Resizing (resize) with Shrinking
1
2
3
4
5
6
7
|
#include <vector>
using namespace std;
int main()
{
vector <int> values = {11, 22, 33, 44, 55};
values.resize(3); // Resize vector to 3 elements. Demolishes values[3] and values[4] which hold 44 and 55.
}
|
Accessing Elements [ ]
1
2
3
4
5
6
7
8
|
#include <vector>
using namespace std;
int main()
{
vector <int> values = {11, 22, 33, 44, 55};
// Prints element 0 (integer 11) and Prints element 4 (integer 55)
cout << values[0] << ',' << values[4] << '\n';
}
|
Accessing Elements .at()
1
2
3
4
5
6
7
8
|
#include <vector>
using namespace std;
int main()
{
vector <int> values = {11, 22, 33, 44, 55};
// Prints element 0 (integer 11) and Prints element 4 (integer 55)
cout << values.at(0) << ',' << values.at(4) << '\n';
}
|
Clearing a Vector
- Reusing a vector can be done with the
.clear()
method
- Clearing a vector does two things:
- Destroys all the elements in the vector.
- Changes the size of the vector to zero.
- You can think of it as resizing the vector to zero.
Clearing (clear)
1
2
3
4
5
6
7
|
#include <vector>
using namespace std;
int main()
{
vector <int> values = {11, 22, 33, 44, 55};
values.clear(); // Resize vector to 0 elements. Demolishes all values in the vector.
}
|
Getting the # of Elements (size)
1
2
3
4
5
6
7
8
9
10
|
#include <vector>
using namespace std;
int main()
{
vector <int> values = {1, 2, 3, 4, 5};
cout << values.size() << '\n';
for (int i = 0; i < values.size(); i++) {
cout << values[i] >> '\n';
}
}
|
Copying Vectors
-
Unlike arrays, vectors can be copied using the assignment operator
1
2
3
4
|
vector <int> a = {1, 2, 3, 4, 5};
vector <int> b = a;
//OR
b = a;
|
Calculating the Min/Max
- Find the minimum and maximum values in a vector.
- Solution:
- Assume the first element is both the min and max.
- Iterate from second element to last element.
- Assume the first element is both the min and max.
- Iterate from second element to last element.
Min/Max code
1
2
3
4
5
6
7
8
9
10
11
12
|
vector <int> values;
// ... add elements to values ...
int min, max, i;
// 1. assume min and max is first element
min = max = values.at(0);
// 2. iterate from second element to last
for (i = 1; i < values.size(); i++) {
// if ith element is smaller than min
if (values.at(i) < min) { min = values.at(i); }
// if ith element is bigger than max
else if (values.at(i) > mx) { mx = values.at(i); }
}
|
Powerpoint