1 Star 1 Fork 0

AlbertDarren / STL

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
lower_bound_upper_bound_equal_range.cpp 2.34 KB
一键复制 编辑 原始数据 按行查看 历史
AlbertDarren 提交于 2022-04-03 00:55 . first commit
/**
* @author AlbertDarren
* @contact 2563491540@qq.com
*/
#include <iostream>
#include <algorithm>
#include <list>
#include <set>
#include <cstdlib>
#include <ctime>
using namespace std;
void show(auto &container)
{
for(auto element:container)
{
cout << element << " ";
}
cout << endl;
}
int main_33()
{
srand((unsigned int)time(nullptr));
list<int> int_list;
multiset<int> int_multiset;
int random_int;
for (int i=0; i<20 ; ++i )
{
random_int=rand()%20;
int_list.push_back(random_int);
int_multiset.insert(random_int);
}
show(int_list);
int_list.sort();
show(int_list);
random_int=rand()%20;
auto iter=lower_bound(int_list.begin(),int_list.end(),random_int);
if (iter!=int_list.end())
{
cout << "The first element that is not less than "<<random_int<<" is at "<<distance(int_list.begin(),iter) << endl;
}
else
{
cout << "Every element is less than "<<random_int << endl;
}
iter=upper_bound(int_list.begin(),int_list.end(),random_int);
if (iter!=int_list.end())
{
cout << "The first element that is greater than "<<random_int<<" is at "<<distance(int_list.begin(),iter) << endl;
}
else
{
cout << "no elements are greater than "<<random_int << endl;
}
pair<list<int>::iterator,list<int>::iterator> range=equal_range(int_list.begin(),int_list.end(),random_int);
cout << "The largest subrange is between "<<*range.first<<" and "<<*range.second<< endl;
cout << "multiset container" << endl;
show(int_multiset);
random_int=rand()%20;
auto it=int_multiset.lower_bound(random_int);
if (it!=int_multiset.end())
{
cout << "The first element that is not less than "<<random_int<<" is "<<*it << endl;
}
else
{
cout << "Every element is less than "<<random_int << endl;
}
it=int_multiset.upper_bound(random_int);
if (it!=int_multiset.end())
{
cout << "The first element that is greater than "<<random_int<<" is "<<*it << endl;
}
else
{
cout << "no elements are greater than "<<random_int << endl;
}
pair<multiset<int>::iterator,multiset<int>::iterator> it_range=int_multiset.equal_range(random_int);
cout << "The largest subrange is between "<<*it_range.first<<" and "<<*it_range.second<< endl;
return 0;
}
C++
1
https://gitee.com/AlbertDarren_admin/stl.git
git@gitee.com:AlbertDarren_admin/stl.git
AlbertDarren_admin
stl
STL
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891