Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

C++ STL에서 equal_range() 함수 설정

<시간/>

이 기사에서는 C++ STL의 set::equal_range() 함수, 구문, 작업 및 반환 값에 대해 설명합니다.

C++ STL에서 무엇을 설정합니까?

C++ STL의 집합은 일반적인 순서로 고유한 요소를 가져야 하는 컨테이너입니다. 요소의 값이 요소를 식별하므로 집합에는 고유한 요소가 있어야 합니다. 집합 컨테이너에 값을 추가하면 나중에 수정할 수 없지만 집합에 값을 추가하거나 제거할 수는 있습니다. 집합은 이진 검색 트리로 사용됩니다.

설정된 항목::equal_range()

equal_range() 함수는 헤더 파일에 정의된 C++ STL의 내장 함수입니다. 이 함수는 함수의 인수로 전달된 값을 포함하는 집합 컨테이너의 범위를 반환합니다. 세트는 모든 고유 값을 포함하므로 발견된 범위의 동등한 값은 단일입니다. 값이 컨테이너에 없으면 범위는 0이 되며 두 반복자는 첫 번째 위치를 가리킵니다.

구문

Set1.equal_range(const type_t& value);

매개변수

이 함수는 하나의 매개변수, 즉 찾을 요소를 허용합니다.

반환 값

이 함수는 쌍을 반환하거나 컨테이너의 하한에서 시작하여 컨테이너에서 찾을 요소까지의 반복자 범위를 말할 수 있습니다.

예시

Input: set<int> myset = {10, 20, 30, 40};
Output: lower bound of 30 is 30

예시

#include <bits/stdc++.h>
using namespace std;
int main(){
   set<int> mySet;
   mySet.insert(10);
   mySet.insert(20);
   mySet.insert(30);
   mySet.insert(40);
   mySet.insert(50);
   cout<<"Elements before applying range() Function : ";
   for (auto i = mySet.begin(); i != mySet.end(); i++)
      cout << *i << " ";
   auto i = mySet.equal_range(30);
   cout<<"\nlower bound of 30 is "<< *i.first;
   cout<<"\nupper bound of 30 is "<< *i.second;
   i = mySet.equal_range(40);
   cout<<"\nlower bound of 40 is " << *i.first;
   cout<<"\nupper bound of 40 is " << *i.second;
   i = mySet.equal_range(10);
   cout<<"\nlower bound of 10 is " << *i.first;
   cout<<"\nupper bound of 10 is " << *i.second;
   return 0;
}

출력

위의 코드를 실행하면 다음 출력이 생성됩니다 -

Elements before applying range() Function : 10 20 30 40 50
lower bound of 30 is 30
upper bound of 30 is 40
lower bound of 40 is 40
upper bound of 40 is 50
lower bound of 10 is 10
upper bound of 10 is 20