A - Where is the Marble? UVA - 10474
阿新 • • 發佈:2018-12-22
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; }