C++ has really great features that come with its modern libraries. One of the most used and very useful things is
std::vector. If you know arrays in C and C++, vectors are a modern and very flexible form of arrays in C++, maybe we can say they are like modern linked lists. If you want to use arrays there is also
std:::array. Both can be used together with an appropriateC++ build toolthat supports C++17. In this article, we describe the differences between std::vector and std::array in C++.
In C++, the STL Standard Template Library has many algorithms for some operations like searching, counting, and manipulation of ranges and their elements. C++17 has a new feature that you can sort both of these vectors and arrays with the
std::sortParallel STL Algorithm. You can use Iterators and many other operators and other properties to manipulate C++ vectors.
Table of Contents
What is std::array in C++?
In C++, after the C++11 standards, an
std::array)is a container that encapsulates fixed-size arrays. This is a C++11 array that can be used in later C++ versions like C++14, C++17, C++20 too. You just need to include the
<array> header as below.
std::array is one of the sequence containers of the Containers Library which also has
map, among others.
std::array is an aggregate type with the same semantics as a struct holding aC style array as its only non-static data member. Unlike a C-style array,
std::array doesn’t decay a pointerautomatically. As an aggregate type, it can be initialized withaggregate-initializationgiven at most
Ninitializers that are convertible to
Type. For example, a
std::array can be used as below,
std::array<object type, <number of elements> > variable_name;
we can create an array,
ar, with 100 members as shown below.
std::array<int, 100> ar;
For example, if you want to sort an array we can do the following:
std::sort( ar.begin(), ar.end() );
What is std::vector in C++?
Vectorsare dynamic arrays included in
<vector> libraryin 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 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.
What is the difference between a linked list and a vector in C++?
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.
How do I define a vector in C++?
A vector can be defined using this syntax,
std::vector<object type> variable_name;
we can declare a vec vector with 100 members as below,
How do I sort a vector in C++?
Now let’s see how we can sort a lot of vector elements with
std::sort algorithm. We can use default sort operator as below,
std::sort( vec.begin(), vec.end() );
What is the main difference between a vector and an array in C++?
The main difference between
std::array is that the number of elements in vectors are resizable in heap memory, whereas arrays have a fixed number of elements in the stack memory.
From a professional perspective, you should consider that
std::array uses stack memory and
std::vector uses heap memory. That means, the allocation of an array (of all array elements) happens on contiguous blocks of memory, in other words the allocation happens in the function call stack. Stack memory has a small amount of memory, which means you can use an array with very less memory than vectors. In general, they are safe and faster, and cannot be accessible by other threads. When a function is called which has a local
std::array inside, the size of memory is known by the compiler and when we call this function, all array members get allocated on the stack memory. When we exit from the function (the function call is over), this stack memory including elements of the array is de-allocated.
On the vector side, when we declare a vector in a function, the memory for the elements of vectors is allocated in the heap memory which means allocated during the execution of instructions written by developers. The heap memory is a pile of memory space available to programmers to allocate and de-allocate, which means you can add a large amount. Note that, the heap memory allocation isn’t as safe as stack memory allocation because the data stored in this space is accessible or visible to all threads.
What are the differences between vector and array in C++
std::arrayis index based, static memory allocation for the defined number of elements on the stack memory. Vectors are not index based dynamic elements, and number of elements can be increased by insertion, they uses heap memory.
std::array objects are efficient and faster, but you can store fewer elements than vectors. Vectors are sequential containers, arrays are fixed-size memory blocks. Both uses iterators, some capacity methods, other operations and some modern algorithms. Their elements can be accessed via
[ ] operator and
Let’s list all of these in a table.
|std::array in C++||std::vector in C++|
|static, number of elements are defined in declaration||dynamic, number of elements can be increased by insertion|
|uses stack memory||uses heap memory|
|uses efficient memory||requires more memory for elements|
|accessing elements takes less time in a contiguous memory allocation||accessing to elements takes more time in a heap memory allocation|
|fixed-size memory blocks||sequential container|
|used to store elements in a block||used to store sequential collection of elements of the same type|
|index based||not index based|
|element access via [ ] operator, at, front back, data||element access via [ ] operator, at, front back, data|
|capacity access via size, max_size, empty||capacity access via size, max_size, empty, reserve, capacity, shrink_to_fit|
|fill and swap operations can be used||clear, erase, insert, emplace, push_back, pop_back, emplace_back, resize, swap can be used|
|Iterators can be used ( begin, end, cbegin, cend, rbegin, rend, … )||Iterators can be used ( begin, end, cbegin, cend, rbegin, rend, … )|
|modern algorithms can be used (i.e. std::sort)||modern algorithms can be used (i.e. std::sort)|
std::vector examples can be found in learncplusplus.org, just search for the keywords.
C++ Builderis 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 fromhere. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download fromhere.
Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial