To use vectors, you need to include the header <vector>
:
#include <vector>
vector<T>
you need to knowConstructors:
vector<T> v;
default constructor, makes an empty vector.vector<T> v(n);
makes a vector of size n
. All elements are initialized using the default
constructor for the type T
. The primitive types (for example int
, double
, or bool
),
don’t have a constructor and they are initialized with 0
.vector<T> v(n, value);
makes a vector of size n
with all elements equal to value
.Methods (member functions):
v.size()
v.push_back(x)
v.pop_back()
v.resize(n)
change the size of the vector, with all newly created elements initialize using
their default constructor,v.resize(n, value)
change the size of the vector, all newly created elements are initialized with
the value value
.v.reserve(n)
v.capacity()
Usage example:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<double> v; // empty vector (size = 0)
vector<char> v2(10); // size = 10
vector< vector<int> > v3 (256);
v.push_back(15.5);
v.push_back(16.5);
v.push_back(19.5);
v.push_back(12.1);
cout << "v.size = " << v.size() << endl;
cout << "v2.size = " << v2.size() << endl;
for (unsigned int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << endl;
}
v.size = 4
v2.size = 10
15.5 16.5 19.5 12.1
The knowledge of how to use templates will not be tested in the exams, but you should be aware of it, since it’s a very useful thing.
Templates let you define generic functions that can be called with the arguments of different types.
For example, see the function swap
below.
swap
Non-generic implementation:
#include <iostream>
using namespace std;
void swp(int &a, int &b) {
int t = a;
a = b;
b = t;
}
int main() {
int x = 2, y = 3;
cout << "x = " << x << ", y = " << y << endl;
swp(x,y);
cout << "x = " << x << ", y = " << y << endl;
}
x = 2, y = 3
x = 3, y = 2
Generic function (inplemented using C++ templates):
#include <iostream>
using namespace std;
template <class T>
void swp(T &a, T &b) {
T t = a;
a = b;
b = t;
}
int main() {
int x = 2, y = 3;
cout << "x = " << x << ", y = " << y << endl;
swp(x,y);
cout << "x = " << x << ", y = " << y << endl;
double a = 3.1415;
double b = 1.27;
cout << "a = " << a << ", b = " << b << endl;
swp(a,b);
cout << "a = " << a << ", b = " << b << endl;
}
x = 2, y = 3
x = 3, y = 2
a = 3.1415, b = 1.27
a = 1.27, b = 3.1415
find_max
searching for the largest element in a vectorNon-generic implementation defined for the vector of doubles:
#include <iostream>
#include <vector>
using namespace std;
double find_max(vector<double> &v) {
double largest = v[0];
for(int i = 0; i < v.size(); i++){
if (v[i] > largest) largest = v[i];
}
return largest;
}
int main() {
vector<double> v;
v.push_back(3.14159265);
v.push_back(13.0);
v.push_back(23.1);
v.push_back(-1.45);
cout << "max = " << find_max(v) << endl;
}
max = 23.1
Generic implementation:
#include <iostream>
#include <vector>
using namespace std;
template <class T> T find_max(vector<T> &v) {
T largest = v[0];
for(unsigned int i = 0; i < v.size(); i++){
if (v[i] > largest) largest = v[i];
}
return largest;
}
int main() {
vector<double> v;
v.push_back(3.14159265);
v.push_back(13.0);
v.push_back(23.1);
v.push_back(-1.45);
cout << "max = " << find_max(v) << endl;
vector<char> v2;
v2.push_back('A');
v2.push_back('Z');
v2.push_back('B');
v2.push_back('*');
cout << "max = " << find_max(v2) << endl;
}
max = 23.1
max = Z