C++Builder How To Add C++ Vector Members on Windows

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
How To Add C++ Vector Members on Windows
By Yilmaz Yoru October 11, 2021

What are the Vectors in C++? How can I add a new member to std::vector? How can I delete a vector member in C++? How can we use push_back() method in vectors? How can we use pop_back() method in vectors? How can we use clear() method in vectors? Let’s answer these questions.

Для просмотра ссылки Войди или Зарегистрируйся and Для просмотра ссылки Войди или Зарегистрируйся in C++ are explained well in our previous posts. These were coming from the C, in this post, we will explain another great feature of C++, The Vectors.

What are vectors in C++?​

Vectors are dynamic arrays included in <vector> library in modern C++ and they can resize themselves automatically when a member of a vector is inserted or deleted. Vectors are the same as dynamic arrays and these dynamic arrays of vectors are handled automatically by the container. Vectors are the way of Modern C++, their members are placed in the contiguous memory storage, thus it can be resized, and it can be accessed and traversed using iterators.

When we Insert data into vectors, this may take a time than other static arrays because of the need of extending the vector array. Vectors have low memory usage as in dynamic array implementations, because of having good data cache utilization and locality of reference. We can easily access an element of a vector by giving its index between ‘[‘ and ‘]’ as same as arrays, which means vector members can be referenced by indices.

Vectors allow random access; that is, an element of a vector may be referenced in the same manner as elements of arrays (by array indices). Linked lists and sets, on the other hand, do not support random access or pointer arithmetic. Vectors are very useful for storing data in lists whose number of elements (length in total) may not be known before setting up the list. Because the vector data structure allocates the necessary memory needed for specific data storage Erasing and clearing vector elements from a vector does not need to free any of the memory associated with that element. That makes vectors much more safe and modern in C++ than arrays.

How do I define a vector in C++?​

A vector can be defined in this syntax,
C++:
std::vector<object type> variable_name;
Now let’s see how we can add and delete a vector member.

How do I add C++ Vector Members in a definition?​

C++:
#include <iostream>
#include <vector>
 
int main()
{
 std::vector<int> vec{ 10, 20, 30, 40, 50, 60, 70, 80 , 90};
 
 for (auto i: vec)   std::cout << i << ',';   std::cout << '\n';
 
 getchar();
 return 0;
}

How do I add a C++ Vector Member using the push_back method?​

We can add members by using push_back() method of vector. Here is a example,
C++:
#include <iostream>
#include <vector>
 
int main()
{
 std::vector<int> vec;
 
 for (auto i=1; i<10; i++) veci.push_back(i*10);
 
 for (auto i: vec)   std::cout << i << ',';   std::cout << '\n';
 
 getchar();
 return 0;
}