使用stl的sort给对象排序的问题
就是把几个point类的对象,放进一个list里,然后再利用sort和自定义的函数对象MyComparator排序。不知道为什么,在Dev—C++4和VC6下都无法编译通...
就是把几个point类的对象,放进一个list里,然后再利用sort和自定义的函数对象MyComparator排序。不知道为什么,在Dev—C++4和VC6下都无法编译通过。
大家帮帮忙看下是怎么回事,谢谢了!
代码如下:
#include <list>
#include <iostream>
#include <functional>
#include <algorithm>
class point
{
public:
int x;
int y;
point(int x,int y);
~point();
};
point::point(int x,int y)
:x(x),y(y)
{}
point::~point()
{}
template<typename T>
void printtest(T i)
{
std::cout<<(i->x)<<","<<(i->y)<<" ";
}
bool MyComparator(const point* a,const point* b)
{
return ((a->x) >(b->x))?true:false;
}
int main()
{
std::list<point*> j_list;
point p1(0,1);
point p2(9,2);
point p3(0,1);
point p4(8,2);
point p5(0,1);
point p6(0,2);
point p7(56,1);
point p8(0,2);
point* pp1=&p1;
point* pp2=&p2;
point* pp3=&p3;
point* pp4=&p4;
point* pp5=&p5;
point* pp6=&p6;
point* pp7=&p7;
point* pp8=&p8;
j_list.push_back(pp1);
j_list.push_back(pp2);
j_list.push_back(pp3);
j_list.push_back(pp4);
j_list.push_back(pp5);
j_list.push_back(pp6);
j_list.push_back(pp7);
j_list.push_back(pp8);
j_list.remove(pp1);
std::for_each(j_list.begin(),j_list.end(),printtest<point*>);
std::cout<<std::endl;
std::sort( j_list.begin(),j_list.end(),MyComparator );
std::for_each(j_list.begin(),j_list.end(),printtest<point*>);
std::cout<<std::endl;
system("pause");
return 0;
} 展开
大家帮帮忙看下是怎么回事,谢谢了!
代码如下:
#include <list>
#include <iostream>
#include <functional>
#include <algorithm>
class point
{
public:
int x;
int y;
point(int x,int y);
~point();
};
point::point(int x,int y)
:x(x),y(y)
{}
point::~point()
{}
template<typename T>
void printtest(T i)
{
std::cout<<(i->x)<<","<<(i->y)<<" ";
}
bool MyComparator(const point* a,const point* b)
{
return ((a->x) >(b->x))?true:false;
}
int main()
{
std::list<point*> j_list;
point p1(0,1);
point p2(9,2);
point p3(0,1);
point p4(8,2);
point p5(0,1);
point p6(0,2);
point p7(56,1);
point p8(0,2);
point* pp1=&p1;
point* pp2=&p2;
point* pp3=&p3;
point* pp4=&p4;
point* pp5=&p5;
point* pp6=&p6;
point* pp7=&p7;
point* pp8=&p8;
j_list.push_back(pp1);
j_list.push_back(pp2);
j_list.push_back(pp3);
j_list.push_back(pp4);
j_list.push_back(pp5);
j_list.push_back(pp6);
j_list.push_back(pp7);
j_list.push_back(pp8);
j_list.remove(pp1);
std::for_each(j_list.begin(),j_list.end(),printtest<point*>);
std::cout<<std::endl;
std::sort( j_list.begin(),j_list.end(),MyComparator );
std::for_each(j_list.begin(),j_list.end(),printtest<point*>);
std::cout<<std::endl;
system("pause");
return 0;
} 展开
展开全部
因为sort需要++,--这样的操作,而list容器不具备这个。
所以你只需要把用到list的地方替换成vector即可。
用erase( remove() );这种方式代替list::remove();
return (a->x) >(b->x) ;
这里这样即可,你不觉得你的写法有点多余吗?
#include <vector>
#include <iostream>
#include <functional>
#include <algorithm>
class point
{
public:
int x;
int y;
point(int x,int y);
~point();
};
point::point(int x,int y)
:x(x),y(y)
{}
point::~point()
{}
template<typename T>
void printtest(T i)
{
std::cout<<( i->x)<<","<<(i->y)<<" ";
}
bool MyComparator(const point* a,const point* b)
{
return (a->x) >(b->x) ;
}
int main()
{
std::vector<point*> j_vector;
point p1(0,1);
point p2(9,2);
point p3(0,1);
point p4(8,2);
point p5(0,1);
point p6(0,2);
point p7(56,1);
point p8(0,2);
point* pp1=&p1;
point* pp2=&p2;
point* pp3=&p3;
point* pp4=&p4;
point* pp5=&p5;
point* pp6=&p6;
point* pp7=&p7;
point* pp8=&p8;
j_vector.push_back(pp1);
j_vector.push_back(pp2);
j_vector.push_back(pp3);
j_vector.push_back(pp4);
j_vector.push_back(pp5);
j_vector.push_back(pp6);
j_vector.push_back(pp7);
j_vector.push_back(pp8);
std::for_each(j_vector.begin(),j_vector.end(),printtest<point*>);
std::cout<<std::endl;
std::sort( j_vector.begin(),j_vector.end(),MyComparator );
std::for_each(j_vector.begin(),j_vector.end(),printtest<point*>);
std::cout<<std::endl;
system("pause");
return 0;
}
所以你只需要把用到list的地方替换成vector即可。
用erase( remove() );这种方式代替list::remove();
return (a->x) >(b->x) ;
这里这样即可,你不觉得你的写法有点多余吗?
#include <vector>
#include <iostream>
#include <functional>
#include <algorithm>
class point
{
public:
int x;
int y;
point(int x,int y);
~point();
};
point::point(int x,int y)
:x(x),y(y)
{}
point::~point()
{}
template<typename T>
void printtest(T i)
{
std::cout<<( i->x)<<","<<(i->y)<<" ";
}
bool MyComparator(const point* a,const point* b)
{
return (a->x) >(b->x) ;
}
int main()
{
std::vector<point*> j_vector;
point p1(0,1);
point p2(9,2);
point p3(0,1);
point p4(8,2);
point p5(0,1);
point p6(0,2);
point p7(56,1);
point p8(0,2);
point* pp1=&p1;
point* pp2=&p2;
point* pp3=&p3;
point* pp4=&p4;
point* pp5=&p5;
point* pp6=&p6;
point* pp7=&p7;
point* pp8=&p8;
j_vector.push_back(pp1);
j_vector.push_back(pp2);
j_vector.push_back(pp3);
j_vector.push_back(pp4);
j_vector.push_back(pp5);
j_vector.push_back(pp6);
j_vector.push_back(pp7);
j_vector.push_back(pp8);
std::for_each(j_vector.begin(),j_vector.end(),printtest<point*>);
std::cout<<std::endl;
std::sort( j_vector.begin(),j_vector.end(),MyComparator );
std::for_each(j_vector.begin(),j_vector.end(),printtest<point*>);
std::cout<<std::endl;
system("pause");
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询