In C++, containers are part of the C++ Standard Template Library (STL) and offer different methods for storing and manipulating data. They are objects used to store the collection of other objects, are implemented as class templates, and provide an efficient way to manipulate and organize the data groups. Generally, there are three types of containers in C++:
Sequential container: It allows us to store elements that can be accessed sequentially.
Associative container: It stores the elements in a sorted manner regardless of their insertion order.
Unordered associative container: It is similar to associative containers but does not maintain specific order. For this container type, the existence of data matters.
Vector containers are sequence containers used to store elements of the same type. They are similar to arrays but can change their size dynamically and store elements in contiguous memory blocks. We can use vector containers in our code by including the vector
header file.
Here is an example of storing and manipulating data using a vector container:
#include <iostream>using namespace std;#include <vector>int main(){vector<int> vect_1;vect_1.push_back(6);vect_1.push_back(7);vect_1.push_back(8);cout << "printing vector values: ";for (auto i : vect_1)cout << i << " ";cout << endl;vect_1.pop_back();vect_1.clear();cout << "Vector size after clear() function:" ;cout<< vect_1.size();return 0;}
List containers are sequence containers used to store elements of the same type. They store elements in non-contiguous memory blocks, are implemented using a doubly linked list, and allow iteration in both directions. Moreover, they perform insertion and deletion of elements in a constant time and do not allow random access to elements. We can use lists in our code by including the list
header file.
Here is an example that shows the use of the list container in C++:
#include <iostream>using namespace std;#include <list>int main(){list<int> list_1;list_1.push_front(7);list_1.push_back(8);list_1.push_front(6);list_1.push_back(9);cout << "printing list values: ";for (auto i : list_1)cout << i << " ";cout << endl;list_1.pop_back();list_1.pop_front();list_1.clear();cout << "List size after clear() function: " ;cout<< list_1.size();return 0;}
Map containers are associative containers used to store elements in key-value pairs. They store pairs in an ordered manner regardless of their insertion order, and no two values can have the same key values. We can use maps in our code by including the map
header file.
Here is an example of storing and manipulating data using the map container:
#include <iostream>using namespace std;#include <map>int main() {map<int, string> map_1;map_1[1] = "Mon";map_1[2] = "Tue";map_1[3] = "Wed";map_1[4] = "Thur";map_1[5] = "Fri";cout << "Value at key 3: " << map_1[3] << endl;if (map_1.find(6) != map_1.end())cout << "Value at key 6: " << map_1[6];elsecout << "Key 6 not found in the map." << endl;// Iterate through the map and print all key-value pairscout << "Printing map values:" << endl;for ( auto i : map_1) {cout << "Key: " << i.first << ", Value: " << i.second ;}return 0;}
The key differences between vector, list, and map containers are listed below:
Feature | Vector | List | Map |
Container | Sequential | Sequential | Associative |
Data Structure | Dynamic array | Doubly linked list | Associative array |
Implementation | Contiguous memory | Non-contiguous memory | Balanced binary search tree |
Random Access | Efficcient as it can access elements in (O(1)) | Inefficient due to sequential traversing as it uses (O(N)) | Less efficient compared to vector |
Search | Linear search (O(n)) | Linear search (O(n)) | Binary search (O(log n)) |
Duplicates | Can exist | Can exist | Cannot exist |
Memory Overhead | Low | High | High |
In C++, vector, list, and map containers organize and manage data efficiently. Each of these containers has a specific purpose and its advantages. It is important to note the following:
Vectors are dynamic arrays suitable for adding or deleting from the back of the container, providing constant time access to its element.
Lists are based on a doubly linked list, providing an efficient way to insert or delete an element at any container’s position. It is ideal for the scenario where insertion or deletion occurs frequently.
Maps are used to store values in key-value pairs and are ideal to use when looking for values based on their keys.
Free Resources