Vectors and genetic datatypes.

Vectors and generic datatypes.

To use vectors, you need to include the header <vector>:

#include <vector>

Constructors and methods of the class vector<T> you need to know

Constructors:

Methods (member functions):

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 

Generic functions

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.

Function 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

Function find_max searching for the largest element in a vector

Non-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