【持续更新】实用算法小点总结(怕忘QAQ)

版权声明:转载请注明出处哦~ https://blog.csdn.net/Cassie_zkq/article/details/88769070
没格式就没格式了吧(。ì _ í。)
#include <iostream>
#include <algorithm>
#include <string.h>
#include <ctype.h>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <map>

using namespace std;
int main() {
    EOF不是char类型的;

    2月平年28天,闰年29天。闰年:(能被4整除,但不能被100整除) or (能被400整除)

    1s可以运行10 ^ 7
    ~10 ^ 8次

    int 稍大于2e
    +9;
    long long
    9e+18;
    unsigned long long
    1e+19;

    const double pi = acos(-1.0);//真正的pi值

    floor(sqrt(x) + 0.5);//四舍五入向下取整

    double a = 1.2323232;
    int x = 4;
    printf("%.*f", x, a);//控制输出的小数位数

    int a[20][20], b[20][20];
    memcpy(a, b, sizeof(b));//把b数组复制到a中

    int abc = 123, de = 45, x = 6, y = 8, z = 9;
    char buf[20];
    sprintf(buf, "%d%d%d%d%d", abc, de, x, y, z);//buf的内容:12345689 长度为8

    string s;
    getline(cin, s);
    int h, m, s, d = 0;
    sscanf(s.c_str(), "%d:%d:%d (%d)", &h, &m, &s, &d);//蓝桥时差那道题的读入,要从字符数组读入

    if (strchr(buf, '1') != NULL)//在字符数组buf中查找单个字符

        int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    reverse(a, a + 10);//此时a[]={9,8,7,6,5,4,3,2,1,0};
    int b =[10];
    reverse_copy(a, a + 10, b);//把逆转后的结果放入b数组中

    //<ctype.h>头文件下的
    if (isalpha(int c));//是字母
    if (isdigit(int c));//是数字
    if (islower(int c));//是小写字母
    if (isupper(int c));//是大写字母
    toupper(int
    c);//转成大写
    tolower(int
    c);//转成小写

    string s = "ABSCD";
    transform(s.begin(), s.end(), s.begin(), ::tolower);//注意前面是三个参数,两个s.begin(),后面的tolower没有括号

    printf("%d %o %x", 10, 10, 10);//十进制10、八进制12、十六进制a

    精度问题时eps = 1e-6;

    特殊读入的四种常用方法:
    1.while(!cin.eof())
    2.string
    s;
    while (getline(cin, s))
        3.string
    s1, s2, ss;
    while (getline(cin, s1)) {
        stringstream ss(s1);//速度很慢
        while (s1 >> s2)
        ....
    }
    4.while(getchar())

    struct node//结构体内的模版化操作
    {
        int x, y;

        node(int x = 0, int y = 0) : x(x), y(y) {}

        friend bool operator<(node a, node b)

        return a.x<b.
        x;
    };

    int a[5] = {1, 2, 4, 6, 7};
    //二分实现,所以要求数组要有序!!,不存在则返回end(),也可用于vetcor、map等容器中
    lower_bound(a, a + 5, 6) - a;//返回第一个大于等6的数的下标
    upper_bound(a, a + 5, 3) - a;//返回第一个大于3的数的下标
    //拓展
    lower_bound(a, a + 5, 5, greater<int>()) - a;//第一个小于等于5
    upper_bound(a, a + 5, 5, greater<int>()) - a;//第一个小于5

    int a[5] = {3, 2, 5, 1, 6};
    sort(a, a + 5, greater<int>());//降序
    set<int, greater<int> > a;//默认降序排列
    priority_queue<int, vector<int>, greater<int> > q;
    priority_queue<int, vector<int>, cmp> q;//再单独写个函数

    int a[10],b[10][10];
    fill(a, a + 10, 233);
    fill(b[0],b[0]+10*10,233);

    vector<int> a(10);//指定变长数组的大小
    a.resize(11);//改变vector容器的大小

    map<key, value> m;
    m.count(key);//key出现的次数,有时相当于在m中查找key是否出现过

    //并集,去重,结果存到vector中,且需提前指定容器的大小
    int a[5] = {1, 2, 3, 4, 5};
    int b[5] = {4, 5, 6, 7, 8};
    vector<int> c(10);//一定要指定大小,否则会无法运行
    vector<int>::iterator it;
    it = set_union(a, a + 5, b, b + 5, c.begin());//set_union返回地址
    cout << it - c.begin() << endl;//元素个数
    //交集
    set<int> a, b, c;
    a.insert(1);
    a.insert(4);
    a.insert(3);
    b.insert(6);
    b.insert(3);
    set_intersection(a.begin(), a.end(), b.begin(), b.end(), inserter(c, c.begin()));
    //最后一个参数不可用c.begin(),否则会无法运行,也不可令it=set_union(。。。)
    cout << "c中元素个数:" << c.size() << endl;


    string s;
    int k = s.find('@');//返回地址
    string ss = s.substr(0, k), sss = s.substr(k + 1);//截取子串

    int a[5] = {1, 2, 2, 4, 5};
    int m = unique(a, a + 5) - a;//返回不重复序列的最后一个下标,有点问题???

    deque<int> qr;//双端队列
    qr.push_back(x);
    qr.push_front(x);

    int p[5] = {1, 2, 3, 4, 5};
    do {

    } while (next_permutation(p, p + 5));//全排列
    //流读入,多次的时候记得清空流!!
    stringstream ss;
    int first, second;
    ss << "456";//将“456”字符串收入流
    ss >> first;
    cout << first << endl;
    ss.clear();//清除,定义的stringstream的名称.clear();
    ss << true;//bool值
    ss >> second;
    cout << second << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Cassie_zkq/article/details/88769070
今日推荐