VECTORS IN C++
A generic container of objects of the same type that is an alternative to C++ fields. In other words, they are dynamic lists. Unlike arrays whose dimension does not change, with vectors it can change dynamically. The array declaration is:
int arr[3]={4,2,7}
Here, the dimensions (3) must be specified in square brackets and cannot be changed further.
Unlike a sheet, the dimension of a vector can be changed during the program.
Here, the dimensions (3) must be specified in square brackets and cannot be changed further.
Unlike a sheet, the dimension of a vector can be changed during the program.
Include header file: vector
In order to be able to use a vector in our program, it needs to be included using the directive #include:
#include < vector >
The following line should be added below that, which defines the use of the std namespace:
using namespace std;
Otherwise, you would have to write std::vector every time you want to call a vector
Example of vector usage:
using namespace std;
Otherwise, you would have to write std::vector every time you want to call a vector
Example of vector usage:
#include < vector >
using namespace std;
vector < int > a(10);
using namespace std;
vector < int > a(10);
This is the declaration of a vector whose data is of type int
Here is:
a[3]
Here is:
- vector-vector identification
- <int> - data type used in the vector, in this case integer
- a - vector name
a[3]
Vector methods in C++
Some of the methods for working with vectors in c++:
Adding an element to the end of a vector
Method push_back
example of adding the number 13 to the end of vector a:
example of adding the number 13 to the end of vector a:
a.push_back(13);
Vector size in c++
The size of the vector can be determined by the method. Usage example:
#include < vector >
using namespace std;
vector < int > b={10,20,30,40,50};
cout << "Number of elements of the vector is " << b.size() << "." << endl;
using namespace std;
vector < int > b={10,20,30,40,50};
cout << "Number of elements of the vector is " << b.size() << "." << endl;
Taking the first element from a vector
The first element of the vector can be obtained by the method vector::front().
#include < vector >
using namespace std;
vector < int > grоups={"Michael","Petar","Lazar","Natasa"};
cout << "The first in the group is " << grоups.front() << endl;
using namespace std;
vector < int > grоups={"Michael","Petar","Lazar","Natasa"};
cout << "The first in the group is " << grоups.front() << endl;
The output would show: Michael
Retrieving the last element in a vector
The last element can be retrieved from the vector using the method vector::back(). For example. in the previous group vector example, the last a group member would be:
#include < vector >
using namespace std;
vector < string > group={"Michael","Petar","Lazar","Natasa"};
cout << "The last one in group is" << grupa.back() << endl;
using namespace std;
vector < string > group={"Michael","Petar","Lazar","Natasa"};
cout << "The last one in group is" << grupa.back() << endl;
The output would show: Natasa
Inserting a value at a specific position in a vector
Suppose an arbitrary dynamic sequence of numbers is given:
vector < int > numbers={0, 4, 11, 6};
If we want e.g. to replace the new number 5 with the existing number 11, which is in position 2 in the sequence, we can do it as follows:
numbers[2] = 5;
or using the at function:
numbers.at(2) = 5;
The array after modification would look like:
{0, 4, 5, 6};
It can be observed that in this way the value that was previously in the inserted position is deleted. If we want to keep the previous element and insert a new one, then the insert function should be used. For example, if we wanted to now add the value 3 to position 1, then we could do it like this:
int pos = 1;
auto itr=numbers.begin() + pos;
numbers.insert(itr, 3);
auto itr=numbers.begin() + pos;
numbers.insert(itr, 3);
After that the array would look like
{0, 3, 4, 5, 6};
We can see that the value 3 has been inserted in position 1 and the value 4 which was previously in that position has moved to position 2.
A array of values can also be added to a specific position. For example. if at position 3 now, we want to insert the following array:
A array of values can also be added to a specific position. For example. if at position 3 now, we want to insert the following array:
{11, 12, 13};
they would do it like this:
int pos = 3;
auto itr=numbers.begin() + pos;
numbers.insert(itr, {11, 12, 13});
auto itr=numbers.begin() + pos;
numbers.insert(itr, {11, 12, 13});
The string after insertion would look like:
{0, 3, 4, 5, 11, 12, 13, 6};
Deleting the last element of the vector
The last element of the vector can be deleted by the method vector::pop_back
The following example shows the application of this method:
The following example shows the application of this method:
#include < vector >
using namespace std;
vector < double > temperatures={23.2 , 25.8 , 11, 15.3, 17};
temperatures.pop_back();
using namespace std;
vector < double > temperatures={23.2 , 25.8 , 11, 15.3, 17};
temperatures.pop_back();
After this command the vector values are: 23.2, 25.8, 11, 15.3
Deleting a vector element at a specific position
vector::erase(it) will delete the element of the vector pointed to by the iterator it
Example:
Example:
#include < vector >
using namespace std;
vector < int > c={2,3,4};
c.erase(c.begin() + 1);//deletes the element at position 1 in the vector
using namespace std;
vector < int > c={2,3,4};
c.erase(c.begin() + 1);//deletes the element at position 1 in the vector
After deletion, the elements of the vector are: 2, 4
Deleting all vector elements in c++
If we want to empty the vector, ie. to delete all its elements we can use the method vector::clear()
Usage example:
Usage example:
#include < vector >
using namespace std;
vector < int > c={2,3,4};
c.clear();//deletes all elements in the vector
using namespace std;
vector < int > c={2,3,4};
c.clear();//deletes all elements in the vector
After this, the vector will be empty
Printing the elements of a vector
Let the following vector be given:
#include < vector >
using namespace std;
vector < int > numbers={1,2,3,4,5};
using namespace std;
vector < int > numbers={1,2,3,4,5};
Example of printing numbers to standard output:
for(int i = 0; i < brojevi.size(); i++)
{
cout << endl;
{
cout << numbers[i] << " ";
}cout << endl;
On the output: 1 2 3 4 5
Reading text data from standard input and inserting one by one into a vector
The <string> header must also be included. Using a while loop, one word at a time is loaded and added to the vector:
#include < vector >
#include < iostream >
#include < string >
using namespace std;
vector <string> text;
string word;
while(cin >> word)
{
#include < iostream >
#include < string >
using namespace std;
vector <string> text;
string word;
while(cin >> word)
{
text.push_back(word);
}Iterating through the elements of a vector using an iterator []
In order to print the words entered into the vector in the previous example on the standard output, the following code must be executed:
cout << "Words in the text::\n" << endl;
for(int i = 0; i < text.size(); i++)
{
cout << endl;
for(int i = 0; i < text.size(); i++)
{
cout << text[i] << " ";
}cout << endl;
Iterating through the elements of a vector using an iterator
The following code demonstrates the use of an iterator to loop through the elements of a vector
#include < vector >
using namespace std;
vector < int > points={5,3,5,7,8,2,3,4};
cout << "Number of points per task:\n " << endl;
for(vector < int >::iterator it=points.begin(); it != points.end(); ++it)
{
cout << endl;
using namespace std;
vector < int > points={5,3,5,7,8,2,3,4};
cout << "Number of points per task:\n " << endl;
for(vector < int >::iterator it=points.begin(); it != points.end(); ++it)
{
cout << *it << " ";
}cout << endl;
The following functions are also implemented in the previous code:
v.begin() – returns an iterator pointing to the initial element of the container
v.end() – returns an iterator pointing "behind the last" element of the container.
v.begin() – returns an iterator pointing to the initial element of the container
v.end() – returns an iterator pointing "behind the last" element of the container.
Sorting vectors in c++ with the sort library function
The sort function is located in the <algorithm> header, which must be included at the beginning of the file.
#include < algorithm >
The parameters to be passed are pointers to the first and last element to be sorted in the array. If it is necessary to sort the entire array then the code would be as in the following example:
#include < vector >
using namespace std;
vector < int > A={4,2,7,5,9};
sort(a.begin(),a.end());
//printing
using namespace std;
vector < int > A={4,2,7,5,9};
sort(a.begin(),a.end());
//printing
Previous
|< Arrays in C/C++ |
Next
Two dimensional array-matrix >| |