Skip to content

Commit

Permalink
Day -14 : C++ Standard Template Library (STL) (#375)
Browse files Browse the repository at this point in the history
Issue No. #270
  • Loading branch information
Samrudhi00 authored Jun 17, 2024
1 parent 839ab74 commit d514e2f
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/day-14/category_.json
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 added docs/day-14/stl-algorithms.md
Empty file.
122 changes: 122 additions & 0 deletions docs/day-14/stl-containers.md
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 added docs/day-14/stl-introduction.md
Empty file.
42 changes: 42 additions & 0 deletions docs/day-14/stl-iterators.md
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;
}

0 comments on commit d514e2f

Please sign in to comment.