W6-Vectors and Arrays
W6-Vectors and Arrays
#include <iostream>
#include <string>
#include <vector>
using namespace std;
Vectors
Regular variables vs vectors (arrays).
Declare
1. Declare a vector of 10 integer elements.
2. Declare a vector of 5 fractional elements.
3. Declare an vector of 3 string elements.
vector<int> xi(10); // size of vector is 10
vector<double> xd(5); // size of the vector is 5
vector<string> xs(3); // size is 3
Access (read/write)
1. Print second element of integer vector xi.
2. Print last element of names.
1
3. Change second element of names to “Joan”.
xi.at(1) = 12;
cout << xi.at(1) << endl; // first element is indexed at 0
cout << xi.at(0) << endl; // is the first element there? Yes,
// it should be, but it is not initialized
// print last item of names:
cout << names.at(names.size()-1) << endl;
// a nicer version for the last item:
cout << names.back() << endl;
names.at(1) = "Joan";
cout << names.at(1) << endl;
12
0
Feng
Feng
Joan
Resize
What is the difference between vector and arrays?
Vector Array
Collection of items of the same type Collection of items of the same type
Number of items can change (dynamic array) Number of itms is fixed
2
v[i] v.at(i)
Array notation Function notation
Can be used to modify the item Can be used to modify the item
No range check Throws an exception if out of range
Use with array AND vectors Use with vectors only
3
6. auto (since C++ 11): type specifier. Replaced by compiler with appro-
priate type, deduced from initializer expression. In range based for, type
deduced from the type of the vector.
7. auto is optional. Can use specific type too.
8. & means type is reference. Initializer must be another variable or element
of vector array (like in range based for).
auto x=3; // initializer must be supplied.
cout << "x is " << x << endl;
auto & refer_x = x; // refer_x and x are the same variable!
refer_x = 4;
cout << "x is " << x << endl;
x is 3
x is 4
// print vec with range based for loop
for (int element: vec) {
cout << element << ' ';
}
cout << endl;
for (auto element: vec) { // without reference (vec will not change)
element--;
}
for (int element: vec) {
cout << element << ' ';
}
1 4 6 9 6 4 98
1 4 6 9 6 4 98
// print vec with range based for loop
for (int element: vec) {
cout << element << ' ';
}
cout << endl;
for (auto & element: vec) { // with reference (vec will not change)
element--;
}
for (int element: vec) {
cout << element << ' ';
}
1 4 6 9 6 4 98
0 3 5 8 5 3 97
4
Add/remove elements at the end of a vector (stack data
structure)
1. vec.push_back(val): adds at the end
2. vec.back(): returns the last value; can we change the last value?
3. vec.pop_back(): removes the last value
//vector<int> vec={2,4,6,8};
cout << vec.back() << endl;
vec.pop_back();
for (auto e: vec) {
cout << e << " ";
}
cout << endl;
vec.push_back(10);
for (auto e: vec) {
cout << e << " ";
}
cout << endl;
vec.back() = 100; // use in assignment, like with [] notation
for (auto e: vec) {
cout << e << " ";
}
97
0 3 5 8 5 3
0 3 5 8 5 3 10
0 3 5 8 5 3 100
vec.push_back(200);
for (auto e: vec) {
cout << e << ' ';
}
cout << "Size is " << vec.size();
0 3 5 8 5 3 100 200 200 200 200 Size is 11
5
replace 7 by avg(4,7) = 5.5
// first and last elements are special cases, because they have only 2 neighbours incl thems
vector<double> img = {2.5, 6, 9, 3, 10, 33, 4, -7, 43};