VC6中stl的list.sort问题

someone 2005-08-23 04:18:34
#include <iostream>
using std::cin;
using std::cout;
using std::cerr;
using std::endl;


#include <list>
using std::list;

#include <string>
using std::string;

#include <ctime>

class Person
{
public:
string name;
int age;

Person( string name, int age )
{
this->name = name;
this->age = age;
}
void PrintInfo()
{
cout << " " << name << "\t" << age << endl;
}
};

bool CompareAge( Person &p1, Person &p2 )
{
return p1.age < p2.age;
}

bool CompareName( Person &p1, Person &p2 )
{
return p1.name < p2.name;
}

void PrintPersonsInfo( list<Person> & listOut )
{
std::list<Person>::iterator li;
for ( li = listOut.begin(); li != listOut.end(); li++ )
{
((Person)*li).PrintInfo();
}
cout << endl;
}
int main(int argc, char *argv[])
{
list<Person> students;

students.push_back( Person("zhao", rand() % 20 + 5) );
students.push_back( Person("qian", rand() % 20 + 5) );
students.push_back( Person("sun", rand() % 20 + 5) );
students.push_back( Person("li", rand() % 20 + 5) );
students.push_back( Person("zhou", rand() % 20 + 5) );
students.push_back( Person("wu", rand() % 20 + 5) );
students.push_back( Person("zhen", rand() % 20 + 5) );
students.push_back( Person("wang", rand() % 20 + 5) );

cout << "the students: " << endl;
PrintPersonsInfo( students );
//for_each( students.begin(), students.end(), PrintStudentInfo );

cout << "sort of age: " << endl;
students.sort( CompareAge );
PrintPersonsInfo( students );

cout << "sort of name: " << endl;
students.sort( CompareName );
PrintPersonsInfo( students );

return 0;
}

上面这段代码,用MinGW编译通过,执行正常,但将代码拷到VC6中却不能编译,提示如下:
Compiling...
ListSort.cpp
d:\golden\src\listsort\listsort.cpp(70) : error C2664: 'void __thiscall std::list<class Person,class std::allocator<class Person> >::sort(struct std::greater<class Person>)' : cannot convert parameter 1 from 'bool (class Person &,class Person &)' to
'struct std::greater<class Person>'
No constructor could take the source type, or constructor overload resolution was ambiguous
d:\golden\src\listsort\listsort.cpp(74) : error C2664: 'void __thiscall std::list<class Person,class std::allocator<class Person> >::sort(struct std::greater<class Person>)' : cannot convert parameter 1 from 'bool (class Person &,class Person &)' to
'struct std::greater<class Person>'
No constructor could take the source type, or constructor overload resolution was ambiguous
Error executing xicl6.exe.

ListSort.exe - 2 error(s), 0 warning(s)

望高手指点。
...全文
535 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
李秀国之印 2006-04-06
  • 打赏
  • 举报
回复
mark
someone 2005-09-06
  • 打赏
  • 举报
回复
如何能既可用age排序,同时也可用name排序,在VC中达到第一个代码在MinGW的效果。
foochow(恰似你的温柔)及其他高手能再指点一下吗,多谢!
someone 2005-09-01
  • 打赏
  • 举报
回复
?
someone 2005-08-29
  • 打赏
  • 举报
回复
多谢foochow(恰似你的温柔)的多次回复

只通过age排序的话在前面我的回复中通过修改了class Person,在其中重载<的方式已经解决,多谢你又告诉我一种方式。
还有一个问题就是这样的话只能对一个关键字排序,如果我既想用age排序,也要用name排序,在VC6中能解决吗,如何解决?
用MinGW能够编译过去,在VC6中换了STL库,改用STLport-4.6.2也不能支持模板成员函数吗
foochow 2005-08-29
  • 打赏
  • 举报
回复
#include <iostream>
using std::cin;
using std::cout;
using std::cerr;
using std::endl;


#include <list>
using std::list;

#include <string>
using std::string;
#include<functional>
#include <ctime>

class Person
{
public:
string name;
int age;

Person( string name, int age )
{
this->name = name;
this->age = age;
}
friend bool operator<(const Person &p1, const Person &p2 );
void PrintInfo()
{
cout << " " << name << "\t" << age << endl;
}
};

bool operator>(const Person &p1,const Person &p2 )
{
return p1.age >p2.age;
}

bool CompareName( Person &p1, Person &p2 )
{
return p1.name < p2.name;
}

