C++ Container explained with Example Codes

(Last Updated On: April 4, 2021)

C++ Container:

C++ container is an object that is used to manage other objects, the elements here of the container. Leave the algorithms working with containers on a defined interface of data types and methods. Table 1 shows an overview of the various containers in the C++ standard library.

Table 1 Container overview

Container type Header Class template
Sequences <array>














Assorted associatives <map> Map





Disordered associatives <unordered_map> unordered_map
Container unordered_multimap
(Hash container) <unordered_set> unordered_set


Special cases <bitset>







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!

Associative C++ container:

Allow quick access to data using a key. There are two different dene concepts:

  1. A map describes a relationship between elements of two quantities. map containers support this concept. So the English drawing for a book by transferring the string “book” as a key to one suitable method of the container can be determined. In this case, the key is “Book” is assigned the date “book”. A lot of keys are stored in one A lot of assigned data (values) are mapped. The container elements are Pairs of keys and values. The type map identifies a unique map because a key is assigned to exactly one date (exception: multimap).
  2. A set is a collection of distinguishable objects, elements called, which have common properties. N = {0; 1; 2; 3; …} refers to the Example the set of natural numbers. set containers support the implementation use of this concept in the computer. Because the elements must be distinguishable there cannot be two identical elements in a set (exception: multiset).

C ++ also distinguishes between two – depending on the underlying data structures.

Different types of C++ container:

  • Assorted associative C++ containers:

In the underlying data structure, the elements are sorted because there are sorted correctly when inserting. With the GNU C ++ compiler (and presumably also with all others) the data structure is a so-called red-black-Tree [CLR09], a variant of the binary search tree.

  • Disordered associative C++ containers

This type of container is useful when sorting is not required. The underlying data structure is based on a scatter storage method. There the elements according to a (pseudo-) random principle on the memory area “Scattered” (English hashing). Special feature: The access time to an element is constant with a suitable design of memory and random function, i.e. regardless of the number of stored items.

Common properties of C++ container:

Before the individual C++ container classes, common properties are first defined wrote. Table 2 shows the data from the C++ containers of the C ++ standard library for Data types provided and required by self-built containers of a C++ Container. X is the data type of the container, for example, vector <int>, and T is the Data type of a container element, for example, int. The type vector <int> :: value_type is then identical to int. These data types are standard in all C++ container classes Standard library included. With const_reference and const_iterator container Elements cannot be changed, only read access is possible.

Table 2: C++ Container Data types

Data type Meaning
X::value_type Type of saved items
X::reference Reference to container element
X::const_reference only be used for reading
X::iterator Iterator
X::const_iterator Read only iterator
X::difference_type signed integral type
X::size_type unsigned integral type for size specifications

X = type of container, for example, vector

Each C++ container exposes a public set of methods that are stored in a Program that can be used. The begin() and end() methods for vectors. Table 3 shows the containers of the C++ Methods provided by the standard library. X is again the name of the C++ Container type. These methods are in all container classes of the standard library contain.

Table 3: C++ Container methods and functions

Return type method Meaning
X() Default constructor; creates empty containers
X(const X&) Copy constructor
~X() Destructor; calls the destructors of all elements of the container
iterator begin() returns iterator referring to the first element of the container if present refers; otherwise, end () is returned
const_iterator begin() the element cannot be changed with this iterator
const_iterator cbegin()
iterator end() Position after the last element
const_iterator end()
const_iterator cend()
size_type size() Size of the container = number of currently saved elements
size_type max_size() maximum possible size of the container

(actually limited by the available storage space)

bool empty() (size () == 0) or (begin () == end ())
void swap(X&) Swap with argument container
void swap(a, b) a.swap(b)
X& operator=(const X&) Assignment operator

X = type of container, for example, vector

The swap () function is very fast because internally only the references to the memory areas are swapped. The exception is the class template array because it stores the data directly encapsulates. The time required for swap() is proportional to the number of elements of the array object.

begin(), end(), cbegin(), cend() in C++ Container:

If C++ container is a const reference to a container, begin() and end() returns a const_iterator, otherwise an iterator. But it may be that container is not const and yet only read operations should be carried out. Then 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:

How to Use the cbegin() and cend() in C++ container:

Relational operators in C++ container:

For all C++ containers with the exception of the priority_queue and the unordered associative Container, there are the well-known relational operators ==,! =, <, <=,> And> =, so that two containers can be compared with each other. The result is based on one lexicographic comparison. The prerequisite is of course the comparability of the Items stored in C++ containers.

Spellings in C++ container:

This section describes some notation conventions common to all of the following C++ Container descriptions apply. p and q are dereferenceable iterators of the same C++ container (type iterator). Often areas have to be specified. For this, the usual mathematical used for intervals. Hereinafter Text is [i, j) so an interval including i and excluding j. i and j are of the type of an input iterator. InputIterator means an iterator that is used for reading and that to another C++ container, also different Type, heard. A parameter n is of the integral type size_type of the container. A parameter t is an Element of the container type value_type (identical to the template parameter T).

Initialization lists in C++ container:

Initialization lists are known from the initialization of objects. They exist too for STL C++ containers. For example, are the instructions

It can any number of elements can be transferred ideal for a C++ container. The standard class initializer_list can be used in the same way for construction can be used by functions that respond to any number of parameters. should be called. Within the function, the list can be started() and end() be processed.

Construction in place in C++ container:

Inserting with the emplace() method creates a construction in place, to avoid a copy. The comparison with the vector method push_back () shows the effect.

Example: Construction in place with emplacing():

Reversible C++ Container:

A doubly linked list can be traversed in two directions, namely from the End to the beginning and vice versa. C++ Container with this property is called reversible Container. The iterators for such containers are bidirectional, that is, they can one step forward with the ++ operator and one step back with the — operator go away. Additional iterators are provided for such containers that are supported by the At the end of the day, you can run to the beginning with the ++ operator. You have the type reverse_iterator and can be determined using the appropriate methods:

const_reverse_iterator rbegin() const,

const_reverse_iterator crbegin() const and

reverse_iterator rbegin()

return a reverse iterator pointing to the last element.

const_reverse_iterator rend() const,

const_reverse_iterator crend() const and

reverse_iterator rend()

return a reverse iterator pointing to a fictitious position before the first element shows. Application example for a container of the type vector <int>:

Example: Outputting data using an iterator, starting at the end:


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