Welcome to the Standard Template Library in C++ repository! This repository provides an in-depth exploration of the containers in the C++ Standard Template Library (STL), demonstrating how each container works, its operations, and performance characteristics. Whether you are a beginner looking to understand the basics of STL or an advanced user seeking optimization tips, this repository has got you covered.
The Standard Template Library (STL) in C++ is a collection of template classes and functions that implement common data structures and algorithms. STL includes several powerful containers, such as vectors, maps, sets, lists, and queues, each designed to handle different types of data manipulation efficiently.
This repository serves as a guide to understanding how these containers function, how to use them, and the performance trade-offs of each. Through well-documented examples and explanations, you can explore the core features of each container.
The following STL containers are explored in this repository:
- Vector
- Dynamic array, which allows random access and supports efficient resizing.
- List
- Doubly linked list, optimized for insertions and deletions at both ends.
- Deque
- Double-ended queue that allows fast insertions and deletions at both ends.
- Set
- Collection of unique elements with automatic sorting.
- Map
- Key-value pairs where each key is unique, and values are associated with keys.
- Queue
- FIFO (First In First Out) data structure.
- Stack
- LIFO (Last In First Out) data structure.
- Priority Queue
- Specialized queue that allows access to the highest priority element.
Each container includes a breakdown of:
- Basic operations (insert, delete, access)
- Time complexities of each operation
- Real-world use cases and performance considerations
Here are some common operations demonstrated for each container:
- Insertion
- Deletion
- Traversal
- Search
- Modification
- Sorting (where applicable)
These operations are explained with code examples and their time complexities are discussed.
Each container comes with a set of examples that show how to use it in real-world scenarios. Here’s a brief example for a Vector:
#include <iostream>
#include <vector>
int main() {
std::vector<int> vec;
// Inserting elements
vec.push_back(10);
vec.push_back(20);
vec.push_back(30);
// Accessing elements
std::cout << "First element: " << vec[0] << std::endl;
// Traversing the vector
for (int i = 0; i < vec.size(); ++i) {
std::cout << "Element " << i << ": " << vec[i] << std::endl;
}
// Deleting an element
vec.pop_back();
std::cout << "After pop_back, size: " << vec.size() << std::endl;
return 0;
}
┌─────────────────────────────────────────────┐
│ SETS │
├───────────────┬────────────┬───────────────┤
│ SET │ MULTISET │ UNORDERED_SET │
├───────────────┼────────────┼───────────────┤
│ [2,3,4,5] │[2,2,3,3,4] │ [5,2,4,3] │
│ │ │ │
│ ✓ Unique │ × Not │ ✓ Unique │
│ ✓ Sorted │ Unique │ × Not │
│ │ ✓ Sorted │ Sorted │
└───────────────┴────────────┴───────────────┘
┌─────────────────────────────────────────────┐
│ MAPS │
├───────────────┬────────────┬───────────────┤
│ MAP │ MULTIMAP │ UNORDERED_MAP │
├───────────────┼────────────┼───────────────┤
│{1:"A",2:"B"} │{1:"A", │ {2:"B", │
│ │ 1:"B", │ 1:"A", │
│ │ 2:"C"} │ 4:"D"} │
│ ✓ Unique Keys │ × Multiple │ ✓ Unique Keys │
│ ✓ Sorted Keys │ Keys │ × Not │
│ │ ✓ Sorted │ Sorted │
└───────────────┴────────────┴───────────────┘
- Set: O(log n) for insertion/deletion
- Multiset: O(log n) for insertion/deletion
- Unordered_set: O(1) average, O(n) worst case
- Map: O(log n) for insertion/deletion
- Multimap: O(log n) for insertion/deletion
- Unordered_map: O(1) average, O(n) worst case
To explore the code:
-
Clone this repository:
git clone https://github.com/yourusername/Standard-Template-Library-in-CPP.git
-
Navigate into the project directory:
cd Standard-Template-Library-in-CPP
-
Compile and run the example files using your favorite C++ compiler:
g++ -o vector_example vector_example.cpp ./vector_example
Feel free to modify the examples to explore different operations or containers.
Contributions are welcome! If you find any issues or have improvements or suggestions for the repository, please feel free to fork the repository and submit a pull request.
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch
). - Make your changes.
- Commit your changes (
git commit -am 'Add new feature'
). - Push to your branch (
git push origin feature-branch
). - Open a pull request.
This repository is licensed under the MIT License - see the LICENSE file for details.