> For the complete documentation index, see [llms.txt](https://42-guide.gitbook.io/42-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://42-guide.gitbook.io/42-guide/all-about-c++/typedef-using-std-vector-iterators-and-std-pair.md).

# typedef, using, std::vector, Iterators, and std::pair

1\. typedef and using (Modern Alternative)

#### **What is `typedef`?**

`typedef` allows you to create a new name (alias) for an existing type to make the code cleaner and easier to read.

**Example:**

```cpp
typedef unsigned int uint;
uint age = 25; // Equivalent to: unsigned int age = 25;
```

#### **Modern Alternative: `using`**

C++11 introduced `using`, which does the same thing but is more readable.

**Example:**

```cpp
using uint = unsigned int;
uint age = 25;
```

#### **Example with Vectors:**

```cpp
typedef std::vector<int> IntVector; // Old way
using IntVector = std::vector<int>; // Modern way
```

Both are valid, but `using` is preferred in modern C++.

***

### 2. std::vector and Iterators

A `std::vector` is a **dynamic array** that can grow and shrink in size automatically.

#### **Basic Usage:**

```cpp
#include <vector>
#include <iostream>

int main() {
    std::vector<int> numbers; // Creates an empty vector
    numbers.push_back(10); // Adds 10 to the vector
    numbers.push_back(20); // Adds 20
    
    std::cout << numbers[0] << " " << numbers[1] << std::endl; // Output: 10 20
}
```

#### **How Vectors Grow Dynamically?**

Each time the vector reaches its capacity, it **doubles** in size to accommodate more elements efficiently.

**Example:**

```cpp
std::vector<int> v;
for (int i = 0; i < 10; i++) {
    v.push_back(i);
    std::cout << "Size: " << v.size() << ", Capacity: " << v.capacity() << std::endl;
}
```

You'll notice the capacity grows dynamically as elements are added.

***

### 3. Iterators in std::vector

Iterators are used to traverse elements in a container like `std::vector`.

#### **Using `.begin()` and `.end()`**

```cpp
std::vector<int> numbers = {10, 20, 30};
for (std::vector<int>::iterator it = numbers.begin(); it != numbers.end(); ++it) {
    std::cout << *it << " "; // Output: 10 20 30
}
```

* `.begin()` points to the first element.
* `.end()` points **past the last element** (so we stop **before** reaching `.end()`).
* `*it` is used to access the value at the iterator's position.

#### **Modern C++ Range-Based Loop (Preferred)**

```cpp
for (int num : numbers) {
    std::cout << num << " "; // Output: 10 20 30
}
```

***

### 4. std::pair

`std::pair` is a container that holds **two values of different types**.

#### **Creating and Accessing a Pair**

```cpp
#include <iostream>
#include <utility> // For std::pair

int main() {
    std::pair<int, std::string> student(42, "Muhammad");
    std::cout << "ID: " << student.first << ", Name: " << student.second << std::endl;
    // Output: ID: 42, Name: Muhammad
}
```

* `.first` → Accesses the first element.
* `.second` → Accesses the second element.

#### **Using `std::pair` with Iterators**

```cpp
using accounts_t = std::vector<int>;
using ints_t = std::vector<int>;
using acc_int_t = std::pair<accounts_t::iterator, ints_t::iterator>;
```

This defines a pair where:

* `accounts_t::iterator` → Iterator for a vector of accounts.
* `ints_t::iterator` → Iterator for a vector of integers.

Example:

```cpp
accounts_t accounts = {1000, 2000};
ints_t numbers = {1, 2};
acc_int_t pair(accounts.begin(), numbers.begin());
std::cout << *pair.first << " " << *pair.second << std::endl; // Output: 1000 1
```

#### **Dereferencing Iterators**

* `*iterator` → Access the value.
* `iterator->` → Access members if it's a pointer.

**Example:**

```cpp
std::vector<std::pair<int, std::string>> students = {{1, "Ali"}, {2, "Sara"}};
for (auto it = students.begin(); it != students.end(); ++it) {
    std::cout << it->first << " - " << it->second << std::endl;
    // Output: 1 - Ali
    //         2 - Sara
}
```

* `it->first` is the same as `(*it).first`.
* `it->second` is the same as `(*it).second`.

***

### **Summary**

| Concept             | Explanation                                           |
| ------------------- | ----------------------------------------------------- |
| `typedef`           | Creates an alias for a type (old way)                 |
| `using`             | Modern alternative to `typedef`                       |
| `std::vector`       | Dynamic array that grows automatically                |
| `.push_back(value)` | Adds a value at the end of a vector                   |
| `.begin()/.end()`   | Iterators for accessing vector elements               |
| `std::pair<T1, T2>` | Stores two values together                            |
| `.first/.second`    | Accesses elements in a `std::pair`                    |
| `*iterator`         | Dereferences iterator to get value                    |
| `iterator->`        | Accesses members in an iterator pointing to an object |

Now you have a clear understanding of these important C++ concepts. 🚀


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://42-guide.gitbook.io/42-guide/all-about-c++/typedef-using-std-vector-iterators-and-std-pair.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
