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)

望高手指点。
...全文
521 16 打赏 收藏 转发到动态 举报
写回复
用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
  • 打赏
  • 举报
回复
关注:
1,01.zip
Dialogs in DLL
在DLL实现对话框(5KB)
2,02.zip
Export dialogs in MFC Extension DLLs
在MFC扩充DLL输出对话框(12KB)
3,03.zip
Remapping resource script ID's
重影象资源的标识符(4KB)
4,04.zip
Determine DLL version number
检测DLL的版本号(7KB)
5,05.zip
Getting the complete information about DLL/Exe module
得到DLL/EXE模块的编译信息(5KB)
6,06.zip
Using one extension DLL in another
在DLL使用扩充的DLL(4KB)
7,07.zip
Handling VB strings (as part of an array of UDT)
VB的串句柄(5KB)
8,08.zip
Class for Dynamic DLL Loading
动态装入DLL的一个类(6KB)
9,pop3.zip
CPop3Connection - an MFC Class to encapsulate the POP3 protocol(20KB)
10,ipenum.zip
IPEnum - an MFC class and console app to allow IP address enumeration
(11KB)
11,smtp.zip
CSMTPConnection - an MFC Class to encapsulate the SMTP protocol(69KB)
12,ping.zip
CPing - an MFC class to encapsulate the PING protocol(13KB)
13,mailslot.zip
CServerMailslot & CClientMailslot - 2 MFC classes to support Win32 mailslots(29KB)
14,rasman.zip
A shareware application to monitor your Dial-Up Networking Connections.
(49KB)
15,rasmonitor.zip
The MFC class to monitor connections as used by RasMan.(17KB)
16,npipe.zip
A Freeware MFC class to encapsulate Named Pipes(24KB)
17,apipe.zip
A Freeware MFC class to encapsulate(8KB)
18,csntp.zip
A Freeware MFC class to encapsulate the SNTP protocol(18KB)
19,tracer.zip
an MFC class to encapsulate trace route functionality(13KB)
20,popwatch.zip
A freeware application to monitor your POP3 mailbox(72KB)
21,w3mfc.zip
A collection of freeware MFC classes to implement a Web Server(34KB)
22,mfccddb.zip
A freeware MFC class to support access to CDDB servers(39KB)
23,cmapi.zip
an MFC class to encapsulate sending mail using Simple MAPI(21KB)
24,finger.zip
An MFC class to encapsulate the "Finger" protocol(26KB)
25,eqd.zip
A Freeware MFC class to support retrieval of recent Earthquake data from the USGS(10KB)
26,httpdownloaddlg.zip
The class implements an MFC CDialog derived class which performs HTTP downloads similar to the Internet Explorer(43KB)
27,ccmc.zip
an MFC class to encapsulate sending mail using CMC(19KB)
28,ftptransferdlg.zip
CFTPTransferDlg provides an MFC dialog which performs FTP uploads and downloads similar to the Internet Explorer(43KB)
29,memmap.zip
A freeware MFC class to encapsulate Memory Mapped Files(18KB)
30,pstat.zip
A Freeware generalized framework for executing a lengthy operation in a thread. (20KB)
31,cfile64.zip
A freeware MFC class to encapsulate the Win32 64 bit file system API(16KB)
32,serv.zip
A class framework for developing NT services in MFC(53KB)
33,shelllink.zip
2 Freeware MFC classes to encapsulate shell shortcuts(12KB)
34,dyndata.zip
A collection of freeware MFC classes to encapsulate the Windows 95/98 performance counters.(10KB)
35,cpdh.zip
A collection of freeware MFC classes to encapsulate the NT Performance Counters.(25KB)
36,serialport.zip
A freeware MFC class for Win32 serial ports(19KB)
37,cpl_pp.zip
A freeware MFC class framework for developing Control Panel Applets(14KB)
38,parallelport.zip
An MFC class to control parallel ports on 95, 98, ME, NT and 2000(16KB)
39,versioninfo.zip
An MFC class to encapsulate the Windows Version API(14KB)
40,waitabletimer.zip
A freeware MFC class for Win32 waitable timers(13KB)
41,vfwwnd.zip
CAVICapWnd - AN MFC wrapper class for Video For WIndows(47KB)
42,fraction.zip
a double / fraction / string conversion class(5KB)
43,cmd5.zip
A C++ Message Digest 5 Class(13KB)
44,cryptit.zip
Keep sensitive data safe via encryption (130KB)
45,gener1.zip
Template functions for serializing arbitrary linked nodes.(25KB)
46,gener2.zip
Template functions for serializing arbitrary linked nodes. (3KB)
47,zip1.zip
The library to create, modify and extract zip archives(92KB)
48,zip2.zip
The library to create, modify and extract zip archives (31KB)
49,pointers.zip
A Beginner's Guide to Pointers
An article showing the use of pointers in C and C++ (4KB)
50,linkedlist_demo.zip
An article showing the basics of the linked list, and how the CList class operates (7KB)
51,ocarray_demo.zip
A simple derived template class that can boost the efficiency of your programs. (18KB)
52,clist_iter_src.zip
A simple iteration class for MFC's CList linked list (2KB)
53,dynopenhashtable_src.zip
Making assorted hash table of strings and/or other data types. (4KB)
54,extcol_demo.zip
Extended Collection classes to provide copy, compare and find operations with 2 dimensional arrays and maps (24KB)
55,extcol_src.zip
Extended Collection classes to provide copy, compare and find operations with 2 dimensional arrays and maps (11KB)
56,lookaside_src.zip
A simple way to keep items such as COM instances 'warm' and available for reuse (3KB)
57,isarray_src.zip
A simple templated array class. (2KB)
58,collectionnotes.zip
An article describing MFC Collection Classes (9KB)
59,qarray_demo.zip
A CArray derived class that provides quick and convenient sorting (13kb)
60,qarray_src.zip
A CArray derived class that provides quick and convenient sorting (2KB)
61,smartlist.zip
Wrapper classes for MFC list classes the extend their functionality (4KB)
62,ufstmaps.zip
A fully featured map class that uses balanced trees to store and retrieve data quickly by key (558KB)
63,qsort_demo.zip
An introduction to a useful function (15KB)
64,seexception_demo.zip
This article describes how to handle SE and C++ exception together(16KB)
65,seexception_src.zip
This article describes how to handle SE and C++ exception together(2KB)
66,circular_buffer_demo.zip
A circular, thread-safe read/write character buffer (12KB)
67,avltree_demo.zip
Describes an implementation of AVL Trees. (54KB)
68,metaclass_demo.zip
A class that can be modified at run-time (6KB)
69,metaclass_src.zip
A class that can be modified at run-time (4KB)
70,arrayex_src.zip
This article presents a callback based, QuickSort enabled CArray template class (2KB)
71,arrayex_demo.zip
This article presents a callback based, QuickSort enabled CArray template class (28KB)
72,chookwnd_src.zip
A freeware MFC class to support MFC subclassing (19KB)
73,cinifile_demo.zip
A class that makes it easy to implement an INI settings file in your applications(14KB)
74,cinifile_src.zip
A class that makes it easy to implement an INI settings file in your applications(4KB)
75,cint96_src.zip
A Freeware MFC class which provides 96 bit integers. (10KB)
76,csingleinst_src.zip
An MFC class to implement single instance apps. (9KB)
77,enitl.zip
A cross platform scripting engine for server applications providing HTML, XML, SGML or other text based formats (38KB)
78,floatutils_src.zip
A set of floating point utilities (3KB)
79,switch_languages.zip
Multilingual Application - Change Application Language(33KB)
80,duration_demo.zip
A simple class that provides high precision timing. (3KB)
81,duration_src.zip
A simple class that provides high precision timing. (1KB)
82,save_temp_var.zip
A safe, and convenient way to store variables temporarily (1KB)
83,tcxunitconverter_demo.zip
TCX Unit Conversion Library(18KB)
84,tcxunitconverter_src.zip
TCX Unit Conversion Library(7KB)
85,templates_demo.zip
Templates are a great way of reusing code, unfortunately MFC
makes it hard to write MFC friendly template classes... (124KB)
86,templates_src.zip
Templates are a great way of reusing code, unfortunately MFC
makes it hard to write MFC friendly template classes... (2KB)
87,functionparser.zip
A simple yet powerful function parser that parses and evaluates standard mathematical functions (82KB)
88,stlxmlparser.zip
This is a small non-validating XML parser based purely on STL (10KB)
89,xmimabparser_src.zip
A class to read and write non validated XML files (178KB)
90,rexsearch_src.zip
Compiles a regular expression into a fast automaton (11KB)
91,rexsearch_demo.zip
Compiles a regular expression into a fast automaton (50KB)
92,sascript.zip
A simple stack-based language that you can easily add to your projects (52KB)
93,strtok_src.zip
A customizable string tokenizer (23KB)
94,ismart_demo.zip
A template-based smart pointer implementation(19KB)
95,ismart_src.zip
A template-based smart pointer implementation(2KB)
96,smartptr.zip
A smart pointer wrapper class(4KB)
97,blockallocator.zip
A block allocator for use with STL containers that greatly improves speed in programs doing massive data insertions and extractions(6KB)
98,stltools.zip
Defines some TCHAR compatible STL elements and gives you an std::ostream to send output to the debugger windows. (4KB)
99,tokeniterator.zip
Token Iterator provides an easy to use, familiar, and customizable way in which to go through the tokens contained in a string (7KB)
100,stdsort.zip
An introduction to sorting using STL(4KB)
101,clniex_src.zip
Class CIniEx carries out extended set of ini-files functions in memory (106KB)
102,dtconverter.zip
A simple app that converts to and between time_t, DATE, and regular date string expressions(17KB)
103,dumphandle_demo.zip
A method of getting more details about application crashes.(24KB)
104,libdump.zip
A tool to display the contents of a library file (383KB)
105,vbactivexwithvc.zip
A simple way to call a VB ActiveX DLL from a VC/MFC Client(24KB)
106,staticlink.zip
VC++ Standard Edition only has support for dynamically linked exes. This article shows you how to by-pass this restriction.(14KB)
107,displayloadedmodules.zip
A Debugging Tool for Application using Multiple DLLs (246KB)
108,dynamicdiiloading_demo.zip
How to dynamically load a DLL (11KB)
109,hookimport_src.zip
A class to hook any imported function call made by your app.(7KB)
110,plug-in_demo.zip
Extending the functionality of your programs using explicit linking (60KB)
111,data_seg_share_demo.zip
Using #pragma statements to share variables in a DLL(37KB)
112,niftyloadlib_src.zip
The home of NiftyLoadLibrary - and some notes on rebasing dlls(3KB)
113,rcremap.zip
Remapping resource script ID's (21KB)
114,get_info.zip
Getting the complete information about DLL/Exe module (29KB)
115,vbstring.zip
Handling VB strings (as part of an array of UDT) (5KB)

64,282

社区成员

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

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