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;
}
原文地址:https://www.cnblogs.com/-ifrush/p/10159681.html