Cpp Libs Utility
In the C++ standard library, the `` header file contains some useful utility classes and functions, which are very useful when writing efficient and readable code.
The core value of the `utility` library lies in:
* Provides basic data structures and utility functions
* Simplifies implementation of common programming tasks
* Provides foundational support for other standard library components
Although the `utility` header file is small, the tools it provides are very useful:
| Component/Function | Purpose | Use Case |
| --- | --- | --- |
| `std::pair` | Stores two related values | Function returning multiple values, map elements |
| `std::make_pair` | Conveniently create pair | Template type deduction, code simplification |
| `std::swap` | Swap two values | Algorithm implementation, sorting operations |
| `std::move` | Enable move semantics | Resource management, performance optimization |
| `std::forward` | Perfect forwarding | Universal references, template programming |
* * *
## Core Components Explained
### std::pair: Key-Value Container
`std::pair` is the most commonly used component in the `utility` library, used to combine two values into a single object.
#### Basic Syntax
#include // Basic way to create pair objects std::pair variableName(value1, value2);
## Example
#include
#include
#include
int main(){
// Method 1: Direct initialization
std::pair student1(101, "Alice");
// Method 2: Using make_pair function (recommended)
auto student2 = std::make_pair(102, "Bob");
// Method 3: Deduction guides supported since C++17
std::pair student3(103, "Charlie");
// Access pair members
std::cout<<"Student ID: "<< student1.first<<", Name: "<< student1.second<< std::endl;
return 0;
}
#### Common Operations on pair
## Example
#include
#include
void pairOperations(){
// Create pairs
std::pair
YouTip