Computer >> Máy Tính >  >> Lập trình >> C ++

đặt hàm Lower_bound () trong C ++ STL

Đặt hàm Lower_bound () trong C ++ STL trả về một trình vòng lặp trỏ đến phần tử trong vùng chứa tương đương với k được truyền vào tham số. Nếu k không có trong vùng chứa đã đặt, thì hàm trả về một trình vòng lặp trỏ đến phần tử tiếp theo ngay lập tức lớn hơn k.

Thuật toán

Begin
   Initialize an empty set container s.
   Initializing a set container as inetrator.
   Insert some elements in s set container.
   Call function to find the lower bound value of a given key, which is
   passed to iter set container.
   Print the lower bound value of the given key.
End.


Mã mẫu

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
   set<int> s;                 //Declaring an empty set container
   set<int>::iterator iter;    //Declaring a set container as iterator which will point to the lower bound value
   s.insert(7);                //inserting elements in the set container s
   s.insert(6);
   s.insert(1);
   s.insert(4);
   s.insert(2);
   s.insert(9);
   s.insert(10);
   iter = s.lower_bound(4);       //passing a key by parameter to find its lower bound
      cout <<"The lower bound of 4 is: "<< *iter << " "<<endl; //printing the lowerbound value
   iter = s.lower_bound(5);
      cout <<"The lower bound of 5 is: " <<*iter << " "<<endl;
   iter = s.lower_bound(30);
      cout <<"The lower bound of 30 is: " <<*iter << " "<<endl;

return 0;
}

Đầu ra

The lower bound of 4 is: 4
The lower bound of 5 is: 6
The lower bound of 30 is: 7