std::flat_m...:equal_range(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

🌸 NAME 🌸

okay so bestie, welcome to the std::flat_m...:equal_range(3) page β€” the manual name is literally std::flat_multimap::equal_range, and the NAME line says std::flat_multimap::equal_range - std::flat_multimap::equal_range. no cap, this is the C++ Standard Library page for your β€œwhere are all my matching keys at?” moment πŸ’…

✨ Synopsis ✨

four overloads, all since C++23, fr fr:

          std::pair<iterator, iterator> equal_range( const Key& key );
       (1) (since C++23)
          std::pair<const_iterator, const_iterator> equal_range( const Key&
       (2) (since C++23)
          key ) const;
          template< class K >
       (3) (since C++23)
          std::pair<iterator, iterator> equal_range( const K& x );
          template< class K >
          std::pair<const_iterator, const_iterator> equal_range( const K& x
       (4) (since C++23)
          ) const;

okay so what does it actually do? Returns a range containing all elements with the given key in the container. The range is defined by two iterators, one pointing to the first element that is not less than key and another pointing to the first element greater than key. Alternatively, the first iterator may be obtained with lower_bound(), and the second with upper_bound(). πŸ¦‹

1,2) Compares the keys to key.

3,4) Compares the keys to the value x. This overload participates in overload resolution only if the qualified-id Compare::is_transparent is valid and denotes a type. It allows calling this function without constructing an instance of Key. iykyk, that’s the whole point of that overload.

πŸ’– Parameters πŸ’–

  • key β€” key value to compare the elements to.
  • x β€” alternative value that can be compared to Key.

πŸŽ€ Return value πŸŽ€

std::pair containing a pair of iterators defining the wanted range: the first pointing to the first element that is not less than key and the second pointing to the first element greater than key. 🫢

If there are no elements not less than key, past-the-end (see end()) iterator is returned as the first element. Similarly if there are no elements greater than key, past-the-end iterator is returned as the second element. that’s the β€œnothing matched, here’s the end” vibe.

Since emplace and unhinted insert always insert at the upper bound, the order of equivalent elements in the equal range is the order of insertion unless hinted insert or emplace_hint was used to insert an element at a different position. so if you’re obsessed with insertion order, this note is your bestie.

πŸ¦‹ Complexity πŸ¦‹

Logarithmic in the size of the container. ⭐

🌈 Example 🌈

the example literally says // Run this code πŸ‘‡

        #include <iostream>
        #include <flat_map>

        template<typename I>
        void print_equal_range(I first, I lb, I ub, I last)
        {
            for (I i{first}; i != lb; ++i)
                std::cout << *i << ' ';
            std::cout << "[ ";

            for (I i{lb}; i != ub; ++i)
                std::cout << *i << ' ';
            std::cout << ") ";

            for (I i{ub}; i != last; ++i)
                std::cout << *i << ' ';
            std::cout << '\n';
        }

        int main()
        {
            std::flat_multimap<int> c{4, 3, 2, 1, 3, 3};
            std::cout << "c = ";
            print_equal_range(begin(c), begin(c), end(c), end(c));
            for (int key{}; key != 6; ++key)
            {
                std::cout << "key = " << key << "; equal range = ";
                const auto [lb, ub] = c.equal_range(key);
                print_equal_range(begin(c), lb, ub, end(c));
            }
        }

πŸ‘‘ See also πŸ‘‘

  • find β€” finds element with specific key (public member function)
  • contains β€” checks if the container contains element with specific key (public member function)
  • count β€” returns the number of elements matching specific key (public member function)
  • upper_bound β€” returns an iterator to the first element greater than the given key (public member function)
  • lower_bound β€” returns an iterator to the first element not less than the given key (public member function)
  • equal_range β€” returns range of elements matching a specific key (function template)

catch you later, bestie πŸ’…βœ¨ this page came from source http://cppreference.com, version/date 2024.06.10, and it’s the std::flat_m...:equal_range(3) page. periodt. πŸŽ€

πŸ“¦ where this page comes from