本帖最後由 林祐霆 於 2023-10-27 19:14 編輯
- #include<bits/stdc++.h>
- using namespace std;
- vector<int> v;
- bool compare(int a, int b)
- {
- return a>b;
- }
- int main()
- {
- v.push_back(7);
- v.push_back(2);
- v.push_back(1);
- v.push_back(3);
- v.push_back(11);
- v.push_back(5);
- sort(v.begin(), v.end());
- for(int i: v)
- cout<<i<<" ";
- cout<<endl;
- cout<<"-------"<<endl;
- auto it=lower_bound(v.begin(), v.end(),7); //>=9最接近的位置
- cout<<*it<<endl; //*: 取該位置的值
- it=upper_bound(v.begin(), v.end(), 7); //>9最接近的位置
- cout<<*it<<endl;
- cout<<"-------"<<endl;
- it=upper_bound(v.begin(), v.end(), 9);
- cout<<*it<<endl;
- cout<<*(--it)<<endl;
- cout<<"-------"<<endl;
- it=lower_bound(v.begin(), v.end(),3);
- cout<<*(++it)<<endl;
- cout<<it-v.begin()<<endl;
- cout<<"------------"<<endl;
- it=find(v.begin(), v.end(), 12); //找不到時會回傳end()指向的位址
- if(it==v.end())
- cout<<"no find"<<endl;
- it=upper_bound(v.begin(), v.end(), 12);
- if(it==v.end())
- cout<<"no find"<<endl;
- return 0;
- //1 2 3 5 7 11
- }
複製代碼 |