※ 引述《softwind (software everywhere)》之銘言:
: sort 可以針對 vector裡面的 element排序~
: sort( v.begin(), v.end(), CMP);
: 我想請問 如果我vector裡面的element 是 pair建出來的 那我該如何比較?
: (用first 比 和用 second 比)
: ex:
: vector< pair<int, int> > point2D; //假設 pair<>::firsst是x, second是y好了
: 那麼我要針對 x or y排序 我必須另外寫 compare function
: bool cmp_x( pair<int,int> &A, pair<int,int> &B){
: return A.first < B.first;
: }
: bool cmp_y( pair<int,int> &A, pair<int,int> &B){
: return A.second < B.second;
: }
: 請問這種情形下 我想用 functional裡面的東西 兜出同樣的效果 我該如何寫???
: 我最基本的問題是 pair 沒有辦法指定把 哪個member抓出來比...
: sort( point2D.begin(), point2D.end(), less<?,?>(?) );
: ?都不會填
: 感謝~
bool cmp(pair<int,int> &A, pair<int,int> &B){
if(A.first < B.first)
if(A.second < B.second)
return 1;
else
return 0;
else
return 0;
}
sort( point2D.begin(), point2D.end(), cmp);
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 60.244.112.65