c, c++ 리마인드

c++ pair, lower_bound/upper_bound 유의 사항

무한 나무 2023. 9. 24. 00:10

pair 에 대해서 lower/upper bound 할 때, second도 영향을 줌

    set<pair<int, int>> test;
    test.insert({ 10,50 });
    test.insert({ 30,10 });
    test.insert({ 50,0 });
    test.insert({ 70,60 });
    test.insert({ 100,-50 });

    auto iter = test.lower_bound({ 30,10 }); //{30,10} first가 같은경우, second보다 같거나 큰걸 찾음

    auto iter2 = test.upper_bound({ 50,0 }); //{50,0} first가 같은경우, second보다 큰걸 찾음.

 

lower든 upper든  first 로만 찾고 싶을 때,

auto iter = test.lower_bound({ 값, -INF});	// first만 고려

auto iter2 = test.upper_bound({ 값, INF});	// first만 고려

 

 

 

내림차순 컨테이너에서 lower_bound / upper_bound

  • 내림차순에서 lower_bound 는 목표보다 같거나 작은 첫 번째 요소 iter를 찾는 것.
auto iter = lower_bound (container.begin(), contaniner.end(), x, greater<type>());

 

  • 내림차순에서 upper_bound 는 목표보다 작은 첫 번째 요소 iter를 찾는 것.
auto iter = upper_bound (container.begin(), contaniner.end(), x, greater<type>());