이 기사에서는 C++ STL의 set::upper_bound(), 구문, 작업 및 반환 값에 대해 설명합니다.
C++ STL에서 무엇을 설정합니까?
C++ STL의 집합은 일반적인 순서로 고유한 요소를 가져야 하는 컨테이너입니다. 요소의 값이 요소를 식별하므로 집합에는 고유한 요소가 있어야 합니다. 집합 컨테이너에 값을 추가하면 나중에 수정할 수 없지만 집합에 값을 제거하거나 추가할 수는 있습니다. 집합은 이진 검색 트리로 사용됩니다.
무엇이 설정되어 있나요::upper_bound()?
upper_bound()는
구문
name_of_set.upper_bound(const type_t&값);
매개변수
이 함수는 매개변수, 즉 상한을 찾아야 하는 값을 받습니다.
반환 값
이 함수는 값보다 큰 바로 다음 요소를 가리키는 반복자를 반환합니다.
예시
Input: set<int> myset = {1, 2, 3, 4, 5};
Myset.upper_bound(3);
Output: Upper bound = 4 예시
#include <bits/stdc++.h>
using namespace std;
int main(){
set<int> Set;
Set.insert(9);
Set.insert(7);
Set.insert(5);
Set.insert(3);
Set.insert(1);
cout<<"Elements are : ";
for (auto i = Set.begin(); i!= Set.end(); i++)
cout << *i << " ";
auto i = Set.upper_bound(5);
cout <<"\nupper bound of 5 in the set is: ";
cout << (*i) << endl;
i = Set.upper_bound(1);
cout<<"upper bound of 1 in the set is: ";
cout << (*i) << endl;
return 0;
} 출력
위의 코드를 실행하면 다음 출력이 생성됩니다 -
upper bound of 5 in the set is: 7 upper bound of 1 in the set is: 3
예시
#include <iostream>
#include <set>
int main (){
std::set<int> Set;
std::set<int>::iterator one, end;
for (int i=1; i<10; i++)
Set.insert(i*10);
one = Set.lower_bound (20);
end = Set.upper_bound (40);
Set.erase(one , end); // 10 20 70 80 90
std::cout<<"Elements are: ";
for (std::set<int>::iterator i = Set.begin(); i!=Set.end(); ++i)
std::cout << ' ' << *i;
std::cout << '\n';
return 0;
} 출력
위의 코드를 실행하면 다음 출력이 생성됩니다 -
Elements are : 10 50 60 70 80 90