# stddef

***

**1. Null Pointer**

```cpp
void* ptr = nullptr;
```

**Explanation:** Sets `ptr` to a null pointer, indicating it does not point to any valid memory location.

**2. Size of Data Type**

```cpp
size_t size = sizeof(int);
```

**Explanation:** Gets the size of the `int` data type in bytes.

**3. Alignment Requirements**

```cpp
ptrdiff_t align = alignof(double);
```

**Explanation:** Gets the alignment requirements for the `double` data type, which indicates the minimum byte boundary on which it can be stored.

**4. Pointer Arithmetic**

```cpp
int* ptr = new int[10];
ptr += 5;
*ptr = 10;  // Assigns 10 to the 6th element
```

**Explanation:** Pointer arithmetic allows you to increment or decrement a pointer to access different elements in an array.

**5. Pointer to Function**

```cpp
int sum(int a, int b) { return a + b; }
int (*ptr_to_sum)(int, int) = &sum;
int result = ptr_to_sum(3, 5);  // Calls the `sum` function through the pointer
```

**Explanation:** A pointer to a function points to the memory location where the function code is stored.

**6. Pointer to Member Function**

```cpp
class A {
public:
    int add(int a, int b) { return a + b; }
};

typedef int (A::*ptr_to_member)(int, int);

void func(A* obj, ptr_to_member add) {
    obj->add(3, 5);  // Calls the `add` member function through the pointer
}
```

**Explanation:** A pointer to a member function points to a specific member function of a class.

**7. Type Trait: is\_same**

```cpp
static_assert(std::is_same<int, int>::value);
```

**Explanation:** Checks if two types are the same. In this example, `int` and `int` are the same type.

**8. Type Trait: is\_integral**

```cpp
static_assert(std::is_integral<int>::value);
```

**Explanation:** Checks if a type is an integral type, such as `int`, `long`, or `short`.

**9. Type Trait: is\_floating\_point**

```cpp
static_assert(std::is_floating_point<float>::value);
```

**Explanation:** Checks if a type is a floating-point type, such as `float` or `double`.

**10. Type Trait: is\_array**

```cpp
static_assert(std::is_array<int[]>::value);
```

**Explanation:** Checks if a type is an array type.

**11. Type Trait: is\_pointer**

```cpp
static_assert(std::is_pointer<int*>::value);
```

**Explanation:** Checks if a type is a pointer type.

**12. Type Trait: is\_reference**

```cpp
static_assert(std::is_reference<int&>::value);
```

**Explanation:** Checks if a type is a reference type.

**13. Type Trait: remove\_reference**

```cpp
static_assert(std::is_same<std::remove_reference<int&>::type, int>::value);
```

**Explanation:** Removes the reference from a type. In this example, it gets the type `int` from the reference type `int&`.

**14. Type Trait: remove\_pointer**

```cpp
static_assert(std::is_same<std::remove_pointer<int*>::type, int>::value);
```

**Explanation:** Removes the pointer from a type. In this example, it gets the type `int` from the pointer type `int*`.

**15. Type Trait: remove\_const**

```cpp
static_assert(std::is_same<std::remove_const<const int>::type, int>::value);
```

**Explanation:** Removes the `const` qualifier from a type. In this example, it gets the type `int` from the constant type `const int`.

**16. Type Trait: remove\_volatile**

```cpp
static_assert(std::is_same<std::remove_volatile<volatile int>::type, int>::value);
```

**Explanation:** Removes the `volatile` qualifier from a type. In this example, it gets the type `int` from the volatile type `volatile int`.

**17. Type Trait: decay**

```cpp
static_assert(std::is_same<std::decay<int[10]>::type, int*>::value);
```

**Explanation:** Gets the decayed type of a type. In this example, it gets the pointer type `int*` from the array type `int[10]`.

**18. Zero Initialization**

```cpp
int arr[10] = {};
```

**Explanation:** Initializes all elements of an array to zero.

**19. Constant Initialization**

```cpp
const int x = 10;
```

**Explanation:** Initializes a variable with a constant value.

**20. Template Metaprogramming**

```cpp
template<int> struct pow : std::integral_constant<int, 2> { };
template<int n> struct pow<n + 1> : std::integral_constant<int, 2 * pow<n>::value> { };

template<int n> static const int value = pow<n>::value;
```

**Explanation:** Computes the value of 2n using template metaprogramming.

**21. Fixed-size Buffer**

```cpp
std::array<int, 10> arr;
```

**Explanation:** Creates a fixed-size array with a specified number of elements.

**22. Dynamic Array**

```cpp
std::vector<int> vec;
vec.push_back(10);
```

**Explanation:** Creates a dynamic array that can grow and shrink as needed.

**23. Linked List**

```cpp
struct Node {
    int data;
    Node* next;
};
```

**Explanation:** Implements a linked list data structure.

