首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

stl中sort()排序的有关问题

2012-02-22 
stl中sort()排序的问题我用C++编了如下代码片断:#includeiostream#includevector#includeiterator#i

stl中sort()排序的问题
我用C++编了如下代码片断:

#include   <iostream>
#include   <vector>
#include   <iterator>
#include   <algorithm>
#include   <functional>

#include   "Advertisement.h "
#include   "Listing.h "

using   namespace   std;

Listing   ::   Listing(const   Listing&   li)
{
copy(li.objects.begin(),   li.objects.end(),   objects.begin());
}

//   return   a   sorted   copy   of   this   Listing
Listing   Listing   ::   sort(string   field)
{
Listing   temp   =   *this;

if(strcmp(field.data(),   "email ")   ==   0)
{
std::sort((*temp.begin()),   (*temp.end()),   less_email);
}

return   temp;

}

bool   Listing   ::   less_email(const   Advertisement*   a1,   const   Advertisement*   a2)
{
return   (strcmp(a1-> getEmail().data(),   a2-> getEmail().data())   ==   -1);
}

编译的时候一直出错:
error   C2664:   'void   __cdecl   std::sort(class   Advertisement   *,class   Advertisement   *,bool   (__thiscall   *)(const   class   Advertisement   *,const   class   Advertisement   *)) '   :  
cannot   convert   parameter   3   from   'bool   (const   class   Advertisement   *,const   class   Advertisement   *) '   to   'bool   (__thiscall   *)(const   class   Advertisement   *,const   class   Advertisement   *) '
                None   of   the   functions   with   this   name   in   scope   match   the   target   type
Error   executing   cl.exe.

麻烦各位解答一下!急!!!

[解决办法]
less_email写的不对,改成类的static成员函数。另外参数好像也有问题,改成const Advertisement & 试下。

[解决办法]
less_email是成员函数,调用不了。
可以定义为友元函数。

你也可以重载Listing的运算符 <
然后直接调用std::sort((*temp.begin()), (*temp.end()));

热点排行