> For the complete documentation index, see [llms.txt](https://alham-rizvi.gitbook.io/alhamrizvi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://alham-rizvi.gitbook.io/alhamrizvi/c++-notes/04.-typedef.md).

# 04. Typedef

`typedef` lets you give an **alias** (a new, custom name) to an existing type. It doesn't create a new type, it's just a nickname that makes code easier to read and write.

```cpp
#include <iostream>

typedef std::string text_t;

int main() {
    text_t firstName = "Bro";

    std::cout << firstName << '\n';

    return 0;
}
```

Here `text_t` is just another name for `std::string`. `text_t firstName = "Bro";` behaves exactly like `std::string firstName = "Bro";`.

**Syntax:** `typedef ExistingType NewName;`

***

### Why use `typedef`?

* **Shorter, clearer names** — especially for long or ugly types (looking at you, `std::vector<std::pair<std::string, int>>`).
* **Easier to change later** — update the type in one place instead of everywhere it's used.
* **Self-documenting code** — `text_t` or `age_t` tells you what the value *represents*, not just its raw type.

```cpp
// without typedef — long and easy to typo
std::vector<std::pair<std::string, int>> scores;

// with typedef — short and clear
typedef std::vector<std::pair<std::string, int>> scoreList_t;
scoreList_t scores;
```

***

### More cool examples

#### 1. Aliasing numeric types

```cpp
#include <iostream>

typedef unsigned long ulong_t;
typedef double money_t;

int main() {
    ulong_t population = 8000000000;
    money_t price = 19.99;

    std::cout << "Population: " << population << '\n';
    std::cout << "Price: $" << price << '\n';

    return 0;
}
```

#### 2. Aliasing a `std::vector`

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

typedef std::vector<int> intList_t;

int main() {
    intList_t scores = {90, 85, 77, 100};

    for (int score : scores) {
        std::cout << score << ' ';
    }
    std::cout << '\n';

    return 0;
}
```

#### 3. Aliasing a `std::pair` (name + age)

```cpp
#include <iostream>
#include <utility>
#include <string>

typedef std::pair<std::string, int> person_t;

int main() {
    person_t p1 = {"Ronaldo", 39};

    std::cout << p1.first << " is " << p1.second << " years old\n";

    return 0;
}
```

#### 4. The commented-out example from your screenshot, explained

```cpp
#include <vector>
#include <string>
#include <utility>

// A list of (name, id) pairs
typedef std::vector<std::pair<std::string, int>> nameIdList_t;

int main() {
    nameIdList_t users = {
        {"Alice", 1},
        {"Bob", 2}
    };

    for (auto& user : users) {
        std::cout << user.first << " -> ID " << user.second << '\n';
    }

    return 0;
}
```

Without `typedef`, you'd have to write `std::vector<std::pair<std::string, int>>` **every single time** you declare something of this type. One alias, used everywhere.

#### 5. `typedef` for function pointers (more advanced, good to recognize)

```cpp
#include <iostream>

typedef int (*operation_t)(int, int);

int add(int a, int b) { return a + b; }
int multiply(int a, int b) { return a * b; }

int main() {
    operation_t op = add;
    std::cout << "Add: " << op(3, 4) << '\n';

    op = multiply;
    std::cout << "Multiply: " << op(3, 4) << '\n';

    return 0;
}
```

***

### `typedef` vs `using` (modern alternative)

Since C++11, `using` can do the same job — and most modern code prefers it because it reads left-to-right and handles templates more cleanly.

```cpp
typedef std::string text_t;   // old style
using text_t = std::string;   // modern style, same result
```

```cpp
typedef std::vector<int> intList_t;   // old style
using intList_t = std::vector<int>;   // modern style
```

Both work. `typedef` is older (from C) and still very common in existing codebases; `using` is the recommended style for new code.

***

### Quick Recap

* `typedef ExistingType NewName;` creates an alias, not a new type.
* Great for shortening long/complex types like `std::vector<std::pair<...>>`.
* Makes code more readable and easier to maintain.
* Modern C++ (11+) prefers `using NewName = ExistingType;` for the same purpose.


---

# 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, and the optional `goal` query parameter:

```
GET https://alham-rizvi.gitbook.io/alhamrizvi/c++-notes/04.-typedef.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
