C++ Vector and Vector bool with examples

(Last Updated On: April 4, 2021)

C++ vector:

C++ vector, integrated by the header <vector>, represents public types and methods that are available, which are briefly described in my previous article. First of all, I would like to mention those already mentioned existing data types and methods are pointed out so as not to repeat them:

  • Data types
  • Constructors and methods
  • relational operators
  • Data types / methods

A C++ vector has other public data types, which are listed in Table 1 are. The declaration of the class is template <typename T> class vector;

Table 1: Additional file types for C++ vector

Data type Meaning
pointer Pointer to vector element
const_pointer can only be used for reading


The following list shows the methods of a C++ vector, in addition to those on referenced at the beginning of the section are usable. The time complexity is 0 (1), unless otherwise stated. Overloaded methods with a reference to an R-value as a parameter type are not listed.

  • vector (size_type n) creates a vector with n elements.
  • const_reference front() const and reference front() provide a reference to the first element.
  • operator = (initializer_list <T> il) assigns the initialization list il to the C++ vector.
  • const_reference at (size_type n) const and reference at (size_type n) return a reference to the nth element. The difference to the previous the operator [] () consists in checking whether n is in a valid range. If not, i.e. n is ≥ size (), an out_of_range exception is thrown.
  • const_reference back() const and reference back() provide a reference to the last element.
  • const_pointer data() const and pointer data() return a pointer to the first element. So data() == & front() applies if the C++ vector is non-empty. The pointer allows access to the data of the C++ vector as in a C array.
  • const_reference operator [] (size_type n) const and reference operator [] (size_type n) return a reference to the nth element.
  • reference emplace_back (args) adds an object whose constructor is called with the arguments args on End a. A reference to the inserted object is returned.
  • void push_back (const T & t) inserts t at the end.
  • void pop_back () deletes the last element.
  • void resize (size_type n) and void resize (size_type n, const T & t) change the C++ vector size. There are n-size () elements T () (or t) at the end added if n is larger than current size. If n is less than the current one Size, size () – n elements are deleted at the end.
  • void reserve (size_type n) Reserve storage space. Purpose: To avoid memory acquisition operations while using the C++ vector. The time complexity is O (n). If n <= capacity ​​(), nothing happens; otherwise, new memory is acquired and the new one The value of capacity () is equal to or greater than n. The content of the C++ vector, i.e. all advertising in the range [0, size ()) is retained. References (iterators, pointers) to these However, the range will be invalid.
  • size_type capacity () const returns the value of the currently available memory (capacity). size () is always less than or equal to capacity ().
  • void shrink_to_fit () reduces the capacity to match size (). The C ++ implementation is not tied to it to facilitate optimizations.



C++ vector iterator:

iterator begin():

returns iterator referring to the first element of the vector, if present, refers; otherwise end() is returned

const_iterator begin():

the element cannot be changed with this C++ vector iterator

iterator end():

Position after the last element in the vector

cbegin(), cend() in C++ vector:

the methods cbegin() and cend() are recommended. If then by mistake a changing statement such as * it = value; would be at the bottom of the loop the compiler already reports an error:

C++ vector <bool>:

A bitset allocates fixed memory, a C++ vector can dynamically allocate its memory mix change. If this is also required for the storage of bits and If one byte per bit is not to be wasted at the moment, a specialization is available of the class vector: the class vector <bool>. It has the same public data types like the vector class with the exception of the reference data type, which is used for manipulations individual bits are thought of:

Example of Inner class vector <bool> :: reference

The index operator of the class vector <bool> does not return bool, but rather a reference to an object of the internal class reference. That in turn can be done with operator bool () can be converted to bool. A bool vector has the methods of a vector <T> if instead of the placeholder T is used for the type bool. The vector <bool> specialization also offers the Void flip () method, which negates all elements. The following little program gives false true false off:


Example of Bool vector:

vector <bool> saves the values ​​as one bit per bool, so it is very compact and behaves therefore not quite like other vectors. So can’t get the address of an item be taken. If the number of bits does not have to be changeable, we recommend bitset as an alternative.

Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Recommended For You

About the Author: Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply