Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

/* std::clamp

If num > high, num is assigned high.


If num < low, num is assigned low.
If num is already clamped, no modifications.
*/
#include <bits/stdc++.h>

int main()
{
int high = 100, low = 10;
int num1 = 120;
int num2 = 5;
int num3 = 50;
num1 = std::clamp(num1, low, high); // 100
num2 = std::clamp(num2, low, high); // 10
num3 = std::clamp(num3, low, high); // 50

std::cout << num1 << " " << num2 << " " << num3;
}

-----------------------------------------------------------------------------
// std::any

#include <iostream>
#include <unordered_map>
#include <string>
#include <any>

int main() {
// Create an unordered_map with string keys and std::any values
std::unordered_map<std::string, std::any> myMap;

// Insert some key-value pairs


myMap["apple"] = 42;
myMap["banana"] = 3.14;
myMap["orange"] = "Hello, world!";

// Access values using keys


try {
std::cout << "Value for 'apple': " << std::any_cast<int>(myMap["apple"]) <<
std::endl;
std::cout << "Value for 'banana': " <<
std::any_cast<double>(myMap["banana"]) << std::endl;
std::cout << "Value for 'orange': " <<
std::any_cast<std::string_view>(myMap["orange"]) << std::endl;
} catch (const std::bad_any_cast& e) {
std::cerr << "Error: " << e.what() << std::endl;
}

return 0;
}

You might also like