diff --git a/docs/day-14/category_.json b/docs/day-14/category_.json new file mode 100644 index 00000000..17177565 --- /dev/null +++ b/docs/day-14/category_.json @@ -0,0 +1,8 @@ +{ + "label": "Day 14", + "position": 15, + "link": { + "type": "generated-index" + } + } + \ No newline at end of file diff --git a/docs/day-14/stl-algorithms.md b/docs/day-14/stl-algorithms.md new file mode 100644 index 00000000..e69de29b diff --git a/docs/day-14/stl-containers.md b/docs/day-14/stl-containers.md new file mode 100644 index 00000000..3f74069e --- /dev/null +++ b/docs/day-14/stl-containers.md @@ -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 + #include + + int main() { + std::vector 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 + #include + + int main() { + std::list 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 + #include + + int main() { + std::deque 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 + #include + + int main() { + std::set 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 + #include + + int main() { + std::map 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 + #include + + int main() { + std::stack 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 + #include + + int main() { + std::queue myQueue; + myQueue.push(1); + myQueue.push(2); + + while (!myQueue.empty()) { + std::cout << myQueue.front() << " "; + myQueue.pop(); + } + return 0; + } diff --git a/docs/day-14/stl-introduction.md b/docs/day-14/stl-introduction.md new file mode 100644 index 00000000..e69de29b diff --git a/docs/day-14/stl-iterators.md b/docs/day-14/stl-iterators.md new file mode 100644 index 00000000..06f8ea9f --- /dev/null +++ b/docs/day-14/stl-iterators.md @@ -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 +#include + +int main() { + std::vector 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; +}