void PrintPersonsInfo( list<Person> & listOut )
{
std::list<Person>::iterator li;
for ( li = listOut.begin(); li != listOut.end(); li++ )
{
((Person)*li).PrintInfo();
}
cout << endl;
}
int main(int argc, char *argv[])
{
list<Person> students;

students.push_back( Person("zhao", rand() % 20 + 5) );
students.push_back( Person("qian", rand() % 20 + 5) );
students.push_back( Person("sun", rand() % 20 + 5) );
students.push_back( Person("li", rand() % 20 + 5) );
students.push_back( Person("zhou", rand() % 20 + 5) );
students.push_back( Person("wu", rand() % 20 + 5) );
students.push_back( Person("zhen", rand() % 20 + 5) );
students.push_back( Person("wang", rand() % 20 + 5) );

cout << "the students: " << endl;
PrintPersonsInfo( students );
//for_each( students.begin(), students.end(), PrintStudentInfo );

cout << "sort of age: " << endl;
std::greater<Person>temp;
students.sort( temp );
PrintPersonsInfo( students );

/*cout << "sort of name: " << endl;
students.sort( CompareName );
PrintPersonsInfo( students );*/

return 0;
}
someone 2005-08-29
  • 打赏
  • 举报
回复
下载了STLport-4.6.2,那个程序在VC6中还是不能编译。
还有高手可以指点一下吗?
someone 2005-08-24
  • 打赏
  • 举报
回复
VC6中就只能重载<了吗?按多种方式排序该如何解决呢?
xinxiakk 2005-08-23
  • 打赏
  • 举报
回复
再次看到了VC6的一个bug,下决心换编译器了
someone 2005-08-23
  • 打赏
  • 举报
回复
VC6中如何修改?
huzhangyou 2005-08-23
  • 打赏
  • 举报
回复
//---------------------------------------------------------------------------
// Unit3
//---------------------------------------------------------------------------
#if !defined(Person_H)
#define Person_H

#include <string>
using namespace std;

class Person{
private:
string name;
int age;
public:
Person(string _name,int _age);
Person();
string getName();
int getAge();

};


bool CompareAge(Person &p1,Person &p2){
return p1.getAge() < p2.getAge();
}

#endif // Unit3_H


//---------------------------------------------------------------------------
// Unit3.cpp
//---------------------------------------------------------------------------
#include "Person.h"

Person::Person(string _name,int _age){
name = _name;
age = _age;
}

Person::Person(){
name = "zhao";
age = 15;
}

string Person::getName(){
return name;
}

int Person::getAge(){
return age;
}
#include <iostream>
#include <list>
#include <string>
#include "main.h"

#include "Person.h"


using namespace std;
int main(int /*argc*/, char* /*argv*/[])
{
list<Person> listPerson;
listPerson.push_back(Person("zhao",20));
listPerson.push_back(Person("qian",21));
listPerson.push_back(Person("sun",19));
listPerson.push_back(Person("li",17));

listPerson.sort(CompareAge);
list<Person>::iterator iter;
for(iter = listPerson.begin();iter != listPerson.end();++iter){
cout << (*iter).getAge() << (*iter).getName() << endl;
}
cin.get();
cout << "Process successful quit!"<< endl;

能够通过

使用
C++builderX编译环境
输出结果为
D:\CBuilderX\samples\welcome\windows\Release_Build\Welcome.exe
17li

19sun

20zhao

21qian
someone 2005-08-23
  • 打赏
  • 举报
回复
谢谢,请问应当如何修改代码使其在VC6中运行呢?

我修改了class Person,在其中重载了<:
class Person
{
public:
string name;
int age;

Person( string name, int age )
{
this->name = name;
this->age = age;
}
void PrintInfo()
{
cout << " " << name << "\t" << age << endl;
}
bool operator< (const Person &p) const
{
return age < p.age;
}
};

然后在主程序中作如下调用:
cout << "sort of age: " << endl;
students.sort( );
PrintPersonsInfo( students );
程序通过。
但这样的话就只能按age排序了,如果我也想按name排序,该如何修改呢?
foochow 2005-08-23
  • 打赏
  • 举报
回复
换编译器,如VC7.0......
要不然你自己再写一个greater<T>结构对特定的类的比较作处理,然后把greater作为参数调用list.sort就可以了
foochow 2005-08-23
  • 打赏
  • 举报
回复
C++标准是这样定义的:
class list {
...
template<class Pr3>
void sort(Pr3 pred);
...
}

VC6.0是这样定义的
class list {
...
typedef greater<_Ty> _Pr3;
void sort(_Pr3 _Pr)
...
}

你的代码没有错误,早期的VC6不支持模板成员函数.........
someone 2005-08-23
  • 打赏
  • 举报
回复
打过Vs6sp6.exe
healer_kx 2005-08-23
  • 打赏
  • 举报
回复
你的VC6打了补丁嘛?
hqhhh 2005-08-23
  • 打赏
  • 举报
回复
关注:

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