程序设计与算法(一)C语言程序设计

第十一周(笔记)

11.1 STL初步

  1. STL概述
    • STL:(Standard Template Library) 标准模板库
    • 包含一些常用的算法如排序查找,还有常用的数据结构如可变长数组、链表、字典等。
    • 使用方便,效率较高
    • 要使用其中的算法,需要**#include < algorithm >**
  2. 用sort进行排序(用法一)
    • 对基本类型的数组从小到大排序sort(数组名+n1,数组名+n2);
      • n1和n2都是int类型的表达式,可以包含变量
      • 如果n1 = 0,则+n1可以不写
    • 将数组中下标范围为[n1,n2)的元素从小到大排序。下标为n2的元素不在排序区间内
//用法一
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int a[] = {15,4,3,9,7,2,6};
    //sort(a,a+7);
    //sort(a,a+3);
    sort(a+2,a+5);
    int len = sizeof(a)/sizeof(int);
    cout << len <<endl;
    int i =0;
    while (i<len) {
        cout << a[i];
        i++;
    }
    return 0;
}
  1. 用sort进行排序(用法二)
    • 对元素类型为T的基本类型数组从大到小排序sort(数组名+n1,数组名+n2,greater< T >());
//用法二(对元素类型为T的基本类型数组从大到小排序)
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
    int a[] = {15, 4, 3, 9, 7, 2, 6};
    sort(a + 1, a + 4, greater<int>());
    int len = sizeof(a) / sizeof(int);
    cout << len << endl;
    int i = 0;
    while (i < len) {
        cout << a[i];
        i++;
    }
    return 0;
}
  1. 用sort进行排序(用法三)
    • 自定义的排序规则,对任何类型T的数组排序:sort(数组名+n1,数组名+n2,排序规则结构名());
    • 排序规则定义方式
struct 结构名 {
    bool operator() (const T & a1,const T & a2) const {
        // 若a1应该在a2的前面,则返回true。
        //否则返回false
    }
}
//用法三(自定义排序规则)
#include <algorithm>
#include <iostream>
using namespace std;
struct Rule1 {
    //按从大到小排序
    bool operator()(const int& a1, const int& a2) const { return a1 > a2; }
};
struct Rule2 {
    //按个位数从小到大排序
    bool operator()(const int& a1, const int& a2) const {
        return a1 % 10 < a2 % 10;
    }
};
void Print(int a[], int size) {
    for (int i = 0; i < size; i++) cout << a[i] << ",";
    cout << endl;
}
int main() {
    int a[] = {12, 45, 3, 9, 8, 21, 7};
    sort(a, a + sizeof(a) / sizeof(int));  //从小到大
    cout << "1) ";
    Print(a, sizeof(a) / sizeof(int));
    sort(a, a + sizeof(a) / sizeof(int), Rule1());  //从大到小
    cout << "2) ";
    Print(a, sizeof(a) / sizeof(int));
    sort(a, a + sizeof(a) / sizeof(int), Rule2());  //按个位数从小到大排序
    cout << "3) ";
    Print(a, sizeof(a) / sizeof(int));
    return 0;
}
  1. 用sort对结构数组进行排序
//自定义sort对结构体排序
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
struct Student {
    char name[20];
    int id;
    double gpa;
};
Student students[] = {{"jack", 112, 3.4},
                      {"mary", 102, 3.8},
                      {"alaa", 333, 3.5},
                      {"zero", 101, 4.0}};
struct StudentRule1 {
    //按姓名从小到大排
    bool operator()(const Student& s1, const Student& s2) const {
        if (stricmp(s1.name, s2.name) < 0) return true;
        return false;
    }
};
struct StudentRule2 {
    //按id从小到大排
    bool operator()(const Student& s1, const Student& s2) const {
        return s1.id < s2.id;
    }
};
struct StudentRule3 {
    //按gpa从高到低排
    bool operator()(const Student& s1, const Student& s2) const {
        return s1.gpa > s2.gpa;
    }
};
void PrintStudents(Student s[], int size) {
    for (int i = 0; i < size; i++)
        cout << "(" << s[i].name << "," << s[i].id << "," << s[i].gpa << ") ";
    cout << endl;
}
int main() {
    int n = sizeof(students) / sizeof(Student);
    sort(students, students + n, StudentRule1());  //按姓名从小到大排
    PrintStudents(students, n);
    sort(students, students + n, StudentRule2());  //按id从小到大排
    PrintStudents(students, n);
    sort(students, students + n, StudentRule3());  //按gpa从高到低排
    PrintStudents(students, n);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值