DEV Community

Cover image for 5 least known STL utilities in C++
pikoTutorial
pikoTutorial

Posted on • Originally published at pikotutorial.com

5 least known STL utilities in C++

Welcome to the next pikoTutorial !

Shared mutex

If you're creating a multi-threaded 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;
}
Enter fullscreen mode Exit fullscreen mode

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);
}
Enter fullscreen mode Exit fullscreen mode

Output:

12 24 36
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

Min/Max element

If you need to get both minimum and maximum element from some container, you can do this with a single 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());
}
Enter fullscreen mode Exit fullscreen mode

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; });
}
Enter fullscreen mode Exit fullscreen mode

The modified vector is:

-1 -1 -1 -1 4 7 9 
Enter fullscreen mode Exit fullscreen mode

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more