Introduction

Vectors are one of the most important data structures in C++. They are used in many algorithms and data structures. The article vectors in C++ easy programming explains the declaration of vectors in C++, access to them, and how to modify their elements.

Vectors in c++

What are vectors in C++?

Vectors are dynamic and resizable arrays. They store several elements that can be accessed via indexing. Vectors are stored in contiguous memory.

Vectors in C++ are standard template libraries that grow at a time when the program is executing. The vector in C++ permits random and quick access to the vector’s elements.The vector in C++ has the ability to use so many functions.

Vectors are kept in contiguous memory locations for uncomplicated traversal with the assistance of iteration.Vectors in C++ provide bounds checking.

Take note:        Be aware that Vectors in C++ is not the same as Vectors in mathematics.

The header vector program in c++

The vector program in c++beguin with the following structure:

#include<iostream>#include<vector>

Standard Template Library

Vectors are part of the C++ Standard Template Library. To use vectors, we need to include the vector header file in our program.

C++ program main image

An idea about C++ Template

C++ templates are a powerful tool for managing complex data structures. Templates are used to implement generic programming, which allows the same code to work with different types of data.

Templates are not really about data storage but about how different pieces of data interact with each other. Templates provide an abstraction layer between the programmer and the underlying implementation details.

Declaring a vector in c++

A vector is an object that stores a sequence of items. The items can be numbers, characters, or strings.

A vector is often used to represent a list of things and to provide operations on those lists.The vectors are objects; therefore, the programmer must include the https://www.geeksforgeeks.org/initialize-a-vector-in-cpp-different-ways/.

Declaring a vector in c++ is done using the following syntax:

#include<iostream>#include<vector>using namespace std;

vector<char> consonants (7);    

Take note: Be aware that there are many ways to declare vectors in C++.

Initialization of a vector in C++

Initialization

The initialization of a vector in C++ can be done by using the following syntax:vector<char> consonants {‘b’, ‘c’, ‘f’, ‘k’, ‘l’, ‘m’, ‘t’, };

The initialization of a vector in C++

The code of the program

#include<iostream>#include<vector>using namespace std;int main(){ vector<char> consonants {‘b’, ‘c’, ‘f’, ‘k’, ‘l’, ‘m’, ‘t’, };}

Accessing vector elements in C++

Accessing with the first method

To access vector elements in the vector C++ program, it is common to use the following syntax:

access vector elements in the vector C++ program

vector name[element index]

The code

#include<iostream>#include<vector>using namespace std;int main(){ cout<<endl;vector<char> consonants {‘b’, ‘c’, ‘f’, ‘k’, ‘l’, ‘m’, ‘t’, };cout<<” The first consonant at index o is “<<consonants[0]<<endl;cout<<” The second consonant at index 1 is “<<consonants[1]<<endl;cout<<” The third consonant at index 2 is “<<consonants[2]<<endl;cout<<” The fourth consonant at index 3 is “<<consonants[3]<<endl;cout<<” The fifth consonant at index 4 is “<<consonants[4]<<endl;cout<<” The sixth consonant at index 5 is “<<consonants[5]<<endl;cout<<” The seventh consonant at index 6 is “<<consonants[6]<<endl;}

Accessing with the first method console display

Accessing with the second method

Accessing c++ vector  with the second method

The code

C++ vector has method called at.#include<iostream>#include<vector>using namespace std;int main(){ cout<<endl;vector<char> consonants {‘b’, ‘c’, ‘f’, ‘k’, ‘l’, ‘m’, ‘t’, };cout<<” The first consonant at index o is “<<consonants.at(0)<<endl;cout<<” The second consonant at index 1 is “<<consonants.at(1)<<endl;cout<<” The third consonant at index 2 is “<<consonants.at(2)<<endl;cout<<” The fourth consonant at index 3 is “<<consonants.at(3)<<endl;cout<<” The fifth consonant at index 4 is “<<consonants.at(4)<<endl;cout<<” The sixth consonant at index 5 is “<<consonants.at(5)<<endl;cout<<” The seventh consonant at index 6 is “<<consonants.at(6)<<endl;}

display console for the second method

Modifying the content of a vector in C++

To change the content of a vector, we use the following syntax:

vector_name.at(element_index)

The code of the program

Vectors in c++

#include<iostream>#include<vector>using namespace std;int main(){ cout<<endl;vector<char> consonants {‘b’, ‘c’, ‘f’, ‘k’, ‘l’, ‘m’, ‘t’, };cin>>consonants.at(0);cin>>consonants.at(1);cin>>consonants.at(2);cin>>consonants.at(3);cin>>consonants.at(4);cin>>consonants.at(5);cin>>consonants.at(6);cout<<endl;cout<<” The first consonant at index o is “<<consonants[0]<<endl;cout<<” The second consonant at index 1 is “<<consonants[1]<<endl;cout<<” The third consonant at index 2 is “<<consonants[2]<<endl;cout<<” The fourth consonant at index 3 is “<<consonants[3]<<endl;cout<<” The fifth consonant at index 4 is “<<consonants[4]<<endl;cout<<” The sixth consonant at index 5 is “<<consonants[5]<<endl;cout<<” The seventh consonant at index 6 is “<<consonants[6]<<endl;}

Push elements into a C++ vector

Push elements 

If we want to push an elements into a C++ vector we use the function push() function using the following syntax.

consonants.push_back(‘x’);

The code of the program

use the function push function

#include<iostream>#include<vector>using namespace std;int main(){ cout<<endl;vector<char> consonants {‘b’, ‘c’, ‘f’, ‘k’, ‘l’, ‘m’, ‘t’, };consonants.push_back(‘x’);cout<<endl;cout<<” The first consonant at index o is “<<consonants[0]<<endl;cout<<” The second consonant at index 1 is “<<consonants[1]<<endl;cout<<” The third consonant at index 2 is “<<consonants[2]<<endl;cout<<” The fourth consonant at index 3 is “<<consonants[3]<<endl;cout<<” The fifth consonant at index 4 is “<<consonants[4]<<endl;cout<<” The sixth consonant at index 5 is “<<consonants[5]<<endl;cout<<” The seventh consonant at index 6 is “<<consonants[6]<<endl;cout<<” The eighth consonant at index 7 is “<<consonants[7]<<endl;}

add the x character to c++ victorThe vector in c++ will automatically give the demanded space to the pushed element.

Conclusion

The C++ vector is a data structure that has the following characteristics:

  • Dynamic size
  • C++ victors elements always have the same type.
  • Elements are stored contiguously in memory.
  • We can access the elements by their index or their position.
  • The first element of the C++ vector has an index of 0.
  • The last element of the C++ vector has an index of size-1
  • C++ vector provides some functions that bounds check.
  • We can use looping in vectors.

Pin It on Pinterest

Share This