标签:aci   容器   class   大于   第一个   include   算法   www   返回   
    
        
            
| 
template
                             
ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value ); 返回第一个不小于(>=)指定的数的迭代器。如果没找到就返回last  这个版本内部比较默认使用
                             | 
template
                             
ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value, Compare comp );
                             
这个版本内部比较默认使用comp函数
                             | 
| 
#include
                             
#include
                             
#include
                             
using namespace std;
                             
void print(vector& ve)
                             
{
                             
        for(auto elem:ve)
                                             cout
                                     cout
                             
}
                             
int main()
                             
{
                             
        //容器有序
                             
        int arr[10] = {1,2,3,4,5,6,7,8,9,10};
                             
        vector ve1(arr,arr+10);
                             
        print(ve1);
                             
        auto it = lower_bound(ve1.begin(),ve1.end(),2);
                                     cout
                             
        auto it2 = lower_bound(ve1.begin(),ve1.end(),11);
                             
        if(it2==ve1.end())
                             
        {
                                             cout
                             
        }
                             
        //容器无序
                             
        int arr2[10] = {2,1,4,3,6,8,5,7,9,10};
                             
        vector ve2(arr2,arr2+10);
                             
        print(ve2);
                             
        auto it3 = lower_bound(ve2.begin(),ve2.end(),5);
                                     cout
                             
}
                             
//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
                             
#include
                             
#include
                             
using namespace std;
                             
void print(vector& ve)
                             
{
                             
        for(auto elem:ve)
                                             cout
                                     cout
                             
}
                             
bool comp(const int& a,const int& b)
                             
{
                             
        return a //从小到大
                             
}
                             
int main()
                             
{
                             
        //容器有序
                             
        int arr[10] = {1,2,3,4,5,6,7,8,9,10};
                             
        vector ve1(arr,arr+10);
                             
        print(ve1);
                             
        auto it = lower_bound(ve1.begin(),ve1.end(),2,comp);
                                     cout
                             
        print(ve1);
                             
        auto it2 = lower_bound(ve1.begin(),ve1.end(),11,comp);
                             
        print(ve1);
                             
        if(it2==ve1.end())
                             
        {
                                             cout
                             
        }
                             
        //容器无序
                             
        int arr2[10] = {2,1,4,3,6,8,5,7,9,10};
                             
        vector ve2(arr2,arr2+10);
                             
        print(ve2);
                             
        auto it3 = lower_bound(ve2.begin(),ve2.end(),5,comp);
                             
        print(ve2);
                                     cout
                             
}
                             
//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
                             
ForwardIt upper_bound( ForwardIt first, ForwardIt last, const T& value );
                             返回第一个大于(>)指定的数的迭代器指针。内部元素之间比较规则采用
                             | 
template
                             
ForwardIt upper_bound( ForwardIt first, ForwardIt last, const T& value, Compare comp );
                             
内部元素之间比较规则采用comp
                             | 
| 
#include
                             
#include
                             
#include
                             
using namespace std;
                             
void print(vector& ve)
                             
{
                             
        for(auto elem:ve)
                                             cout
                                     cout
                             
}
                             
int main()
                             
{
                             
        //容器有序
                             
        int arr[10] = {1,2,3,4,5,6,7,8,9,10};
                             
        vector ve1(arr,arr+10);
                             
        print(ve1);
                             
        auto it = upper_bound(ve1.begin(),ve1.end(),2);
                                     cout
                             
        auto it2 = lower_bound(ve1.begin(),ve1.end(),11);
                             
        if(it2==ve1.end())
                             
        {
                                             cout
                             
        }
                             
        //容器无序
                             
        int arr2[10] = {2,1,4,3,6,8,5,7,9,10};
                             
        vector ve2(arr2,arr2+10);
                             
        print(ve2);
                             
        auto it3 = lower_bound(ve2.begin(),ve2.end(),3);
                                     cout
                             
}
                             
//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
                             
#include
                             
#include
                             
using namespace std;
                             
void print(vector& ve)
                             
{
                             
        for(auto elem:ve)
                                             cout
                                     cout
                             
}
                             
bool comp(const int& a,const int& b)
                             
{
                             
        return a//从小到大
                             
}
                             
int main()
                             
