C++Builder How To Set And Print Vector Members On Windows

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
How To Set And Print Vector Members On Windows
By Yilmaz Yoru September 22, 2021

What are the Vectors in C++? How can we define std::vector? How can I print a vectormember? How can I change the number of vectors? How can I use resize() method with vectors? How can I iterate a vector in for loop? How can we print a vector member? Let’s answer these questions.

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

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.

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

How do I set and print Vector Members?​

We can set members of a vector and we can print out each member by using vector iterators as below,
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';
 std::cout << '\n';
 
 getchar();
 return 0;
}
Vectors can be set in runtime and we can resize them by using their resize() method, also we can use push_back() method to add new members. We can also print them by using indices. See example below:
C++:
#include <iostream>
#include <vector>
 
int main()
{
 std::vector<int> vec;
 
 vec.resize(2);
 vec[0]=17;
 vec[1]=23;
 
 vec.resize(4);
 vec[2]=29;
 vec[3]=35;
 
 vec.push_back(45); // add and resize to the end
 
 for(int i=0; i<vec.size(); i++)
 {
 std::cout << vec[i] << ',';
 }
 
 getchar();
 return 0;
}