What are C++ Vectors? C++ Vector Synthesis

One of the highlights that makes C++ superior to its "predecessor" C is Vector. So, what is Vector C++? Why use Vector C++? What is the meaning of declarations and functions in C++? All will be answered by Tino Group in this article!

What are C++ Vectors?
To solve a customer list management problem or manage a library or school, you can use many ways such as: using arrays, using linked lists, queues, stacks, etc. If you use conventional methods in one exercise, this will be very easy. However, for problems with more variability in arrays, Vector C++ will be the most appropriate method.

Vector C++ is a potentially "dynamic" array quite similar to dynamic array. However, Vector C++ has the ability to automatically resize if an element is removed or inserted. This means that the container will handle the storage itself.

C++ Vector elements will be placed in a contiguous memory or contiguous storage and you can use iterator to access and traverse them.

Why use Vector C++?
There are many reasons you have encountered in the process of programming such as:

You don't want to create a new array from scratch, manually copy the elements then delete the old array
Do you have to start over every time you resize the array?
Are you bored with dynamic array management with C++ pointers?
Let's learn about Vector C++ right away so you can experience the following wonderful things:

You need to expand the array size? You just need to add 1 part from Vector and they will automatically increase.
You do not need to declare the size of the array because Vector can expand the volume and size by itself.
You can understand the number of elements stored in the array
You can use negative number elements in Vector and use that to implement algorithms.
Not only at Vector C++, you can also take full advantage of the power of STL - Standard Template Library , a C++ template library. Since the STL is a larger part that includes Vector C++, let's just go through it briefly!

What is the Standard Template Library?
The Standard Template Library is often abbreviated as STL . This is a collection of many functions that are mainly used to store and process data.

We can define the STL as a library of containers as well as algorithms and loops. In addition, STL was developed so that you can reuse pre-written and tested code to save time and effort.

And Vector C++ is part of the Standard Template Library.

After reading the benefits Vector C++ brings, do you want to learn about Vector C++? If yes, we will continue to explore together!

Synthesis of Vector C++ functions
How to use Vectors in C++
How to declare a Vector in C++
First you will need to declare the #include<Vector> library , then you can use Vector C++!

The C++ Vector declaration formula and example are as follows:

Vector< object_type > Vector_variable_name;
std::Vector<int> my_Vector;
After the declaration is complete, you can also assign a value to Vector_variable_name or in the example my_Vector.

Vector<int> my_Vector = {1,3,5,7,9}
How to initialize Vector in C++
We will have 4 main ways to initialize including:

Using the constructor overload of Vector Class
Using Array
Use pre-initialized Vectors
If you want the elements stored in the Vector to be pushed back one by one, you can use push_back(), the statement is like this:

Using the constructor overload of Vector Class
In case you want to use a Vector with the same value more than once, you use the statement:

Vector<object_type> Vector_name (number_of_repetition,element_value);
Using Array
You want to use an array as a parameter passed to Vector? You just need to execute the command:

Vector<object_type> Vector_name {val1,val2,val3,....,valn}
Use pre-initialized Vectors
If you want to reuse an initialized Vector and not affect the value of that Vector, you do the following:

Vector<object_type> Vector_name_1{val1,val2,…,valn};
Vector<object_type> Vector_name_2(Vector_name_1.begin(),Vector_name_1.end())
Learn about functions/Functions in Vector
push_back(): as in the example above, this function is used to push an element in the Vector later. If the object type passed in the parameter is not the same as the Vector's type, they are thrown away.
assign(): overwrite new values ​​by replacing old values
pop_back(): used to reduce the size of the Vector by 1 element.
insert(): used to insert a new element before the position pointed by the loop
erase(): used to remove elements from the sp ring .
swap(): used to swap the value/content of a Vector of the same type, not necessarily the same size.
clear(): used to remove the elements of the Vector container
begin(): Used to return an iterator pointing to the first element in the Vector.
end(): used to return the iterator to the last element in the Vector.
You can observe the following example:

#include <iostream>
#include <Vector>
using namespace std;
int main()
Vector<int> vec1;
for (int i = 1; i <= 10; i++)
cout << "Understanding begin() and end() function: " << endl;
for (auto i = vec1.begin(); i != vec1.end(); ++i)
cout << *i << " ";
return 0;
The output we have is:

vector-c ++ - la-gi
In this example, you can see that Tino Group uses begin() and end() functions. We first create vec1, then push back the values ​​from 1 to 10 with a for loop . Then we print the values ​​of the Vector using a for loop and use the begin() and end() functions to specify the end point of the loop

size(): this function returns the number of elements in the Vector.
max_size(): function used to return the maximum number of elements Vector can hold.
capacity(): function used to return the size of the Vector's storage space given numerical graph.
resize(): function used to hold "n" elements. If the current size of the Vector is greater than n, the elements after the n will be removed from the Vector and the additional element IDs will be inserted after the Vector.
empty(): if the return value of the function is true, your Vector is empty. If the return value is false, your Vector is not empty.
Through this article, you also have a better understanding of what Vector C++ is, right? Probably, Vector C++ is quite complicated. However, this will make it much easier for you to manage and execute your commands! Tino Group wishes you success on the path of programming with C/C++!

Frequently asked questions about Vector C++
What should C++ language be used for?
If you want to become a professional game developer C/C++ language will be perfect for you! Also, you can learn and master C/C++ then any other programming language will become easier to learn.

What are the most popular programming languages ​​in the world?
Currently, the most popular programming languages used in the world include: Python , C, C++, C#, Java, JavaScript, etc.

Where to learn more about Vector C++?
If you don't like learning through articles, blogs. You can use Youtube and find keywords related to Vector C++. There will be a lot of detailed video tutorials by programmers from many countries.

As for the writer alone, I like the explanation of the Indian friends the most, although some of your English is not so excellent, but the Indian programmers are really impressive.

Which IDE should I use for C++ programming?
You are getting used to C++, maybe the lightweight, compact Dev-C++ software will be the right choice for you. If you want more professional IDE software, you can use Visual Studio Code and install Extension C/C++ to further extend this programming language. You can make the most of the power of Visual Studio Code to get the most out of your work!

Address: 107 Dong Nai - Vietnam. - Email: services@cmt8.net - Phone: 18001119
Copyright © 2012 - BlogMe. All rights reserved