home.social

#ranges — Public Fediverse posts

Live and recent posts from across the Fediverse tagged #ranges, aggregated by home.social.

  1. Встреча ISO C++ в Софии: С++26 и рефлексия

    Привет! На связи Антон Полухин из техплатформы городских сервисов Яндекса, и сейчас я расскажу о софийской встрече Международного комитета по стандартизации языка программирования C++, в которой принимал активное участие. Это была последняя встреча, на которой новые фичи языка, с предодобренным на прошлых встречах дизайном, ещё могли попасть в C++26. И результат превзошёл все ожидания: compile-time-рефлексия рефлексия параметров функций аннотации std::optional<T&‍> параллельные алгоритмы Об этих и других новинках расскажу в посте

    habr.com/ru/companies/yandex/a

    #c++29 #с++29 #c++26 #с++26 #с++ #c++ #reflection #constexpr #exception #simd #safety #security #undefined_behavior #annotations #parallel_programming #executor #executors #ranges #coroutines

  2. Reverse Iterations

    Sometimes, we all need a way to iterate over a container in the opposite direction. There are several ways to reverse-iterate a container, and in this article, we’ll explore them.

    Index Iteration

    Probably the simplest way, taken from C is to iterate using an index location:

    for (int64_t index = ssize(container) - 1; index >= 0; --index) {    // do something with `container[index]`}

    This way is highly not recommended as it might lead to infinite loops if done incorrectly (for example by using uint64_t or size_t for the index type), and you can find more issues with this way in some previous articles about iterators in this blog.

    Reverse Iterators

    Another way to iterate a container is by using reverse iterators (rbegin() and rend()):

    for (auto it = container.rbegin(); it != container.rend(); ++it) {    // do something with `*it`}

    This is a more recommended way, but it might be a little bit frustrating compared to a regular for-range loop:

    for (auto elem : container) { /*do something with `elem`*/ }

    The closest way to this method using the standard (before C++20) is:

    std::for_each(container.rbegin(), container.rend(), [](auto elem) {    // so something with `elem`});

    Ranges (C++20)

    If you are using the ranges library, or using at least C++20, you can use the following method to iterate a container in reverse order:

    for (auto elem : container | std::views::reverse) {    // do something with `elem`}

    Custom Reverse Container View

    Another way to use the for-range loop (even if you don’t use C++20 or the ranges library), is to create your own container mask, to modify the behavior of the begin() and end() functions:

    template <typename T>class reverse_view {public:    reverse_view(T& cont) : container(&cont) {}        typename T::reverse_iterator begin() { return container->rbegin(); }    typename T::reverse_iterator end() { return container->rend(); }    private:    T* container;};

    Then, you’ll be able to use it like that:

    std::vector<int> container = {1, 2, 3, 4, 5};for (int elem : reverse_view(container)) {    std::cout << elem << ", ";}

    More Ways

    I hope you learned something new, and feel free to share in the comments if you know other ways to reverse-iterate over a container.

    #advanced #C_ #c11 #c14 #c17 #c20 #containers #Intermediate #Iterators #ranges #reverse #reverseIterators