Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STL in C++ #1257

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
292 changes: 292 additions & 0 deletions docs/languages/cpp/cp-13.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,292 @@
---
id: stl-in-cpp
sidebar_position: 13
title: "STL in C++"
sidebar_label: "STL In C++"
---

# STL in C++

In this guide, we will explore the **Standard Template Library (STL)** in C++, which is a powerful library that provides data structures and algorithms in an organized manner. STL helps in writing efficient and maintainable code by offering a wide range of functionalities.

---

## 1. Introduction to STL

STL in C++ is divided into three main components:

- **Containers**: Data structures to store objects.
- **Algorithms**: Procedures to process the data stored in containers.
- **Iterators**: Objects that point to elements in containers and allow navigation.

---

## 2. Containers

Containers are used to store collections of data. They are categorized into three types:

### 2.1 Sequence Containers

These containers store data in a linear sequence.

| Container | Description |
| --------- | ------------------ |
| `vector` | Dynamic array |
| `deque` | Double-ended queue |
| `list` | Doubly linked list |

#### Example:

```cpp
#include <iostream>
#include <vector>
using namespace std;

int main() {
vector<int> numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
cout << num << " ";
}
return 0;
}
```

#### Output:

```
1 2 3 4 5
```

---

### 2.2 Associative Containers

These containers store data in key-value pairs, providing fast search and retrieval.

| Container | Description |
| ---------- | ----------------------------- |
| `set` | Collection of unique elements |
| `map` | Key-value pairs |
| `multiset` | Allows duplicate values |
| `multimap` | Allows duplicate keys |

#### Example:

```cpp
#include <iostream>
#include <map>
using namespace std;

int main() {
map<int, string> students;
students[1] = "Alice";
students[2] = "Bob";

for (auto& student : students) {
cout << student.first << ": " << student.second << endl;
}
return 0;
}
```

#### Output:

```
1: Alice
2: Bob
```

---

### 2.3 Container Adaptors

These are wrappers around sequence containers and provide restricted interfaces.

| Container | Description |
| ---------------- | --------------------------- |
| `stack` | LIFO (Last In First Out) |
| `queue` | FIFO (First In First Out) |
| `priority_queue` | Elements sorted by priority |

#### Example (Stack):

```cpp
#include <iostream>
#include <stack>
using namespace std;

int main() {
stack<int> s;
s.push(10);
s.push(20);
s.push(30);

while (!s.empty()) {
cout << s.top() << " ";
s.pop();
}
return 0;
}
```

#### Output:

```
30 20 10
```

---

## 3. Iterators

Iterators are used to point to elements in containers. They provide a mechanism to traverse through elements.

### Types of Iterators:

- **Input Iterator**: Reads values in a sequence.
- **Output Iterator**: Writes values in a sequence.
- **Forward Iterator**: Can move in one direction (forward).
- **Bidirectional Iterator**: Can move both forward and backward.
- **Random Access Iterator**: Provides direct access to any element.

#### Example:

```cpp
#include <iostream>
#include <vector>
using namespace std;

int main() {
vector<int> vec = {10, 20, 30, 40};
vector<int>::iterator it;

for (it = vec.begin(); it != vec.end(); ++it) {
cout << *it << " ";
}
return 0;
}
```

#### Output:

```
10 20 30 40
```

---

## 4. Algorithms

STL provides a rich set of algorithms to work with data stored in containers. These algorithms work with iterators.

### 4.1 `sort()`

Sorts elements in a range.

#### Example:

```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
vector<int> vec = {4, 2, 5, 1, 3};
sort(vec.begin(), vec.end());

for (int num : vec) {
cout << num << " ";
}
return 0;
}
```

#### Output:

```
1 2 3 4 5
```

---

### 4.2 `find()`

Searches for a specific value in a range.

#### Example:

```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
vector<int> vec = {10, 20, 30, 40, 50};
auto it = find(vec.begin(), vec.end(), 30);

if (it != vec.end()) {
cout << "Element found: " << *it << endl;
} else {
cout << "Element not found" << endl;
}

return 0;
}
```

#### Output:

```
Element found: 30
```

---

### 4.3 `reverse()`

Reverses the order of elements in a range.

#### Example:

```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
vector<int> vec = {1, 2, 3, 4, 5};
reverse(vec.begin(), vec.end());

for (int num : vec) {
cout << num << " ";
}
return 0;
}
```

#### Output:

```
5 4 3 2 1
```

---

## 5. Commonly Used STL Algorithms

Here are some commonly used algorithms in STL:

- **`count()`**: Counts occurrences of an element.
- **`max_element()`**: Finds the largest element.
- **`min_element()`**: Finds the smallest element.
- **`accumulate()`**: Sums up all elements.
- **`binary_search()`**: Searches for an element using binary search.

---

## Conclusion

The STL in C++ provides a wide range of containers, iterators, and algorithms that help make your code more efficient and easier to maintain. By mastering STL, you can write cleaner and faster code. Happy coding!
Loading