C++ STL set::lower_bound() 函數
set::lower_bound() 函數是一個預定義的函數,用於獲取集合中任意元素的下界。
它從集合中找到任何所需元素的下限。下界any_element
表示集合中第一個不被考慮之前的數字any_element
.因此,如果any_element
本身存在,那麽它就是any_element
否則立即下一個any_element
。
原型:
set<T> st; //declaration st<T> st::iterator it; //iterator declaration it=st.upper_bound(T key);
參數: T key;
//T
是數據類型
返回類型:如果lower_bound
鍵存在於集合中,迭代器指針指向下界,否則,st.end()
用法:
該函數從集合中找到任何所需元素的下限。下界x
表示集合中第一個不被考慮之前的數字x
.因此,如果x
本身存在,那麽它就是x
否則立即下一個x
。
例:
For a set of integer, set<int> st; st.insert(6); st.insert(4); st.insert(10); set content://sorted always(ordered) 4 6 10 it=st.lower_bound(6) Print *it; //6 it=st.lower_bound(8) Print *it; //10
要包含的頭文件:
#include <iostream> #include <set> OR #include <bits/stdc++.h>
C++ 實現:
#include <bits/stdc++.h>
using namespace std;
void printSet(set<int> st){
set<int>::iterator it;
cout<<"Set contents are:\n";
if(st.empty()){
cout<<"empty set\n";
return;
}
for(it=st.begin();it!=st.end();it++)
cout<<*it<<" ";
cout<<endl;
}
int main(){
cout<<"Example of lower_bound function\n";
set<int> st;
set<int>::iterator it;
cout<<"inserting 4\n";
st.emplace(4);
cout<<"inserting 6\n";
st.emplace(6);
cout<<"inserting 10\n";
st.emplace(10);
printSet(st); //printing current set
cout<<"lower bound of 6 is "<<*(st.lower_bound(6));
return 0;
}
輸出
Example of lower_bound function inserting 4 inserting 6 inserting 10 Set contents are: 4 6 10 lower bound of 6 is 6
相關用法
- C++ set::rbegin()、set::rend()用法及代碼示例
- C++ set::begin()、set::end()用法及代碼示例
- C++ set::erase()用法及代碼示例
- C++ set::size()用法及代碼示例
- C++ set::empty()用法及代碼示例
- C++ set::clear用法及代碼示例
- C++ set::find()用法及代碼示例
- C++ set::insert()用法及代碼示例
- C++ set::erase用法及代碼示例
- C++ set::swap()用法及代碼示例
- C++ set::upper_bound()用法及代碼示例
- C++ set::clear()用法及代碼示例
- C++ set::key_comp()用法及代碼示例
- C++ set::emplace()用法及代碼示例
- C++ set rbegin()用法及代碼示例
- C++ set upper_bound()用法及代碼示例
- C++ set swap()用法及代碼示例
- C++ set size()用法及代碼示例
- C++ set lower_bound()用法及代碼示例
- C++ set erase()用法及代碼示例
注:本文由純淨天空篩選整理自 set::lower_bound() function in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。