北大程序设计与算法一 (c语言程序设计)mooc课笔记合集

目录:

  1.stl初步

  2.poj须知

  3.qsort:(c标准库中的排序算法)

 

 

正文内容:

1:stl初步

 

我的实践:

当想把两个用数组存放的字符串利用sort函数排序时,

#include <iostream>
#include <cstring>
#include <cmath>
#include <stdio.h>
#include <algorithm>
using namespace std;

struct rule
{
    bool operator()(const char & a1,const char & a2)//总之吧,这里的类型该怎么写,我不知道。试了char * 等等多个都报错。
    {
        return strcmp((char *)a1,(char* )a2)<0;
    }
};

int main()
{
    char a[2][10];
    cin>>a[0]>>a[1];
    sort(a,a+2,rule());
    cout<<a[0]<<" "<<a[1];
    return 0;
} 

 

但通过如下把字符串存在结构体中可以解决。

#include <iostream>
#include <cstring>
#include <cmath>
#include <stdio.h>
#include <algorithm>
using namespace std;


struct xx
{
    char a[10];
};
struct rule
{
    bool operator()(const xx& a1,const xx& a2)
    {
        return strcmp(a1.a,a2.a)<0;
    }
};

int main()
{
    xx a[2];
    cin>>a[0].a>>a[1].a;
    sort(a,a+2,rule());
    cout<<a[0].a<<" "<<a[1].a;
    return 0;
} 

 

 

 

 

 

 

 

 

 

 

2:poj须知。

 

 

 

3.qsort:https://www.runoob.com/cprogramming/c-function-qsort.html

 

posted @ 2019-07-22 21:29  奇婆子  阅读(397)  评论(0编辑  收藏  举报