Javatpoint Logo
Javatpoint Logo

Add in Vector C++ Language

Vectors are a powerful data structure that are widely used in programming. They are similar to arrays but have additional features such as dynamic resizing capabilities. In C++, vectors are implemented as classes in the Standard Template Library (STL) and can be used to store elements of any type. The STL provides a variety of member functions that can be used to add, remove, or access elements, as well as to resize the vector. Vectors are very useful for storing and manipulating collections of data.

They can be used for a wide range of applications such as storing lists of numbers, complex data structures, and algorithms that require dynamic resizing of the data set. Additionally, vectors are efficient in terms of memory usage and performance, which makes them a popular choice for many programming tasks. They are widely used in software development, particularly in areas such as game development, simulations, data analysis, and data science predictions.

In C++, vectors are defined in the <vector> header and are used to store elements of any type. They have several member functions that allow you to add, remove, or access elements, as well as to resize the vector.

C++ Code


Vector size: 4
myVector[0] = 1
myVector[1] = 2
myVector[2] = 3
myVector[3] = 4
Vector size after removing last element: 3
Vector size after inserting an element: 4
myVector[0] = 0
myVector[1] = 1
myVector[2] = 2
myVector[3] = 3


The above code is an example of a C++ program that demonstrates the use of vectors. The program includes the necessary headers, iostream for input and output, and vector for the vector class. In the main function, the program starts by declaring a vector myVector to store integers. Then, it uses the push_back() function to add 4 integers to the vector. The push_back() function adds an element to the end of the vector. Then, the program uses cout to print the size of the vector, which is 4 in this case. It also uses a for loop to iterate through the elements of the vector and print their values. The elements of the vector can be accessed using the [] operator, like an array.

Next, the program uses the pop_back() function to remove the last element of the vector. Then, it uses cout to print the new size of the vector, which is now 3.After that, the program uses the insert() function to insert an element at the beginning of the vector. The insert() function takes two arguments, the first one is the position where to insert the element, and the second one is the value of the element to insert. In this case, the element 0 is inserted at the beginning of the vector. Finally, the program uses a for loop to iterate through the elements of the vector again and print their values, this time showing the effect of the insert operation.

This example demonstrates several common operations that can be performed on vectors, such as adding elements, removing elements, and inserting elements, as well as accessing the elements and getting the size of the vector. It's a very basic example, but it shows the basic functionalities of vectors and how they can be used in C++.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA