| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1131 人关注过本帖
标题:[求助]stl sort中的谓词参数
只看楼主 加入收藏
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
 问题点数:0 回复次数:4 
[求助]stl sort中的谓词参数

sort中第三个参数是个函数指针,返回类型bool,接受两个元素对象。

我想知道其完整的定义类型

bool (*pred) (****,****);

搜索更多相关主题的帖子: stl sort 谓词 参数 
2007-10-14 12:34
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 
回复:(aipb2007)[求助]stl sort中的谓词参数

don't know if i understand what you need.

The predicate (I don't know if it should be translated as "weici") can be a funtion pointer and function object --- a class which overloads operator().


Here is what I copied from cplusplus.com


程序代码:

// sort algorithm example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool myfunction (int i,int j) { return (i<j); }

struct myclass {
bool operator() (int i,int j) { return (i<j);}
} myobject;

int main () {
int myints[] = {32,71,12,45,26,80,53,33};
vector<int> myvector (myints, myints+8); // 32 71 12 45 26 80 53 33
vector<int>::iterator it;

// using default comparison (operator <):
sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33

// using function as comp
sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)

// using object as comp
sort (myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80)

// print out content:
cout << \"myvector contains:\";
for (it=myvector.begin(); it!=myvector.end(); ++it)
cout << \" \" << *it;

cout << endl;

return 0;
}



I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-10-14 13:02
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 

oh,I made a unclear expression.

my problem is:

template<class T>
struct X{
typedef bool (*pred)(const T&,const T&); //define a function pointer type
pred p; //pred data member
X(pred _p):p(_p){}
};
bool cmp1(int a,int b){return a<b;}
bool cmp2(const int &a,const int &b){return a<b;}
X<int> x1(cmp1); //doesn't work,compile error
X<int> x2(cmp2); //ok


how I define the type of pred can accept both cmp1 and cmp2?
or maybe I should use the sort(1,2,pred)'s way to implement,because both sort(..,..,cmp1) and sort(..,..,cmp2)
can be accepted,but I don't konw the sort's implementation clearly.

[此贴子已经被作者于2007-10-14 15:19:17编辑过]


Fight  to win  or  die...
2007-10-14 15:02
jiangzw625
Rank: 1
等 级:新手上路
帖 子:119
专家分:0
注 册:2006-3-27
收藏
得分:0 

why don't you refer stl source code?


马马乎乎
2007-10-19 21:43
孤魂居士
Rank: 2
来 自:老A(中国地大)
等 级:论坛游民
威 望:4
帖 子:1142
专家分:18
注 册:2007-5-21
收藏
得分:0 
我进入了外语世界

准备用3年做个高级软件工程师 10年也做不成。准备用10年做成高级软件工程师 3年就成了QQ 群 45771086
欢迎版主...欢迎JAVA爱好者...
一起从深夜 到凌晨...
2007-10-19 22:16
快速回复:[求助]stl sort中的谓词参数
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018777 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved