body, table{font-family: 微软雅黑; font-size: 13.5pt}
table{border-collapse: collapse; border: solid gray; border-width: 2px 0 2px 0;}
th{border: 1px solid gray; padding: 4px; background-color: #DDD;}
td{border: 1px solid gray; padding: 4px;}
tr:nth-child(2n){background-color: #f8f8f8;}

template< class ForwardIt, class T >
ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value ); 返回第一个不小于(>=)指定的数的迭代器。如果没找到就返回last  这个版本内部比较默认使用<
template< class ForwardIt, class T, class Compare >
ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value, Compare comp );
这个版本内部比较默认使用comp函数
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
void print(vector<int>& ve)
{
        for(auto elem:ve)
                cout<<elem<<" ";
        cout<<endl;
}
int main()
{
        //容器有序
        int arr[10] = {1,2,3,4,5,6,7,8,9,10};
        vector<int> ve1(arr,arr+10);
        print(ve1);
        auto it = lower_bound(ve1.begin(),ve1.end(),2);
        cout<<*it<<endl;
        auto it2 = lower_bound(ve1.begin(),ve1.end(),11);
        if(it2==ve1.end())
        {
                cout<<"not found!"<<endl;
        }
        //容器无序
        int arr2[10] = {2,1,4,3,6,8,5,7,9,10};
        vector<int> ve2(arr2,arr2+10);
        print(ve2);
        auto it3 = lower_bound(ve2.begin(),ve2.end(),5);
        cout<<*it3<<endl;
}
//1 2 3 4 5 6 7 8 9 10
//2
//not found!
//2 1 4 3 6 8 5 7 9 10
//6
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
void print(vector<int>& ve)
{
        for(auto elem:ve)
                cout<<elem<<" ";
        cout<<endl;
}
bool comp(const int& a,const int& b)
{
        return a<b;  //从小到大
}
int main()
{
        //容器有序
        int arr[10] = {1,2,3,4,5,6,7,8,9,10};
        vector<int> ve1(arr,arr+10);
        print(ve1);
        auto it = lower_bound(ve1.begin(),ve1.end(),2,comp);
        cout<<*it<<endl;
        print(ve1);
        auto it2 = lower_bound(ve1.begin(),ve1.end(),11,comp);
        print(ve1);
        if(it2==ve1.end())
        {
                cout<<"not found!"<<endl;
        }
        //容器无序
        int arr2[10] = {2,1,4,3,6,8,5,7,9,10};
        vector<int> ve2(arr2,arr2+10);
        print(ve2);
        auto it3 = lower_bound(ve2.begin(),ve2.end(),5,comp);
        print(ve2);
        cout<<*it3<<endl;
}
//1 2 3 4 5 6 7 8 9 10
//2
//1 2 3 4 5 6 7 8 9 10
//1 2 3 4 5 6 7 8 9 10
//not found!
//2 1 4 3 6 8 5 7 9 10
//2 1 4 3 6 8 5 7 9 10
//6
template< class ForwardIt, class T >
ForwardIt upper_bound( ForwardIt first, ForwardIt last, const T& value );
返回第一个大于(>)指定的数的迭代器指针。内部元素之间比较规则采用<
template< class ForwardIt, class T, class Compare >
ForwardIt upper_bound( ForwardIt first, ForwardIt last, const T& value, Compare comp );
内部元素之间比较规则采用comp
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
void print(vector<int>& ve)
{
        for(auto elem:ve)
                cout<<elem<<" ";
        cout<<endl;
}
int main()
{
        //容器有序
        int arr[10] = {1,2,3,4,5,6,7,8,9,10};
        vector<int> ve1(arr,arr+10);
        print(ve1);
        auto it = upper_bound(ve1.begin(),ve1.end(),2);
        cout<<*it<<endl;
        auto it2 = lower_bound(ve1.begin(),ve1.end(),11);
        if(it2==ve1.end())
        {
                cout<<"not found!"<<endl;
        }
        //容器无序
        int arr2[10] = {2,1,4,3,6,8,5,7,9,10};
        vector<int> ve2(arr2,arr2+10);
        print(ve2);
        auto it3 = lower_bound(ve2.begin(),ve2.end(),3);
        cout<<*it3<<endl;
}
//1 2 3 4 5 6 7 8 9 10
//3
//not found!
//2 1 4 3 6 8 5 7 9 10
//4
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
void print(vector<int>& ve)
{
        for(auto elem:ve)
                cout<<elem<<" ";
        cout<<endl;
}
bool comp(const int& a,const int& b)
{
        return a<b;  //从小到大
}
int main()
{
        //容器有序
        int arr[10] = {1,2,3,4,5,6,7,8,9,10};
        vector<int> ve1(arr,arr+10);
        print(ve1);
        auto it = upper_bound(ve1.begin(),ve1.end(),2,comp);
        cout<<*it<<endl;
        print(ve1);
        auto it2 = upper_bound(ve1.begin(),ve1.end(),11,comp);
        print(ve1);
        if(it2==ve1.end())
        {
                cout<<"not found!"<<endl;
        }
        //容器无序
        int arr2[10] = {2,1,4,3,6,8,5,7,9,10};
        vector<int> ve2(arr2,arr2+10);
        print(ve2);
        auto it3 = upper_bound(ve2.begin(),ve2.end(),5,comp);
        print(ve2);
        cout<<*it3<<endl;
}
//1 2 3 4 5 6 7 8 9 10
//3
//1 2 3 4 5 6 7 8 9 10
//1 2 3 4 5 6 7 8 9 10
//not found!
//2 1 4 3 6 8 5 7 9 10
//2 1 4 3 6 8 5 7 9 10
//6
template< class ForwardIt, class T >
std::pair<ForwardIt,ForwardIt>
    equal_range( ForwardIt first, ForwardIt last,
                const T& value );
返回的是两个迭代器指针,第一个迭代器指针相当于lower_bound返回的,第二个相当于upper_bound返回的,内部比较规则,默认<
如果没有不小于指定元素的数,就返回ForwardIt first,同理,后面一个没有满足要求的元素就返回ForwardIt last
template< class ForwardIt, class T, class Compare >
std::pair<ForwardIt,ForwardIt>
    equal_range( ForwardIt first, ForwardIt last,
                const T& value, Compare comp );
内部比较规则,comp
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
void print(vector<int>& ve)
{
        for(auto elem:ve)
                cout<<elem<<" ";
        cout<<endl;
}
int main()
{
        //容器有序
        int arr[10] = {1,2,3,4,5,6,7,8,9,10};
        vector<int> ve1(arr,arr+10);
        print(ve1);
        auto pair = equal_range(ve1.begin(),ve1.end(),5); //返回一个pair<vector<int>::iterator,vector<int>::iterator>,第一个指向第一个不小于5的元素,第二个指向第一个大于5的元素
        for(auto it = pair.first;it<=pair.second;++it)
                cout<<*it<<" ";
        cout<<endl;
        auto pair2 = equal_range(ve1.begin(),ve1.end(),11);
        if(pair2.first==ve1.end() && pair2.second==ve1.end())
                cout<<"not found"<<endl;
        //容器无序
        int arr2[10] = {2,1,4,3,6,8,5,7,9,10};
        vector<int> ve2(arr2,arr2+10);
        print(ve2);
        auto pair3 = equal_range(ve2.begin(),ve2.end(),5);
        for(auto it = pair3.first;it<=pair3.second;++it)
                cout<<*it<<" ";
        cout<<endl;
}
//1 2 3 4 5 6 7 8 9 10
//5 6
//not found
//2 1 4 3 6 8 5 7 9 10
//6 
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
void print(vector<int>& ve)
{
        for(auto elem:ve)
                cout<<elem<<" ";
        cout<<endl;
}
bool comp(const int&a ,const int&b)
{
        return a<b;
}
int main()
{
        //容器有序
        int arr[10] = {1,2,3,4,5,6,7,8,9,10};
        vector<int> ve1(arr,arr+10);
        print(ve1);
        auto pair = equal_range(ve1.begin(),ve1.end(),5,comp); //返回一个pair<vector<int>::iterator,vector<int>::iterator>,第一个指向第一个不小于5的元素,第二个指向第一个大于5的元素
        for(auto it = pair.first;it<=pair.second;++it)
                cout<<*it<<" ";
        cout<<endl;
        auto pair2 = equal_range(ve1.begin(),ve1.end(),11,comp);
        if(pair2.first==ve1.end() && pair2.second==ve1.end())
                cout<<"not found"<<endl;
        //容器无序
        int arr2[10] = {2,1,4,3,6,8,5,7,9,10};
        vector<int> ve2(arr2,arr2+10);
        print(ve2);
        auto pair3 = equal_range(ve2.begin(),ve2.end(),5,comp);
        for(auto it = pair3.first;it<=pair3.second;++it)
                cout<<*it<<" ";
        cout<<endl;
}
//1 2 3 4 5 6 7 8 9 10
//5 6
//not found
//2 1 4 3 6 8 5 7 9 10
//6 
std::pair<iterator,iterator> equal_range( const Key& key );
std::pair<const_iterator,const_iterator> equal_range( const Key& key ) const;
Returns a range containing all elements with key key in the container. The range is defined by two iterators, the first pointing to the first element of the wanted range and the second pointing past the last element of the range. 
//1 a
//1 b
//1 d
//请按任意键继续. . .

#include<iostream>
#include<unordered_map>
using namespace std;
int main()
{
        pair<int,char> arr[4] = {
                pair<int,char>(1,'a'),
                pair<int,char>(1,'b'),
                pair<int,char>(1,'d'),
                pair<int,char>(2,'b')
        };
        unordered_multimap<int,char> ump(arr,arr+4);
        pair<unordered_multimap<int,char>::iterator,unordered_multimap<int,char>::iterator> it = ump.equal_range(1);
        for(unordered_multimap<int,char>::iterator iit = it.first;iit!=it.second;++iit)
        {
                cout<<iit->first<<" "<<iit->second<<endl;
        }
        system("pause");
}

