| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付赛孚耐:软件保护加密专家
身份认证令牌USB KEY   
共有 265 人关注过本帖
标题:[求助]stl sort中的谓词参数
收藏  订阅  推荐  打印 
aipb2007
Rank: 12Rank: 12Rank: 12
来自:CQU
等级:贵宾
威望:40
帖子:2881
积分:29414
注册:2007-3-18
[求助]stl sort中的谓词参数

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

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

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

搜索更多相关主题的帖子: stl  sort  谓词  参数  
2007-10-14 12:34
HJin
Rank: 12Rank: 12Rank: 12
等级:贵宾
威望:27
帖子:401
积分:4134
注册:2007-6-9
回复:(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



程序代码:

<BR>// sort algorithm example<BR>#include &lt;iostream&gt;<BR>#include &lt;algorithm&gt;<BR>#include &lt;vector&gt;<BR>using namespace std;</P> <P>bool myfunction (int i,int j) { return (i&lt;j); }</P> <P>struct myclass {<BR> bool operator() (int i,int j) { return (i&lt;j);}<BR>} myobject;</P> <P>int main () {<BR> int myints[] = {32,71,12,45,26,80,53,33};<BR> vector&lt;int&gt; myvector (myints, myints+8); // 32 71 12 45 26 80 53 33<BR> vector&lt;int&gt;::iterator it;</P> <P> // using default comparison (operator &lt;):<BR> sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33</P> <P> // using function as comp<BR> sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)</P> <P> // using object as comp<BR> sort (myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80)</P> <P> // print out content:<BR> cout &lt;&lt; "myvector contains:";<BR> for (it=myvector.begin(); it!=myvector.end(); ++it)<BR> cout &lt;&lt; " " &lt;&lt; *it;</P> <P> cout &lt;&lt; endl;</P> <P> return 0;<BR>}<BR> <BR><BR>


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: 12Rank: 12Rank: 12
来自:CQU
等级:贵宾
威望:40
帖子:2881
积分:29414
注册:2007-3-18

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: 2
等级:注册会员
帖子:119
积分:1298
注册:2006-3-27

why don't you refer stl source code?


马马乎乎
2007-10-19 21:43
孤魂居士
Rank: 4
来自:老A(中国地大)
等级:高级会员
威望:2
帖子:869
积分:8840
注册:2007-5-21

我进入了外语世界

想用3年成为编程高手10年都成不了,准备用10年成为编程高手的3年就成为了编程高手
2007-10-19 22:16
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.053652 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved