C++ One-Liners

Post by ailswan Jan 16, 2024

C++ One-Liners

Date: January 16, 2024 Tags: c++ language

Tricky C++ One-Liners

Swap Two Variables:

int a = 5, b = 10; a ^= b ^= a ^= b;

Flatten a List of Lists:

std::vector<int> flat_list; for (const auto& sublist : list_of_lists) flat_list.insert(flat_list.end(), sublist.begin(), sublist.end());

Factorial Using Lambda:

auto factorial = [](int x) { return (x == 0) ? 1 : x * factorial(x-1); };

Find All Unique Values in a List:

std::vector<int> unique(my_list.begin(), std::unique(my_list.begin(), my_list.end()));

Calculate Fibonacci Sequence:

auto fib = [](int x) { return (x <= 1) ? x : fib(x-1) + fib(x-2); };

Transpose a Matrix:

std::vector<std::vector<int>> transposed(n, std::vector<int>(m)); for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) transposed[j][i] = matrix[i][j];

One-liner If-Else Assignment:

std::string value = (n % 2 == 0) ? "even" : "odd";

Multiple Assignments:

int a = 1, b = 2, c = 3;

Check If All Items in List Are Equal:

bool all_equal = std::all_of(my_list.begin(), my_list.end(), [&](int x) { return x == my_list[0]; });

Check If Any Item in Matrix is Zero:

bool zero_exists = std::any_of(matrix.begin(), matrix.end(), [](const std::vector<int>& row) { return std::any_of(row.begin(), row.end(), [](int x) { return x == 0; }); });

Check if Element is Present in Vector:

if (std::find(vec.begin(), vec.end(), x) != vec.end())

Check if Element is Present in Set::

if (my_set.find(x) != my_set.end())

Check if Element is Present in Map:

if (my_map.find(key) != my_map.end())

Count Occurrences of Element in Vector:

int count = std::count(vec.begin(), vec.end(), x)

Remove Duplicates from Vector:

vec.erase(std::unique(vec.begin(), vec.end()), vec.end())

Calculate Sum of Vector Elements:

int sum = std::accumulate(vec.begin(), vec.end(), 0)

Find Maximum Element in Vector:

int max_element = *std::max_element(vec.begin(), vec.end())

Sort Vector in Descending Order:

std::sort(vec.rbegin(), vec.rend())

Reverse a Vector:

std::reverse(vec.begin(), vec.end())

Rotate Vector to the Left by K Positions:

std::rotate(vec.begin(), vec.begin() + k, vec.end())

Check Palindrome String:

bool is_palindrome = std::equal(str.begin(), str.begin() + str.size() / 2, str.rbegin())

Find Intersection of Two Vectors:

std::vector<int> result; std::set_intersection(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), std::back_inserter(result))

example: