zoukankan      html  css  js  c++  java
  • A

    ACM紫书 第五章 P108 【排序与检索】

    题意:找输入的数在排完序之后的位置。

    想自己用vector写下,却报错 iterator cannot convert '__gnu_cxx::__normal<int*, std::vector<int> >' to 'int' in assignment

    ·迭代器的接口几乎相当于普通的指针。让一个迭代器递增只需调用++操作符。

    使用*操作符可以得到迭代器引用的数据值。因而迭代器可以被任为是一种智能指针

    lower_bound ()返回值是地址,要用迭代器操作;

    还有就是lower_bound返回的是第一个大于等于对象的地址

    不一定是等于啊,要判断在不在 数组里,还要再判断一下

    还有就是 每次循环一定要初始化vector,进行操作 s.clear()

    贴代码(第一次用c++的输入输出格式<<>>总打反)

    #include<iostream>
    #include<algorithm>
    #include<vector>
    using namespace std;
    
    vector<int> s;
    int main(){
        int rnd=0,n,qst,q,x;
        while(++rnd){
                s.clear();        
            cin>>n>>qst;
            if(n==0&&qst==0)break;
            cout<<"CASE# "<<rnd<<":"<<endl;
            while(n--){
                cin>>x;
                s.push_back(x);
            }
            sort(s.begin(),s.end());
            while(qst--){
                cin>>q;
                vector<int>::iterator p=lower_bound(s.begin(),s.end(),q);
                if(*p==q)
                cout<<q<<" found at "<<p-s.begin()+1<<endl;
                else 
                cout<<q<<" not found"<<endl;
            }
        }
        return 0;
    }
  • 相关阅读:
    ●BZOJ 3926 [Zjoi2015]诸神眷顾的幻想乡
    ●BZOJ 1396 识别子串
    ●UVA 1608 Non-boring sequences
    ●SPOJ 8222 NSUBSTR–Substrings
    ●SPOJ 7258 Lexicographical Substring Search
    ●CodeForces 429D Trick_Function
    ●BZOJ 2555 SubString
    ●洛谷P2664 树上游戏
    ●洛谷P3168 [CQOI2015]任务查询系统
    【UVA1057】Routing
  • 原文地址:https://www.cnblogs.com/-ifrush/p/10159681.html
Copyright © 2011-2022 走看看