0% found this document useful (0 votes)
24 views6 pages

W6-Vectors and Arrays

Learn C++ 5

Uploaded by

Ro Ben
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views6 pages

W6-Vectors and Arrays

Learn C++ 5

Uploaded by

Ro Ben
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

// SETUP CELL

#include <iostream>
#include <string>
#include <vector>
using namespace std;

Vectors
Regular variables vs vectors (arrays).

Regular variables Vectors (arrays)


Store one value Store many values of the same type
(a collection)
Declaration: int x, float x, ... vector<int> x(size),
vector<float> x(size), ...
Declare and initialize: float x=2.3; vector<float> x = {1.0, 2.1,
3.2};
Access: x Access x[index] or x.at(index)
Nothing needed to use To use, must #include <vector>

size: number of elements.


index: order of element in the collection. First element at 0.

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

Declare and initialize


1. Declare and initialize a vector of 4 double values
2. Declare and initialize a vector of 3 strings to “Anna”, “Jon”, and “Feng”
vector<string> names={"Anna", "Jon", "Feng"}; // notice there is no size explicitly given;
// the vector is automatically assigned the size of the initializer list {...}

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

Change the size of array xi to 4.


// original size 10
cout << xi.size() << endl;
xi.resize(4);
cout << xi.size();
10
4
// what happened to value 12 at index 1?
cout << xi.at(1);
12

Access all elements in a vector


1. Use a counter controlled loop (for loop is best choice)
2. Access element using the counter variable in the loop body.
3. Element access: vector[index] or vector.at(index)

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

Example Declare and initialize vector of integers. Output vector elements.


Decrease elements by 1.
vector<int> vec={2,5,7,10,7,5,99};
for (int i=0; i<vec.size(); i++) {
cout << vec.at(i) << ' ';
}
cout << endl;

// decrease all elements by 1


for (int i=0; i<vec.size(); i++) {
vec.at(i)--;
}

for (int i=0; i < vec.size(); i++) {


cout << vec.at(i) << ' ';
}
2 5 7 10 7 5 99
1 4 6 9 6 4 98

Access all elements in a vector (II) (since C++11)


1. Range based for loop
2. Syntax (read only):
for (auto e: vec) {
statements
}
3. Syntax (modify elements):
for (auto & e: vec) {
statements
}
4. May be used for simple access or element modification, eg: output elements.
Use counter controlled for loop for more control, ex: when the index is
needed.
5. Advantage of range based for: more compact notation (similar to python).

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

Complex access patterns (when index is needed)


Example: Convolution filter (smoothing an image in image analysis). Given a
vector of doubles (original sharp image), change each value to the average of its
neighboring values.
|- index i; so left neighbour is indexed by (i-1), right neighbour by (i+1)
v
img = {2 4 7}
replace 2 by avg(2,4) = 3
replace 4 by avg(2,4,7) = 4.33

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};

for (int i=0; i<img.size(); i++) {


// to be continued....
}

You might also like