zoukankan      html  css  js  c++  java
  • upper_bound() lower_bound() 用法

    头文件 #include<algorithm>

    #include<iostream>
    #include<vector>
    #include
    <algorithm> #include<stdio.h> using namespace std; int main() { ///第一种 int a[5]={1,2,4,4,6}; int it1,it2; it1=lower_bound(a,a+5,4)-a; it2=upper_bound(a,a+5,4)-a; printf("%d %d ",it1,a[it1]); // /*输出*/ 2 4 printf("%d %d ",it2,a[it2]); // /*输出*/ 4 6 ///第二种 vector<int>vec; vec.push_back(1),vec.push_back(2); vec.push_back(4),vec.push_back(4); vec.push_back(6); vector<int>::iterator it11,it22; it11=lower_bound(vec.begin(),vec.end(),4); it22=upper_bound(vec.begin(),vec.end(),4); it1=lower_bound(vec.begin(),vec.end(),4)-vec.begin(); it2=upper_bound(vec.begin(),vec.end(),4)-vec.begin(); printf("%d %d ",it1,*it11); // /*输出*/ 2 4 printf("%d %d ",it2,*it22); // /*输出*/ 4 6

      /// 若是数组内未出现的值 如3 则输出均为 2 4
    return 0; }

     结构体也可使用

    #include <iostream>
    #include <algorithm>
    #include <stdio.h>
    #include <cstring>
    using namespace std;
    struct NODE
    {
        int x,y;
        bool operator<(const NODE& p)const{
            if(x==p.x) return y>p.y;
            return x<p.x;
        }
    }a[1005];
    int main()
    {
        int len=0;
        a[len].x=1, a[len++].y=4;
        a[len].x=2, a[len++].y=3;
        a[len].x=2, a[len++].y=5;
        a[len].x=1, a[len++].y=3;
        sort(a,a+len);
        printf("sort:
    ");
        for(int i=0;i<len;i++)
            printf("%d %d
    ",a[i].x,a[i].y);
        printf("
    ");
    
        int ind=lower_bound(a,a+len,(NODE){2,5})-a;
        printf("%d %d %d
    ",ind,a[ind].x,a[ind].y);
    
        return 0;
    }
  • 相关阅读:
    后台架构设计—数据存储层
    Linux应用程序基础
    Linux文件管理命令笔记
    CentOS7搭建LAMP实战
    (ospf、rip、isis、EIGRP)常见的动态路由协议简介
    python while 循环语句
    获取linux帮助命令
    破解linux虚拟机的密码
    gawk编程语言
    MySQL触发器在PHP项目中用来做信息备份、恢复和清空的方法介绍
  • 原文地址:https://www.cnblogs.com/zquzjx/p/8835425.html
Copyright © 2011-2022 走看看