Menu
Coddy logo textTech

Vector

Lesson 5 of 23 in Coddy's C++ - Standard Template Library course.

Vector in C++ STL is basically a dynamically-sized array. It has the ability to resize itself automatically when an element is inserted or removed. Vector elements are stored in a contiguous storage, thus they can be accessed, modified and traversed using iterators.


In vectors, the data is inserted from the back. They are a slower data structure than an array, but the vector is fast enough to be used in problem solving even in competitive competition level of complexity. 


Vectors in C++ are implemented by including them in the top using #include <vector>

!Notice: In C++ there is a non-standard header file called bits/stdc++.h. It can be included in the top of a C++ file and you can use any of the standard library elements without the need to include them. (#include <bits/stdc++.h)

#include <iostream>
#include <vector>

using namespace std;

int main()
{
	// Code...
	
	return 0;
}
#include <bits/stdc++.h>

using namespace std;

int main()
{
	// Code...
	
	return 0;
}

So, how do we create, initialize and use a vector? We use the keyword vector<>. Inside the <> we have the variable type, the data type of the elements that we are going to store inside the vector. If we want to declare a vector that is going to store integers we use vector<int>.

#include <bits/stdc++.h>

using namespace std;

int main()
{
	vector<int> numbers = {1, 2, 3};
	
	cout << numbers[0];
	
	return 0;
}
Output:
1

As we can see from above, vectors behave exactly like arrays. We declared a vector of type int and the name numbers and we initialized it with some values. Then, using square brackets - [], we outputted the first element - the element at index 0.


We insert elements to the back of the vector using the function push_back().  The push_back() method adds a new element after the last element in a vector.

#include <bits/stdc++.h>

using namespace std;

int main()
{
	vector<int> numbers = {1, 2, 3};
	
	cout << numbers[2] << endl;
	
	numbers.push_back(10);
	
	cout << numbers[3];
	
	return 0;
}
Output:
3
10

As you see, we use the method with the vector name and a dot - .. After the use of the push_back() method the vector size is increased by one.

We also have the size() method that we can use to get the number of elements a vector has. 

#include <bits/stdc++.h>

using namespace std;

int main()
{
	vector<int> numbers = {1, 2, 3};
	
	cout << numbers.size();
	
	return 0;
}
Output:
3

We often use the size() method when we don't know the number of elements a vector has but we want to loop through it, so in the loop condition we use vector.size().


The vector data structure has many functionalities and features, like the function resize(n) resizes the vector size to n and many more. But we are going to get back to them when we learn about algorithms and functions, for now remember that vectors are dynamically-sized arrays, how we declare them and that we insert new elements using the push_back() method.


Example: Below is an example program where the user is going to keep inputting number until they've entered -1. We are going to use a loop and a vector to store them because we don't know how many elements are there going to be.

#include <bits/stdc++.h>

using namespace std;

int main()
{
	int x;
	vector<int> numbers;
	
	while(x != -1)
	{
		cin >> x;
		numbers.push_back(x);
	}
	
	return 0;
}

In every lesson you'll be given a table of the methods available to use with the specific C++ data structure.

Vector Methods

MethodFunctionality
push_back()Adds a new element at the end of the vector
pop_back()Removes the last element from the vector
swap()Swaps the specified elements
front()Gives a reference to the first element
back()Gives a reference to the last element
empty()Determines whether the vector is empty or not
clear()Removes all elements from the vector
resize()Modifies the size of the vector
size()Returns the number of elements in the vector
insert()Inserts a specified element at the specified position
challenge icon

Challenge

Easy

Positive numbers are entered from input while a -1 isn't entered. After that, the last given natural number N is your context number. Output every element that N can be divided by it with no remainder

For example:

 

Input
5
10
15
-1
100
Output
5
10

because 100 is divided by 5 and 10 but not with 15

 

Input
2
4
6
8
-1
12
Output
2
4
6

Because 12 is divided by 2, 4, 6 but not with 8

Try it yourself

#include <vector>
#include <iostream>

using namespace std;

int main()
{
    // Enter your code here

    return 0;
}

All lessons in C++ - Standard Template Library