-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Day -14 : C++ Standard Template Library (STL) (#375)
Issue No. #270
- Loading branch information
1 parent
839ab74
commit d514e2f
Showing
5 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"label": "Day 14", | ||
"position": 15, | ||
"link": { | ||
"type": "generated-index" | ||
} | ||
} | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
### 2. stl-containers.md | ||
```markdown | ||
--- | ||
sidebar_position: 2 | ||
title: "STL Containers" | ||
description: "In this tutorial, we will learn about various containers provided by the Standard Template Library (STL) in C++ with the help of examples. Containers are data structures that store objects and data." | ||
sidebar_label: "STL Containers" | ||
slug: stl-containers-in-cpp | ||
--- | ||
|
||
## STL Containers | ||
|
||
### What are Containers in STL? | ||
Containers are objects that store collections of other objects. The STL provides several different types of containers, each optimized for specific kinds of data manipulation. | ||
|
||
### Sequence Containers | ||
1. **Vector**: A dynamic array that can grow in size. | ||
```cpp | ||
#include <vector> | ||
#include <iostream> | ||
|
||
int main() { | ||
std::vector<int> vec = {1, 2, 3, 4, 5}; | ||
vec.push_back(6); // Add element at the end | ||
|
||
for (int i : vec) { | ||
std::cout << i << " "; | ||
} | ||
return 0; | ||
} | ||
2. **List**: A doubly-linked list. | ||
```cpp | ||
#include <list> | ||
#include <iostream> | ||
|
||
int main() { | ||
std::list<int> lst = {1, 2, 3, 4, 5}; | ||
lst.push_back(6); // Add element at the end | ||
|
||
for (int i : lst) { | ||
std::cout << i << " "; | ||
} | ||
return 0; | ||
} | ||
3. **Deque**: A double-ended queue that allows fast insertion and deletion at both ends. | ||
```cpp | ||
#include <deque> | ||
#include <iostream> | ||
|
||
int main() { | ||
std::deque<int> deq = {1, 2, 3, 4, 5}; | ||
deq.push_front(0); // Add element at the front | ||
|
||
for (int i : deq) { | ||
std::cout << i << " "; | ||
} | ||
return 0; | ||
} | ||
### Associative Containers | ||
|
||
1. **Set**: A collection of unique elements. | ||
```cpp | ||
#include <set> | ||
#include <iostream> | ||
|
||
int main() { | ||
std::set<int> mySet = {1, 2, 3, 4, 5}; | ||
mySet.insert(6); // Insert element | ||
|
||
for (int i : mySet) { | ||
std::cout << i << " "; | ||
} | ||
return 0; | ||
} | ||
2. **Map**: A collection of key-value pairs. | ||
```cpp | ||
#include <map> | ||
#include <iostream> | ||
|
||
int main() { | ||
std::map<int, std::string> myMap = {{1, "one"}, {2, "two"}, {3, "three"}}; | ||
myMap[4] = "four"; // Insert element | ||
|
||
for (const auto& pair : myMap) { | ||
std::cout << pair.first << ": " << pair.second << "\n"; | ||
} | ||
return 0; | ||
} | ||
### Container Containers | ||
|
||
1. **Stack**: A container adaptor that gives the functionality of a stack (LIFO). | ||
```cpp | ||
#include <stack> | ||
#include <iostream> | ||
|
||
int main() { | ||
std::stack<int> myStack; | ||
myStack.push(1); | ||
myStack.push(2); | ||
|
||
while (!myStack.empty()) { | ||
std::cout << myStack.top() << " "; | ||
myStack.pop(); | ||
} | ||
return 0; | ||
} | ||
2. **Queue**: A container adaptor that gives the functionality of a queue (FIFO). | ||
```cpp | ||
#include <queue> | ||
#include <iostream> | ||
|
||
int main() { | ||
std::queue<int> myQueue; | ||
myQueue.push(1); | ||
myQueue.push(2); | ||
|
||
while (!myQueue.empty()) { | ||
std::cout << myQueue.front() << " "; | ||
myQueue.pop(); | ||
} | ||
return 0; | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
sidebar_position: 4 | ||
title: "STL Iterators" | ||
description: "In this tutorial, we will learn about Iterators in the Standard Template Library (STL) in C++ with the help of examples. Iterators are used to point to the elements of containers." | ||
sidebar_label: "STL Iterators" | ||
slug: stl-iterators-in-cpp | ||
--- | ||
|
||
## STL Iterators | ||
|
||
### What are Iterators in STL? | ||
Iterators are objects used to iterate through the elements of a container. They act as pointers and provide a way to access elements in a container sequentially. | ||
|
||
### Types of Iterators | ||
1. **Input Iterator**: Reads elements from a sequence. | ||
2. **Output Iterator**: Writes elements to a sequence. | ||
3. **Forward Iterator**: Reads and writes elements sequentially. | ||
4. **Bidirectional Iterator**: Supports moving forward and backward through a sequence. | ||
5. **Random Access Iterator**: Provides random access to elements, supporting arithmetic operations like addition and subtraction. | ||
|
||
### Example: | ||
```cpp | ||
#include <iostream> | ||
#include <vector> | ||
|
||
int main() { | ||
std::vector<int> vec = {1, 2, 3, 4, 5}; | ||
|
||
// Forward iterator example | ||
std::cout << "Forward traversal using iterators: "; | ||
for (auto it = vec.begin(); it != vec.end(); ++it) { | ||
std::cout << *it << " "; | ||
} | ||
|
||
// Reverse iterator example | ||
std::cout << "\nReverse traversal using reverse iterators: "; | ||
for (auto rit = vec.rbegin(); rit != vec.rend(); ++rit) { | ||
std::cout << *rit << " "; | ||
} | ||
|
||
return 0; | ||
} |