关于C与C++效率的问题

在九度OJ中,1061试题上,分别用C和C++风格来实现了一遍。

下面贴出代码:

C风格:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct student{
       char name[110];
       int age;
       int score;
       bool operator < ( const student & s)  const {
                 if (score!=s.score)    return score<s.score;
                 else if ( strcmp (name,s.name)!=0)   return   strcmp (name,s.name)<0;
                 else if (age!=s.age)    return  age<s.age;
         }
 
}buf[1010];
 
 
int main(){
           int n;
           while ( scanf ( "%d" ,&n)!=EOF){
                   for ( int i=0;i<n;i++){
                           scanf ( "%s%d%d" ,buf[i].name,&buf[i].age,&buf[i].score);
                     }
                    sort(buf,buf+n);
                    for ( int i=0;i<n;i++){
                            printf ( "%s %d %d\n" ,buf[i].name,buf[i].age,buf[i].score);
 
 
                    }
 
            }
 
 
         return 0;
 
 
}
C++风格:

#include <iostream>
#include <algorithm>
#include <string>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;

class stu{
    private:
        string name;
        int age;
        int score;
    public:
        bool operator < (const stu &s)const{
            if(score != s.score) return score < s.score;
            else{
                if(name != s.name) return name < s.name;
                else return age < s.age;
            }
        }
        void init(string n,int a,int s);
        stu();
        void display();
};
stu::stu(){
}
void stu::init(string n,int a,int s){
    name = n;
    age = a;
    score = s;
}
void stu::display(){
    cout << name <<" " << age << " " << score << endl;
}

int main(int argc, char** argv) {
    int n;
    string name;
    int age;
    int score;
    stu buf[1010];
    //int count;
    while( cin >> n ){
        //count = n;
        //while(count--){
            for(int i=0; i<n ; i++){
                cin >> name >> age >> score;
                buf[i].init(name,age,score);
            }
            sort(buf , buf+n);
            for(int i=0; i<n ; i++){
                buf[i].display();
            }
        //}
    }
    
    
    return 0;
}

当然了,在C的实现中也使用了一部分STL库中的内容,但是使用的都是C的过程的思想。

但是执行效率查了好多,运行的结果中,利用C实现的都是60MS,而C++的实现上两次都超出时间限制,另外两次为630MS,790MS。可以看出来,效率相差10倍以上。

另外,在C++风格的实现上,在利用类来定义数组的时候,首先进行构造函数的定义。当然,可以什么都不说,直接写个空函数,如果不写会导致无法定义buf[]数组。但是还要对私有成员进行赋值。但是不能写成构造函数的形式,因为构造函数的形式要求在定义的时候进行赋值,但是数组的赋值不能边赋值边定义。因为数组只能定义一次,所以,要写一个init()函数对它进行赋值。

除了上边提到的,还要注意sort函数。sort一共包含三个参数,起始指针,结尾指针和符号的定义。我采用了重载<的方法。在重载的过程中一共用到了两个const。我将对这两个进行分析。因为最开始没有写,出错了大哭

没研究明白,困了,先睡了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值