5 least known STL utilities in C++


Welcome to the next pikoTutorial!

Shared mutex

If you’re creating a multithreaded application with tons of read operations and only a few write operations, std::shared_mutex is the way to go. It allows threads to read from the same resource without locking it and at the same time prevents writing to it from more than one thread.

#include <iostream>
#include <mutex>
#include <shared_mutex>

std::shared_mutex mutex;
int value = 0;

void Read()
{
    // This line prevents other threads from writing to "value", but allows
    // other threads to read from it at the same time
    std::shared_lock<std::shared_mutex> lock(mutex);
    std::cout << "value = " << value << std::endl;
}

void Write()
{
    // This line locks the access to "value", so that no other thread can
    // read or write to it
    std::unique_lock<std::shared_mutex> lock(mutex);
    value = 12;
}

Apply

This is a C++ way to perform arguments unpacking. Thanks to unpacking, you can provide multiple function input arguments having only one variable. In Python we have * and in C++ we have std::apply:

#include <iostream>
#include <tuple>

void Function(int a, int b, int c)
{
    std::cout << a << " " << b << " " << c << std::endl;
}

int main()
{
    const auto args = std::make_tuple(12, 24, 36);
    std::apply(Function, args);
}

Output:

12 24 36

Clamp

std::clamp helps to reduce the amount of boilerplate code because instead of writing such function:

#include <iostream>

static constexpr unsigned int kMinValue = 12U;
static constexpr unsigned int kMaxValue = 24U;

unsigned int GetInRangeValue(unsigned int value)
{
    if (value < kMinValue) {
        return kMinValue;
    }
    else if (value > kMaxValue) {
        return kMaxValue;
    }
    else {
        return value;
    }
}

int main(int argc, char** argv)
{
    std::cout << GetInRangeValue(8U) << std::endl;
    std::cout << GetInRangeValue(18U) << std::endl;
    std::cout << GetInRangeValue(28U) << std::endl;
}

You can just use std::clamp:

#include <iostream>
#include <algorithm>

static constexpr unsigned int kMinValue = 12U;
static constexpr unsigned int kMaxValue = 24U;

int main(int argc, char** argv)
{
    std::cout << std::clamp(8U, kMinValue, kMaxValue) << std::endl;
    std::cout << std::clamp(18U, kMinValue, kMaxValue) << std::endl;
    std::cout << std::clamp(28U, kMinValue, kMaxValue) << std::endl;
}

Min/Max element

If you need to get both minimum and maximum element from some container, you can do this with a single std::minmax_element function:

#include <algorithm>
 
int main(int argc, char** argv)
{
    const auto elements = {6, 8, 3, 5, 1, 7, 9};
    const auto [min_element, max_element] = std::minmax_element(elements.begin(), elements.end());
}

For each, but limited to a number of elements

To execute some function only on some number of elements, use std::for_each_n:

#include <algorithm>
#include <iostream>
#include <vector>

int main(int argc, char** argv)
{
    std::vector<int> elements {6, 8, 3, 5, 4, 7, 9};
    std::for_each_n(elements.begin(), 4U, [](int &element) { element = -1; });
}

The modified vector is:

-1 -1 -1 -1 4 7 9