**24. Binary Tree**

```cpp
struct Node {
    int data;
    Node* left;
    Node* right;
};
```

**Explanation:** Implements a binary tree data structure.

**25. Hash Table**

```cpp
std::unordered_map<int, std::string> map;
map.insert({1, "One"});
```

**Explanation:** Implements a hash table data structure for storing key-value pairs.

**26. Bit Manipulation**

```cpp
int x = 10;
int bit_set = x | (1 << 2);  // Sets the 3rd bit
int bit_cleared = x & ~(1 << 2);  // Clears the 3rd bit
```

**Explanation:** Performs bitwise operations on integers.

**27. Thread-safe Data Structure**

```cpp
std::atomic<int> x;
x++;  // Increments the value of x atomically
```

**Explanation:** Creates a thread-safe data structure using the `std::atomic` class.

**28. Multi-Threading**

```cpp
std::thread t1([]() { ... });
```

**Explanation:** Creates a new thread that executes a specified function.

**29. Deadlock Prevention**

```cpp
std::mutex m1;
std::lock_guard<std::mutex> lock(m1);
```

**Explanation:** Prevents deadlocks by using a lock and a lock guard.

**30. Memory Management**

```cpp
auto* ptr = new int;
delete ptr;
```

**Explanation:** Manually manages memory allocation and deallocation.

**31. File Handling**

```cpp
std::ifstream file("myfile.txt");
std::ofstream output("myfile.out");
```

**Explanation:** Opens files for reading and writing.

**32. Input/Output Streams**

```cpp
std::cin >> input;
std::cout << output;
```

**Explanation:** Reads input from and writes output to the standard input/output streams.

**33. Regular Expressions**

```cpp
std::regex r("pattern");
std::smatch matches;
std::regex_search("input", matches, r);
```

**Explanation:** Matches input strings against a regular expression.

**34. String Manipulation**

```cpp
std::string str = "Hello";
str.replace(0, 1, "J");  // Replaces "H" with "J"
```

**Explanation:** Manipulates strings using the `std::string` class.

**35. Date and Time**

```cpp
time_t now = std::time(nullptr);
```

**Explanation:** Gets the current time as a timestamp.

**36. Localization**

```cpp
std::setlocale(LC_ALL, "fr_FR");
```

**Explanation:** Sets the locale for localized formatting and parsing.

**37. Math Functions**

```cpp
double pi = std::acos(-1);
```

**Explanation:** Provides access to mathematical functions like trigonometric functions and constants.

**38. Random Number Generation**

```cpp
std::uniform_int_distribution<int> dist(1, 10);
auto random = dist(generator);
```

**Explanation:** Generates pseudo-random numbers using the standard random number generator.

**39. Sorting**

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

**Explanation:** Sorts a container using the standard sort algorithm.

**40. Searching**

```cpp
std::vector<int> vec = {1, 3, 5, 2, 4};
auto it = std::find(vec.begin(), vec.end(), 3);
```

**Explanation:** Searches for an element in a container using a linear search.

**41. Algorithm Execution**

```cpp
std::for_each(vec.begin(), vec.end(), [](int& x) { x *= 2; });
```

**Explanation:** Executes a function for each element in a container using the standard for\_each algorithm.

**42. Iterators**

```cpp
std::vector<int> vec = {1, 3, 5, 2, 4};
for (auto it = vec.begin(); it != vec.end(); ++it) {
    *it *= 2;
}
```

**Explanation:** Iterates over a container using iterators.

**43. Function Objects**

```cpp
struct Add {
    int operator()(int a, int b) { return a + b; }
};
```

**Explanation:** Creates a function object that can be treated as a function.

**44. Lambda Expressions**

```cpp
auto add = [](int a, int b) { return a + b; };
```

**Explanation:** Creates a lambda expression that represents a function.

**45. Exception Handling**

```cpp
try {
    throw std::runtime_error("Error");
} catch (const std::exception& e) {
    std::cout << e.what();
}
```

**Explanation:** Catches and handles runtime errors.

**46. Assertion**

```cpp
assert(x > 0);
```

**Explanation:** Checks a condition and terminates the program if the condition is false.

**47. Preprocessor Macros**

```cpp
#define MAX_SIZE 10
```

**Explanation:** Defines a preprocessor macro that can be used in code.

**48. Conditional Compilation**

```cpp
#ifdef DEBUG
    std::cout << "Debug mode";
#endif
```

**Explanation:** Selectively compiles code based on preprocessor conditions.

**49. Inline Assembly**

```cpp
asm("mov %%eax, %%ebx");
```

**Explanation:** Inserts inline assembly code into the C++ code.

**50. Platform-Specific Code**

```cpp
#ifdef _WIN32
    // Windows-specific code
#else
    // Platform-independent code
#endif
```

**Explanation:** Writes platform-specific code using preprocessor conditionals.
