Introduction to C++ Containers Vector, List, Set, Map
Data structures in the C++ programming language provide its users with a variety of rich set data types, which are added to C++ through built-in Standard Template Library (STL) containers called containers. These help C++ users store and manipulate collections of data in a proper order. These C++ container data types are built-in features of vector, list, set, map, and other header files in the C++ programming standard library. In C++, any container like vector, list, set, map, etc. is used for a specific purpose, and these vector, list, set, map, data types are optimized in C++ for multiple individual purposes.

So, let’s learn in detail about the most commonly used vector, list, set, and map data type containers in C++.
Vector Data Type Concept in C++.
In C++ programming, a vector is a dynamic array data type concept that can automatically modify its default size when a vector element is added or deleted from the current program. The vector data type provides immediate, fast access to its stored data elements using the index method, making it the best choice for situations where C++ programmers need to access random data information.
Important Features of the Vector Data Type.
- Dynamic resizing – Adding a new element to a vector data type can automatically modify its size.
- Efficient random access – The vector data type uses an O(1) time method to access a stored element from its index.
- Efficient at the end – It adds or deletes a new element from the last O(1) location in the vector data type.
- Doesn’t work properly from the front – inserting or deleting a new element at the beginning or middle of a vector data type requires shifting the element, making it an O(n) shift.
Example of using vector in C++ programming.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> vctr; // Here we declare a vector of integers
// Here we add elements to the vector database element
vctr.push_back(89); // Here we add 89 to the vector end
vctr.push_back(23); // Here we add 23 to the end
vctr.push_back(77); // Here we add 77 to the end
vctr.push_back(01); // here we add 01 to the end
// here it accessing vector elements using indices index storage location method
cout << “First element of vector – ” << vctr[0] << endl; //89
cout << “Second element of vector – ” << vctr.at(1) << endl; //23
cout << “Last element of vector – ” << vctr.at(3) << endl; //01
// here it Iterating through the vector data element
cout << “All Element of Vector data is – “;
for (int p = 0; p < vctr.size(); p++) {
cout << vctr[p] << ” “;
}
cout << endl;
// here it is removing last vector element
vctr.pop_back(); // here it removes 01
return 0;
}
List Data Type Concept in C++.
In the C++ programming language, the list data type is a doubly linked list container data storage method that provides features for inserting and deleting data elements at both ends (start and end) and in the middle, in a proper order. As such, random data element access is not supported in the list data type. Accessing an element in the list data type requires proper iteration through the entire list data.
Important Features of the list data type.
- Straightforward insertion and removal – Inserting and deleting a new data element at any location (beginning, middle, or end of the list) is O(1) location.
- No random access – Accessing a list data element when needed is O(n), requiring iteration through a loop in the list data.
- Memory overhead – The list data type requires storing additional pointers (next and previous) for each data element in the list data type.
Example of using the list data type.
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> lst; // here we are declaring a list of integers data type element
// here we adding elements to the list data type
lst.push_back(34); // here it adds 34 to the active list end
lst.push_back(55); // here it adds 55 to the active list end
lst.push_front(66); // here it adds 66 to the active list end
lst.push_front(89); // here it adds 89 to the front active list
// here it Iterating through the list element
cout << “Element of List contents is – “;
for (int n : lst) {
cout << n << ” “;
}
cout << endl;
// here it is removing the list elements
lst.pop_front(); // here it removes the first list element (89)
lst.pop_back(); // here it removes the last list element (55)
cout << “After popping (Remove) front and back list element – “;
for (int n : lst) {
cout << n << ” “;
}
cout << endl;
return 0;
}
Set Data Type Concept in C++.
In the C++ programming language, a set is an associative data type container that stores unique data elements in a sorted alphabetical sequence. Internally, the set data type is typically implemented as a balanced binary search tree (such as a red-black tree). The set data type does not allow the storage of duplicate data elements and automatically sorts and arranges the elements.
Important Features of the Set Data Type.
- Unique element – The set data type allows no duplicate values of any type.
- Automatic sorting – The set data type stores elements in a sorted order based on a comparison function.
- Efficient search – The set data type uses an O(log n) time complexity method for easy search, insert, and delete operations.
Example of Set data type usage in C++.
#include <iostream>
#include <set>
using namespace std;
int main() {
set<int> sat; // here it is declaring a set of integers element
// here it is adding elements to the set data type
sat.insert(90);
sat.insert(70);
sat.insert(99);
sat.insert(99); // we add duplicate set element, and this will not to be added in set group
//here it iterating through the set element
cout << “The list of Set element is – “;
for (int n : sat) {
cout << n << ” “; // here set data will automatically sort the elements in ascending order
}
cout << endl;
//here it checking for a set element
if (sat.find(99) != sat.end()) {
cout << “99 is in the set.” <<endl;
}
// here it removes an element from the set
sat.erase(70);
cout << “After removing the set element – “;
for (int n : sat) {
cout << n << ” “;
}
cout << endl;
return 0;
}
Map Data Type Concept in C++.
In the C++ programming language, a map is an associative data type container storage concept that stores and processes data values in key-value pair order. The map data type allows extracting, inserting, and deleting key-value pairs in the proper order based on the key. Like the set data type, the map data type can be implemented simply as a balanced binary data search tree.
Important Features of the Map Data Type.
- Key-value pairs – Each stored data element in the map data type is a pair, consisting of a key and a value.
- Unique keys – Each key in a map data type must be unique and distinct. However, duplicate values can be present in the map.
- Automatic sorting – Map data elements can be stored and processed in a sorted order based on their keys.
- Improved search – The O(log n) time complexity concept is used for element searches, insertions, and deletions in map data.
Example of using the map data type in C++.
#include <iostream>
#include <map>
using namespace std;
int main() {
map<string, int> mdata; // Here we declare a map data type of strings to integer values
// Here we add a key-value pair to the map data type
mdata[“Java”] = 90;
mdata[“Python”] = 70;
mdata[“Matlab”] = 55;
mdata[“Xcode”] = 20;
//here we Iterating through the map data type
cout << “Map data type contents is – “;
for (auto& pair : mdata) {
cout << pair.first << ” -> ” << pair.second << “, “;
}
cout << endl;
// here we access map value by key
cout << “The value for key ‘Java’ is – ” << mdata[“Java”] << endl;
cout << “The value for key ‘Xcode’ is – ” << mdata[“Xcode”] << endl;
// here we removing a map key-value pair
mdata.erase(“Python”);
cout << “After delete ‘Python’ map element – “;
for (auto& pair : mdata) {
cout << pair.first << ” => ” << pair.second << “, “;
}
cout << endl;
return 0;
}
Detail summary of vector, list, set, map, Containers in c++
| Container | Data Type method | Key Features elements | TimeComplexity (Access/Search/Insert/Erase) method |
| Vector data type | Storage data type Sequence container method | Used to store dynamic array, fast random access, it can resize with any changes or modification | We can access vector data as O(1) for access, O(n) for insertion/removal at middle location |
| List data type | List data store in Sequence container (doubly linked list) data type method | In list as linked list possible efficient insertions and deletions anywhere in start middle or end | We can access O(n) for access, O(1) for insertion/removal list data in linked list order |
| Set data type | Set data type store element in associative container order | It used to Sorted, unique set data elements | We can access O(log n) for insert, erase, and search data operation in set data type |
| Map data type | Map data type used associative container (key-value pairs) order | Map data type Sorted key-value pairs, unique keys with data elements | We can access map data O(log n) for insert, erase, and search key value pair operation |

