0% found this document useful (0 votes)
166 views

Introduction To Vectors in C++ Word PDF

This document provides an introduction to vectors in C++. It discusses that vectors are similar to dynamic arrays but can automatically resize. The document is divided into three parts: (1) declaring and initializing vectors, which shows how to initialize vectors with single values or multiple elements; (2) useful vector functions like push_back(), pop_back(), size(), and insert(); (3) iterators, which are used to traverse vector elements similarly to indexes and reduce complexity, using functions like begin() and end().

Uploaded by

Karima Baba
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
166 views

Introduction To Vectors in C++ Word PDF

This document provides an introduction to vectors in C++. It discusses that vectors are similar to dynamic arrays but can automatically resize. The document is divided into three parts: (1) declaring and initializing vectors, which shows how to initialize vectors with single values or multiple elements; (2) useful vector functions like push_back(), pop_back(), size(), and insert(); (3) iterators, which are used to traverse vector elements similarly to indexes and reduce complexity, using functions like begin() and end().

Uploaded by

Karima Baba
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

American University of Beirut

Fall 2020-2021
EECE 330
Data Structures and Algorithms

Introduction to Vectors in C++

In this tutorial, we will get familiar with vectors in C++ STL.

Guidelines
 This tutorial consists of three parts.
 After you finish each part, solve the problems associated with it.

About vectors
A vector is in many ways similar to dynamic arrays in C++.
 Unlike arrays, it has the ability to resize itself automatically if elements are added or deleted
to the vector.
 They can also be accessed/traversed with iterators.
 Vector elements are placed in contiguous storage. They are as efficient as arrays but consume
more memory due to their storage management and dynamic growth.

Part I – Vector Declaration, Initialization, and Useful Functions


Whenever working with vectors, always include at the beginning:
#include <vector>
using namespace std;

To declare a vector and initialize it with a number of elements equal to an initial value:
// vector <type> name (number of elements, initial value);
vector <int> v1(5, 18);

Table 1 below shows how vector v1 is initialized in memory.


Table 1 - Memory representation of v1

index: 0 1 2 3 4
v1[index]: 18 18 18 18 18

We can also initialize the vector with multiple elements as follows.


vector <int> v2 = {3, 5, 6, 0, 202};

Table 2 below shows how vector v2 is initialized in memory.

Table 2 - Memory representation of v2

index: 0 1 2 3 4
v2[index]: 3 5 6 0 202

The table below captures many useful vector functions.


Instruction Description
v1[i] Accesses the element at index i
Adds an element at the end of the vector
v1.push_back(element);

v1.pop_back(); Removes one element from the end of the


vector
v1.size(); Returns the length of the vector

v1.insert(position, value); Inserts element at given position

Part III - Iterators


An iterator is used to move through the elements of an STL container (vector, list, set, map, ...) in a
similar way to array indexes or pointers.
Iterators point at the memory addresses of the elements of the vector. They reduce the complexity
and execution time of the program.
To declare an iterator on a vector:
#include <iterator>
...
vector <type> :: iterator itr;

To get an iterator pointing to the first element of a vector, we use:


vec.begin();

To get an iterator pointing to the (theoretical) element after the last element of a vector, we use:
vec.end();

The functions above are typically used in a for/while loop as in the example below which prints all
the elements in the vector.

vector<int> g1;
...
for (vector<int>::iterator it = g1.begin(); it != g1.end(); ++it)
cout << *it << " ";

Alternatively, we can use the placeholder auto to automatically deduce the type of the variable.
for (auto it = g1.begin(); it != g1.end(); ++it)
cout << *it << " ";

Another common usage for iterators is STL sort.


#include <algorithm>
sort(vec.begin(), vec.end());

You might also like