C++ Containers Cheat Sheet
This cheat sheet provides a concise overview of the most commonly used C++ containers, their characteristics, and key operations.
Introduction to Containers
C++ containers are data structures that provide efficient storage and retrieval of elements. They offer a standardized interface, making them versatile and easy to use.
Common Container Types
Here's a summary of common C++ container types:
1. Sequence Containers:
-
std::array
: Fixed-size array with elements stored contiguously in memory.- Pros: Efficient access to elements, known size at compile time.
- Cons: Fixed size, cannot resize dynamically.
-
std::vector
: Dynamically resizable array with elements stored contiguously.- Pros: Efficient access to elements, dynamic resizing.
- Cons: Potential for reallocation overhead when resizing.
-
std::list
: Doubly-linked list with elements not stored contiguously.- Pros: Efficient insertion and deletion anywhere in the list.
- Cons: Less efficient random access compared to arrays.
-
std::forward_list
: Singly-linked list, optimized for insertion and deletion at the beginning.- Pros: Efficient insertion and deletion at the front.
- Cons: Limited functionality compared to
std::list
.
2. Associative Containers:
-
std::set
: Sorted set that stores unique elements.- Pros: Efficient search, insertion, and deletion operations.
- Cons: No direct access to elements by index.
-
std::multiset
: Sorted set that allows duplicate elements.- Pros: Efficient search, insertion, and deletion operations, allows duplicates.
- Cons: No direct access to elements by index.
-
std::map
: Sorted map that stores key-value pairs.- Pros: Efficient search, insertion, and deletion operations based on keys.
- Cons: No direct access to elements by index.
-
std::multimap
: Sorted map that allows duplicate keys.- Pros: Efficient search, insertion, and deletion operations based on keys, allows duplicate keys.
- Cons: No direct access to elements by index.
3. Unordered Containers:
-
std::unordered_set
: Hash table that stores unique elements with fast search, insertion, and deletion.- Pros: Highly efficient search, insertion, and deletion operations.
- Cons: No ordering guarantees.
-
std::unordered_multiset
: Hash table that allows duplicate elements with fast search, insertion, and deletion.- Pros: Highly efficient search, insertion, and deletion operations, allows duplicates.
- Cons: No ordering guarantees.
-
std::unordered_map
: Hash table that stores key-value pairs with fast search, insertion, and deletion.- Pros: Highly efficient search, insertion, and deletion operations based on keys.
- Cons: No ordering guarantees.
-
std::unordered_multimap
: Hash table that allows duplicate keys with fast search, insertion, and deletion.- Pros: Highly efficient search, insertion, and deletion operations based on keys, allows duplicates.
- Cons: No ordering guarantees.
Choosing the Right Container
The choice of container depends on the specific needs of your application:
- Data ordering: Use
std::vector
,std::list
,std::set
,std::multiset
,std::map
, orstd::multimap
if you need sorted data. Usestd::array
,std::forward_list
,std::unordered_set
,std::unordered_multiset
,std::unordered_map
, orstd::unordered_multimap
if order is not crucial. - Element uniqueness: Use
std::set
,std::unordered_set
,std::map
, orstd::unordered_map
if you need to store unique elements. Usestd::multiset
,std::unordered_multiset
,std::multimap
, orstd::unordered_multimap
if duplicates are allowed. - Efficiency: Consider the time complexity of operations for insertion, deletion, searching, and accessing elements.
Example Usage
#include
#include
#include
int main() {
// Create a vector of integers
std::vector numbers = {1, 2, 3, 4, 5};
// Access elements by index
std::cout << "First element: " << numbers[0] << std::endl;
// Add an element to the end
numbers.push_back(6);
// Print the vector
for (int number : numbers) {
std::cout << number << " ";
}
std::cout << std::endl;
// Create a set of strings
std::set names = {"Alice", "Bob", "Charlie"};
// Check if a name exists in the set
if (names.count("Bob") > 0) {
std::cout << "Bob is in the set" << std::endl;
}
return 0;
}
Further Exploration
- C++ Standard Library Documentation:
- Learn C++:
- C++ Containers: A Detailed Explanation: