# stdlib

***

**1. Random Number Generation**

```cpp
#include <random>

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> dist(1, 6);

for (int i = 0; i < 10; i++)
  std::cout << dist(gen) << " ";
```

**2. Vector Manipulation**

```cpp
#include <vector>

std::vector<int> v = {1, 2, 3, 4, 5};
v.push_back(6);
v.pop_back();
v.insert(v.begin() + 2, 7);
v.erase(v.begin() + 3);
```

**3. String Manipulation**

```cpp
#include <string>

std::string s = "Hello World";
s.append("!");
s.replace(6, 5, "Universe");
s.find("World");
s.substr(0, 5); // "Hello"
```

**4. File Reading and Writing**

```cpp
#include <fstream>

std::ifstream ifs("input.txt");
std::ofstream ofs("output.txt");
ofs << ifs.rdbuf();
```

**5. Regex Matching**

```cpp
#include <regex>

std::regex re("^[a-z0-9]+$");
std::string s = "abc123";
std::cout << std::regex_match(s, re) << std::endl;
```

**6. Date and Time Manipulation**

```cpp
#include <ctime>

std::time_t t = std::time(nullptr);
std::tm* tm = std::localtime(&t);

std::cout << tm->tm_year + 1900 << "-"
          << tm->tm_mon + 1 << "-"
          << tm->tm_mday << std::endl;
```

**7. Mathematical Functions**

```cpp
#include <cmath>

std::cout << std::sqrt(4) << std::endl;
std::cout << std::pow(2, 3) << std::endl;
std::cout << std::log10(100) << std::endl;
```

**8. Input and Output**

```cpp
#include <iostream>

std::cout << "Enter a number: ";
int num;
std::cin >> num;

std::cout << "The number you entered is: " << num << std::endl;
```

**9. Containers (Lists)**

```cpp
#include <list>

std::list<int> l = {1, 2, 3, 4, 5};
l.push_back(6);
l.pop_back();
l.insert(l.begin(), 0);
```

**10. Containers (Sets)**

```cpp
#include <set>

std::set<std::string> s = {"apple", "banana", "cherry"};
s.insert("dog");
s.erase("apple");
```

**11. Containers (Maps)**

```cpp
#include <map>

std::map<std::string, int> m = {{"apple", 1}, {"banana", 2}, {"cherry", 3}};
m["dog"] = 4;
m.erase("apple");
```

**12. Algorithms (Sorting)**

```cpp
#include <algorithm>

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

**13. Algorithms (Searching)**

```cpp
#include <algorithm>

std::vector<int> v = {1, 5, 2, 4, 3};
std::cout << std::binary_search(v.begin(), v.end(), 3) << std::endl;
```

**14. Algorithms (Counting)**

```cpp
#include <algorithm>

std::vector<int> v = {1, 2, 2, 3, 4, 5, 6, 6, 6};
std::cout << std::count(v.begin(), v.end(), 6) << std::endl;
```

**15. Algorithms (Min and Max)**

```cpp
#include <algorithm>

std::vector<int> v = {1, 5, 2, 4, 3};
std::cout << std::min_element(v.begin(), v.end()) << std::endl;
std::cout << std::max_element(v.begin(), v.end()) << std::endl;
```

**16. Algorithms (Accumulate)**

```cpp
#include <algorithm>

std::vector<int> v = {1, 2, 3, 4, 5};
std::cout << std::accumulate(v.begin(), v.end(), 0) << std::endl;
```

**17. Iterators**

```cpp
#include <iterator>

std::vector<int> v = {1, 2, 3, 4, 5};
std::copy(v.begin(), v.end(),
         std::ostream_iterator<int>(std::cout, " "));
```

**18. Function Objects**

```cpp
#include <functional>

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

**19. Lambdas**

```cpp
#include <vector>

std::vector<int> v = {1, 2, 3, 4, 5};
std::sort(v.begin(), v.end(), [](int a, int b) { return a > b; });
```

**20. Exceptions**

```cpp
#include <exception>

try {
  // Do something that may throw an exception
} catch (std::exception& e) {
  // Handle the exception
}
```

**21. Threads**

```cpp
#include <thread>

std::thread t([] {
  // Do something in a separate thread
});
t.join();
```

**22. Mutexes**

```cpp
#include <mutex>

std::mutex m;

void func() {
  std::lock_guard<std::mutex> lock(m);
  // Do something that needs to be synchronized
}
```

**23. Condition Variables**

```cpp
#include <condition_variable>

std::condition_variable cv;
std::mutex m;

void func() {
  std::unique_lock<std::mutex> lock(m);
  cv.wait(lock);
  // Do something that depends on a condition being met
}
```

**24. Filesystem**

```cpp
#include <filesystem>

namespace fs = std::filesystem;

fs::create_directory("my_directory");
fs::remove_all("my_directory");
```

**25. Random Access Iterators**

```cpp
#include <iterator>

std::vector<int> v = {1, 2, 3, 4, 5};
std::random_access_iterator<int> it = v.begin();
it += 2;
```

**26. Reverse Iterators**

```cpp
#include <iterator>

std::vector<int> v = {1, 2, 3, 4, 5};
std::reverse_iterator<std::vector<int>::iterator> it = v.rbegin();
```

**27. Input Iterators**

```cpp
#include <iterator>

std::ifstream ifs("input.txt");
std::istream_iterator<int> it(ifs);
```

**28. Output Iterators**

```cpp
#include <iterator>

std::ofstream ofs("output.txt");
std::ostream_iterator<int> it(ofs, "\n");
```

**29. Function Templates**

```cpp
#include <functional>

template <typename T>
T max(T a, T b) {
  return a > b ? a : b;
}
```

**30. Class Templates**

```cpp
#include <iostream>

template <typename T>
class Stack {
  T data[100];
  int top;
public:
  Stack() : top(-1) {}
  void push(T value) { data[++top] = value; }
  T pop() { return data[top--]; }
  bool empty() { return top == -1; }
};
```

**31. Smart Pointers (unique\_ptr)**

```cpp
#include <memory>

std::unique_ptr<int> ptr(new int(5));
std::cout << *ptr << std::endl; // 5
```

**32. Smart Pointers (shared\_ptr)**

```cpp
#include <memory>

std::shared_ptr<int> ptr = std::make_shared<int>(5);
std::cout << *ptr << std::endl; // 5
```

**33. Smart Pointers (weak\_ptr)**

```cpp
#include <memory>

std::weak_ptr<int> wptr;
{
  std::shared_ptr<int> ptr(new int(5));
  wptr = ptr;
}
std::cout << *wptr.lock() << std::endl; // 5
```

**34. Bit Manipulation**

```cpp
#include <bitset>

std::bitset<8> b1(0b11001011);
std::bitset<8> b2(0b10110100);

std::cout << (b1 & b2) << std::endl; // 0b10000000
std::cout << (b1 | b2) << std::endl; // 0b11111111
```

**35. Containers (Unordered Map)**

```cpp
#include <unordered_map>

std::unordered_map<std::string, int> m = {{"apple", 1}, {"banana", 2}};
m["cherry"] = 3;
std::cout << m["apple"] << std::endl; // 1
```

**36. Containers (Unordered Set)**

```cpp
#include <unordered_set>

std::unordered_set<std::string> s = {"apple", "banana"};
s.insert("cherry");
std::cout << (s.count("apple") > 0) << std::endl; // true
```

**37. Containers (Deque)**

```cpp
#include <deque>

std::deque<int> d = {1, 2, 3, 4, 5};
d.push_front(0);
d.push_back(6);
```

**38. Containers (Stack)**

```cpp
#include <stack>

std::stack<int> s;
s.push(1);
s.push(2);
s.push(3);
std::cout << s.top() << std::endl; // 3
```

**39. Containers (Queue)**

```cpp
#include <queue>

std::queue<int> q;
q.push(1);
q.push(2);
q.push(3);
std::cout << q.front() << std::endl; // 1
```

**40. Algorithms (Permutation)**

```cpp
#include <algorithm>

std::vector<int> v = {1, 2, 3, 4};
std::next_permutation(v.begin(), v.end());
```

**41. Algorithms (Combination)**

```cpp
#include <algorithm>

std::vector<int> v = {1, 2, 3, 4};
std::vector<std::vector<int>> combinations;
std::copy_if(v.begin(), v.end(),
             std::back_inserter(combinations),
             [](int i) { return i % 2 == 1; });
```

**42. Algorithms (Heap)**

```cpp
#include <algorithm>

std::vector<int> v = {1, 2, 3, 4, 5};
std::make_heap(v.begin(), v.end());
std::cout << v.front() << std::endl; // 1
```

**43. Algorithms (Sort)**

```cpp
#include <algorithm>

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

**44. Algorithms (Merge)**

```cpp
#include <algorithm>

std::vector<int> v1 = {1, 2, 3};
std::vector<int> v2 = {4, 5, 6};
std::vector<int> v3;
std::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(v3));
```

**45. Algorithms (Set Operations)**

```cpp
#include <algorithm>

std::set<int> s1 = {1, 2, 3};
std::set<int> s2 = {3, 4, 5};
std::set<int> s3;
std::set_union(s1.begin(), s1.end(), s2.begin(), s2.end(), std::inserter(s3, s3.begin()));
```

**46. Algorithms (Numeric Operations)**

```cpp
#include <numeric>

std::vector<int> v = {1, 2, 3, 4, 5};
std::cout << std::accumulate(v.begin(), v.end(), 0) << std::endl; // 15
```

**47. Type Traits**

```cpp
#include <type_traits>

static_assert(std::is_integral<int>::value, "int is not an integral type");
```

**48. Macros**

```cpp
#define MAX(a, b) ((a) > (b) ? (a) : (b))
std::cout << MAX(5, 10) << std::endl; // 10
```

**49. Inline Functions**

```cpp
inline int max(int a, int b) {
  return a > b ? a : b;
}
std::cout << max(5, 10) << std::endl; // 10
```

**50. Error Handling (Assert)**

```cpp
#include <cassert>

int main() {
  int x = 10;
  assert(x == 10); // No error
  x = 15;
  assert(x == 10); // Assertion error
}
```
