C++Builder How To Define Vectors In Modern C++ On Windows

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,203
Credits
4,034
How To Define Vectors In Modern C++ On Windows
By Yilmaz Yoru November 1, 2021

What are the Vectors in C++? How can we use std::vector? How can I define vector in different data types? Can I define structs or other datatypes in std::vector? 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.

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 they can be resized, and can be accessed and traversed using iterators.

When we Insert data into vectors it may more time than 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 ‘]’ just as we do with 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 define different vector types.

How to define C++ Vector types?​

The object type of a vector can be a Boolean, char, integer, float, double, string, complex number, struct and any other object types in C++. This allows you to use any datatype as a dynamic array. Here are definition examples of various kinds of vectors;
C++:
#include <iostream>
#include <vector>
#include <complex>
#include <string>
 
struct mystruct
{
 float m,x,y,z;
};
 
int main()
{
 std::vector<bool>        vecb;  // Vector in bits
 std::vector<char>        vecc;  // Vector in bytes
 std::vector<int>         veci;  // Vector in integers
 std::vector<float>       vecf;  // Vector in floats
 std::vector<double>      vecd;  // Vector in doubles
 std::vector<long int>    vecl;  // Vector in long integers
 std::vector<std::string> vecs;  // Vector in strings
 
 std::vector<std::complex<double>> veccomp; // Vector in complex numbers
 std::vector<struct mystruct> vecstruct; // Vector in structure form
}