Arrays and structs 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 safer and more modern in C++ than arrays.
In this post, you will learn more about Vectors in C++, including how to define std::vector, how to print a vectormember, how to change the number of vectors, how to use the resize() method with vectors, and how to iterate a vector in a for loop and print a vector member. By learning How to set and print vector members on windows, and how to compile c++ in windows. It will help you to easily build C++ applications.
A vector can be defined in this syntax,
1 2 3 |
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,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#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\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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#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] << ','; } std::cout << std::endl; getchar(); return 0; } |
Note that you can use vectors in your VCL and FMX applications in C++ Builder.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition