Namespaces
Variants

std::set<Key,Compare,Allocator>:: equal_range

From cppreference.net

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

std:: pair < const_iterator, const_iterator >

equal_range ( const K & x ) const ;
(4) (since C++14)
(constexpr since C++26)

주어진 키를 가진 모든 요소를 포함하는 범위를 반환합니다. 이 범위는 두 개의 반복자로 정의되며, 하나는 주어진 키보다 작지 않은 첫 번째 요소를 가리키고 다른 하나는 주어진 키보다 큰 첫 번째 요소를 가리킵니다.

또는, 첫 번째 반복자는 lower_bound() 로 얻을 수 있고, 두 번째 반복자는 upper_bound() 로 얻을 수 있습니다.

1,2) 키를 key 와 비교합니다.
3,4) 키를 값 x 와 비교합니다.
이 오버로드는 Compare transparent 인 경우에만 오버로드 해결에 참여합니다. 이를 통해 Key 의 인스턴스를 생성하지 않고 이 함수를 호출할 수 있습니다.

목차

매개변수

key - 요소와 비교할 키 값
x - Key 와 비교 가능한 대체 값

반환값

std::pair 원하는 범위를 정의하는 한 쌍의 반복자를 포함하는:

  • 첫 번째 반복자는 주어진 키보다 작지 않은 첫 번째 요소를 가리키며, 해당 요소가 존재하지 않으면 end ( ) 를 가리킵니다.
  • 두 번째 반복자는 주어진 키보다 큰 첫 번째 요소를 가리키며, 해당 요소가 존재하지 않으면 end ( ) 를 가리킵니다.

복잡도

컨테이너 크기에 대해 로그 시간 복잡도를 가집니다.

참고 사항

기능 테스트 매크로 표준 기능
__cpp_lib_generic_associative_lookup 201304L (C++14) 연관 컨테이너 에서의 이종 비교 검색, ( 3,4 ) 오버로드용

예제

#include <set>
#include <functional>
#include <print>
#include <ranges>
#include <string>
#include <string_view>
#include <tuple>
struct Names
{
    std::string forename, surname;
    friend auto operator<(const Names& lhs, const Names& rhs)
    {
        return std::tie(lhs.surname, lhs.forename) < std::tie(rhs.surname, rhs.forename);
    }
};
struct SurnameCompare
{
    std::string_view surname;
    friend bool operator<(const Names& lhs, const SurnameCompare& rhs)
    {
        return lhs.surname < rhs.surname;
    }
    friend bool operator<(const SurnameCompare& lhs, const Names& rhs)
    {
        return lhs.surname < rhs.surname;
    }
}; 
std::set<Names, std::less<>> characters
{
    {"Homer", "Simpson"},
    {"Marge", "Simpson"},
    {"Lisa", "Simpson"},
    {"Ned", "Flanders"},
    {"Joe", "Quimby"}
};
void print_unique(const Names& names)
{
    auto [begin, end] = characters.equal_range(names);
    std::print
    (
        "Found {} characters with name \"{} {}\"\n", 
        std::distance(begin, end), 
        names.forename, names.surname
    );
}
void print_by_surname(std::string_view surname)
{
    auto [begin, end] = characters.equal_range(SurnameCompare{surname});
    std::print("Found {} characters with surname \"{}\":\n", std::distance(begin, end), surname);
    for (const Names& names : std::ranges::subrange(begin, end))
        std::print("    {} {}\n", names.forename, names.surname);
}
int main()
{
    print_unique({"Maude", "Flanders"});
    print_unique({"Lisa", "Simpson"});
    print_by_surname("Simpson");
}

출력:

Found 0 characters with name "Maude Flanders"
Found 1 characters with name "Lisa Simpson"
Found 3 characters with surname "Simpson":
    Homer Simpson
    Lisa Simpson
    Marge Simpson

참고 항목

특정 키를 가진 요소를 찾습니다
(public member function)
(C++20)
컨테이너가 특정 키를 가진 요소를 포함하는지 확인합니다
(public member function)
특정 키와 일치하는 요소의 개수를 반환합니다
(public member function)
주어진 키보다 첫 번째 요소에 대한 반복자를 반환합니다
(public member function)
주어진 키보다 작지 않은 첫 번째 요소에 대한 반복자를 반환합니다
(public member function)
특정 키와 일치하는 요소들의 범위를 반환합니다
(function template)