C++ STL 算法:查找算法(7) lower_bound、upper_bound、equal_range

来源:互联网 发布:新一代网络ipv6 编辑:程序博客网 时间:2024/06/11 07:18
1、lower_bound()
pos1 = lower_bound(ilist.begin(), ilist.end(), 5);
cout << "第一个5的位置: " << distance(ilist.begin(),pos1) + 1 << endl;
2、upper_bound()
pos2 = upper_bound(ilist.begin(), ilist.end(), 5);
cout << "大于5的第一个位置: " << distance(ilist.begin(),pos2) + 1 << endl;
3、equal_range()
pair<list<int>::iterator,list<int>::iterator> range;
range = equal_range(ilist.begin(),ilist.end(),5);
cout << distance(ilist.begin(),range.first)+1 << endl;
cout << distance(ilist.begin(),range.second)+1 << endl;
4、关联式容器有等效的成员函数,性能更佳
multiset<int> imset;
//插入数据
imset.lower_bound(5);
imset.upper_bound(5);
imset.equal_range(5);

0 0
原创粉丝点击