64,646
社区成员
发帖
与我相关
我的任务
分享
struct average_descending_comp_t
{
bool operator () (student const* x, student const* y) const
{
return x->average > y->average;
}
};
#include <algorithm>
void student::Sort ()
{
average_descending_comp_t average_descending_comp;
std::sort(&s[0],&s[0]+50,average_descending_comp);
}
另外你得把 class student 的 average 数据成员改成公有的,或者提供相应的访问接口。否则那个比较函数还是会出编译错误的。[/quote]
把 class student 的 average 数据成员改成公有的这样不就违反了面向对象的规则了吗?
我试过了 编译是没错 但是一调用void student::Sort ()这个函数的时候 程序直接崩溃了
#include <algorithm>
void student::Sort()
{
struct
{
bool operator () (student const* x, student const* y) const
{
return x->average > y->average;
}
} average_descending_comp;
std::sort(&s[0],&s[0]+50,average_descending_comp);
}
[/quote]
我有点懂你的意思了 但是编译还是错
error C2627: member function defined in unnamed class
error C2919: illegal use of anonymous local type in template instantiation
error C2780: 'void __cdecl std::sort(_RI,_RI)' : expects 2 arguments - 3 provided
\vc98\vc98\include\algorithm(538) : see declaration of 'sort'
我是用vc的[/quote]
vc6 吧,都老掉牙了,对学 c++ 有害无益。貌似这家伙不喜欢局部匿名类对象做模板参数。再试一下这个吧。
struct average_descending_comp_t
{
bool operator () (student const* x, student const* y) const
{
return x->average > y->average;
}
};
#include <algorithm>
void student::Sort ()
{
average_descending_comp_t average_descending_comp;
std::sort(&s[0],&s[0]+50,average_descending_comp);
}
另外你得把 class student 的 average 数据成员改成公有的,或者提供相应的访问接口。否则那个比较函数还是会出编译错误的。//假设带表头结点的单向链表头指针为head,试编写一个算法将值为5的结点插入到连接表的第k个结点前,并对该链表进行排序。
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
struct NODE {
int data;
struct NODE *next;
} H,*head,*p,*q;
int i,j,k,n,t;
int main() {
srand(time(NULL));
//填写头节点数据
H.data=-1;
H.next=NULL;
head=&H;
//创建10个节点的单链表
p=head;
for (i=0;i<10;i++) {
q=(struct NODE *)malloc(sizeof(struct NODE));
if (NULL==q) return 1;
q->data=10+rand()%90;//填写10..99的随机值
q->next=NULL;
p->next=q;
p=q;
}
//输出整个单链表
p=head->next;
while (1) {
if (NULL==p) {
printf("\n");
break;
}
printf("%02d->",p->data);
p=p->next;
}
//将值为5的结点插入到单链表的第k个结点前
k=3;
n=0;
p=head;
while (1) {
if (NULL==p) {
break;
}
n++;
if (k==n) {
q=(struct NODE *)malloc(sizeof(struct NODE));
if (NULL==q) return 1;
q->data=5;
q->next=p->next;
p->next=q;
break;
}
p=p->next;
}
//输出整个单链表
p=head->next;
while (1) {
if (NULL==p) {
printf("\n");
break;
}
printf("%02d->",p->data);
p=p->next;
}
//从小到大排序
for (p=head->next;p!=NULL && p->next!=NULL;p=p->next) {
for (q=p->next;q!=NULL;q=q->next) {
if (p->data > q->data) {
t=p->data;p->data=q->data;q->data=t;
}
}
}
//输出整个单链表
p=head->next;
while (1) {
if (NULL==p) {
printf("\n");
break;
}
printf("%02d->",p->data);
p=p->next;
}
//释放所有节点
p=head->next;
while (1) {
if (NULL==p) {
break;
}
q=p->next;
free(p);
p=q;
}
return 0;
}
//20->75->80->82->78->33->54->63->10->99->
//20->75->05->80->82->78->33->54->63->10->99->
//05->10->20->33->54->63->75->78->80->82->99->
#include <algorithm>
void student::Sort()
{
struct
{
bool operator () (student const* x, student const* y) const
{
return x->average > y->average;
}
} average_descending_comp;
std::sort(&s[0],&s[0]+50,average_descending_comp);
}
[/quote]
我有点懂你的意思了 但是编译还是错
error C2627: member function defined in unnamed class
error C2919: illegal use of anonymous local type in template instantiation
error C2780: 'void __cdecl std::sort(_RI,_RI)' : expects 2 arguments - 3 provided
\vc98\vc98\include\algorithm(538) : see declaration of 'sort'
我是用vc的