泛型算法,排序的相关操作,lower_bound、upper_bound、equal_range的更多相关文章

  1. 二分查找法(binary_search,lower_bound,upper_bound,equal_range)

    binary_search(二分查找) //版本一:调用operator<进行比较 template <class ForwardIterator,class StrictWeaklyCo ...

  2. C++ Primer : 第十章 : 泛型算法 之 只读、写和排序算法

    大多数算法都定义在<algorithm>头文件里,而标准库还在头文件<numeric>里定义了一组数值泛型算法,比如accumulate. ●  find算法,算法接受一对迭代 ...

  3. php对二维数组进行相关操作(排序、转换、去空白等)

    php对二维数组进行相关操作(排序.转换.去空白等) 投稿:lijiao 字体:[增加 减小] 类型:转载 时间:2015-11-04   这篇文章主要介绍了php对二维数组进行相关操作,包括php对 ...

  4. C++的那些事:容器和泛型算法

    一.顺序容器 1,标准库定义了3种类型的顺序容器:vector.list和deque.它们的差别主要在于访问元素的方式,以及添加或删除元素相关操作运算代价.标准库还提供了三种容器适配器:stack.q ...

  5. STL容器及泛型算法

    一.顺序容器 1.容器的选择 (1) 随机访问,选vector ,deque (2) 在中间插入或者删除元素,选list (3) 在头尾插入或删除元素 , 选deque 2.list的成员函数 (1) ...

  6. STL的一些泛型算法

    源地址:http://blog.csdn.net/byijie/article/details/8142859 从福州大学资料里摘下来的我现在能理解的泛型算法 algorithm min(a,b) 返 ...

  7. c++中常用的泛型算法

    std中定义了很好几种顺序容器,它们自身也提供了一些操作,但是还有很多算法,容器本身没有提供. 而在algorithm头文件中,提供了许多算法,适用了大多数顺序容器.与c++11相比,很多函数在 c+ ...

  8. C++基础之泛型算法

    标准库并未给每个容器添加大量功能,因此,通过大量泛型算法,来弥补.这些算法大多数独立于任何特定的容器,且是通用的,可用于不同类型的容器和不同的元素. 迭代器使得算法不依赖容器,但是算法依赖于元素的类型 ...

  9. C++ 泛型算法

    <C++ Primer 4th>读书笔记 标准容器(the standard container)定义了很少的操作.标准库并没有为每种容器类型都定义实现这些操作的成员函数,而是定义了一组泛 ...

随机推荐

  1. Lua --- 输入一个数字,输出阶乘

    function fact(n) == n then else ) end end print("Enter a number : ") a = io.read("*nu ...

  2. 20165327 2017-2018-2 《Java程序设计》第一周学习总结

    第1章 Java入门 一.Java 的特点 简单 面向对象 平台无关 多线程:允许同时完成多个任务 动态:Java程序的基本组成单元就是类(有些类是自己编写的,有一些是从类库中引入的,而类又是运行时动 ...

  3. Django初始化之基本操作

    1.指定要安装的Django版本 C:\Users\win7>pip install Django==1.11.8 2.查看安装的django版本 C:\Users\win7>pip sh ...

  4. WCF开发框架形成之旅---WCF的几种寄宿方式

    WCF开发框架形成之旅---WCF的几种寄宿方式 WCF寄宿方式是一种非常灵活的操作,可以在IIS服务.Windows服务.Winform程序.控制台程序中进行寄宿,从而实现WCF服务的运行,为调用者 ...

  5. sgu 203 Hyperhuffman

    题意:给出字符出现的次数,问替换成哈夫曼编码后的文本长度. 实际上观察发现就等于树的所有节点的和.用nlogn超时.用O(n),用两个队列,一个放原始数组,一个放新生成的节点. #include &l ...

  6. Spark分布式安装

    三台 服务器 n0,n2,n3 centos 6.4 X64 JDK, SCALA 2.11 Hadoop 2.2.0 spark-0.9.1-bin-hadoop2.tgz 说明: 1.所有机器上安 ...

  7. MySQL之库表详细操作

    一 库操作 1.创建数据库 1.1 语法 CREATE DATABASE 数据库名 charset utf8; 1.2 数据库命名规则 可以由字母.数字.下划线.@.#.$ 区分大小写 唯一性 不能使 ...

  8. spring boot 创建web项目并使用jsp作前台页面

    参考链接:https://www.cnblogs.com/sxdcgaq8080/p/7712874.html

  9. loj#528. 「LibreOJ β Round #4」求和

    求:\(\sum_{i=1}^n\sum_{j=1}^m\mu(gcd(i,j))^2\) 化简可得\(\sum_{i=1}^{min(n,m)}{\lfloor \frac{n}{i} \rfloo ...

  10. 模拟spring的IoC

    1.新建一个web项目,jdk版本为1.8.0_111,使用 Jsp + Servlet + Model 实现MVC模式,并使用BeanFactory工厂 + xml配置文件 + 反射 来解耦合 整个 ...