计算机上级复试资料

9. 入门并实践STL——algorithm篇

2019-03-06  本文已影响0人  zju_dream

1. How to use?

#include<algorithm>
using namespace std;

2. 常用函数解析

1. max(x, y), min(x, y), abs(i)

2. swap(x, y)

3. reverse(it, it2)

4. next_permutation()

int a[4] = {1, 2, 3};
do {
    printf("%d%d%d\n", a[0], a[1], a[2]);
} while(next_permutation(a, a+3);

5. fill()

6. sort()

bool cmp(int a, int b) {
    return a > b; // 可以理解为当a>b时把a放在b前面
}
- 记忆方法:如果要把数据从小到大排列,那么就用<,因为a<b就是左小右大
- 容器的排序,想set,map这种容器是用,元素本身有序,红黑树实现的,元素本身有序,故不允许使用sort排序。

7. lower_bound()upper_bound()

习题

题目

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main() {
    string str;
    while(cin>>str) {
        do{
            cout<<str<<endl;
        } while(next_permutation(str.begin(), str.end()));
        cout<<endl;
    }
    return 0;
}
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main() {
    string str;
    while(getline(cin,str)) {
        reverse(str.begin(), str.end());
        cout<<str<<endl;
    }
    return 0;
}
上一篇 下一篇

猜你喜欢

热点阅读