{
                             
        //容器有序
                             
        int arr[10] = {1,2,3,4,5,6,7,8,9,10};
                             
        vector ve1(arr,arr+10);
                             
        print(ve1);
                             
        auto it = upper_bound(ve1.begin(),ve1.end(),2,comp);
                                     cout
                             
        print(ve1);
                             
        auto it2 = upper_bound(ve1.begin(),ve1.end(),11,comp);
                             
        print(ve1);
                             
        if(it2==ve1.end())
                             
        {
                                             cout
                             
        }
                             
        //容器无序
                             
        int arr2[10] = {2,1,4,3,6,8,5,7,9,10};
                             
        vector ve2(arr2,arr2+10);
                             
        print(ve2);
                             
        auto it3 = upper_bound(ve2.begin(),ve2.end(),5,comp);
                             
        print(ve2);
                                     cout
                             
}
                             
//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
                             
std::pair
                             
    equal_range( ForwardIt first, ForwardIt last,
                             
                 const T& value );
                             
返回的是两个迭代器指针,第一个迭代器指针相当于lower_bound返回的,第二个相当于upper_bound返回的,内部比较规则,默认
                             
如果没有不小于指定元素的数,就返回ForwardIt first,同理,后面一个没有满足要求的元素就返回ForwardIt last
                             | 
template
                             
std::pair
                             
    equal_range( ForwardIt first, ForwardIt last,
                             
                 const T& value, Compare comp );
                             
内部比较规则,comp
                             | 
| 
#include
                             
#include
                             
#include
                             
using namespace std;
                             
void print(vector& ve)
                             
{
                             
        for(auto elem:ve)
                                             cout
                                     cout
                             
}
                             
int main()
                             
{
                             
        //容器有序
                             
        int arr[10] = {1,2,3,4,5,6,7,8,9,10};
                             
        vector ve1(arr,arr+10);
                             
        print(ve1);
                             
        auto pair = equal_range(ve1.begin(),ve1.end(),5); //返回一个pair::iterator,vector::iterator>,第一个指向第一个不小于5的元素,第二个指向第一个大于5的元素
                                     for(auto it = pair.first;it
                                             cout
                                     cout
                             
        auto pair2 = equal_range(ve1.begin(),ve1.end(),11);
                             
        if(pair2.first==ve1.end() && pair2.second==ve1.end())
                                             cout
                             
        //容器无序
                             
        int arr2[10] = {2,1,4,3,6,8,5,7,9,10};
                             
        vector ve2(arr2,arr2+10);
                             
        print(ve2);
                             
        auto pair3 = equal_range(ve2.begin(),ve2.end(),5);
                                     for(auto it = pair3.first;it
                                             cout
                                     cout
                             
}
                             
//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
                             
#include
                             
#include
                             
using namespace std;
                             
void print(vector& ve)
                             
{
                             
        for(auto elem:ve)
                                             cout
                                     cout
                             
}
                             
bool comp(const int&a ,const int&b)
                             
{
                                     return a
                             
}
                             
int main()
                             
{
                             
        //容器有序
                             
        int arr[10] = {1,2,3,4,5,6,7,8,9,10};
                             
        vector ve1(arr,arr+10);
                             
        print(ve1);
                             
        auto pair = equal_range(ve1.begin(),ve1.end(),5,comp); //返回一个pair::iterator,vector::iterator>,第一个指向第一个不小于5的元素,第二个指向第一个大于5的元素
                                     for(auto it = pair.first;it
                                             cout
                                     cout
                             
        auto pair2 = equal_range(ve1.begin(),ve1.end(),11,comp);
                             
        if(pair2.first==ve1.end() && pair2.second==ve1.end())
                                             cout
                             
        //容器无序
                             
        int arr2[10] = {2,1,4,3,6,8,5,7,9,10};
                             
        vector ve2(arr2,arr2+10);
                             
        print(ve2);
                             
        auto pair3 = equal_range(ve2.begin(),ve2.end(),5,comp);
                                     for(auto it = pair3.first;it
                                             cout
                                     cout
                             
}
                             
//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 
                             | 
            
         
    
泛型算法,排序的相关操作,lower_bound、upper_bound、equal_range
标签:aci   容器   class   大于   第一个   include   算法   www   返回   
原文地址:https://www.cnblogs.com/meihao1203/p/9552606.html