• C++语言


    基础知识

    typedef type newname; // 为已有的类型取一个新名字
    
    // 枚举类型
    enum color {red, green=5, yellow};
    // color: 枚举名
    // red, green, yellow: 标识符
    // 默认,第一个标识符的值为0,第二个标识符的值为1,以此类推
    // 也可以赋值,此时yellow=6,总是比前一个大1
    
    // 定义常量
    #define LENGTH 10
    const int LENGTH 10;
    
    // 杂项运算符
    sizeof(a) // 4,a为整数
    
    // 查看数据类型
    #include<typeinfo>
    cout << typeid(a).name() << endl;
    cout << (typeid(a) == typeid(b)) << endl;
    
    // 数组二分查找
    int a[10];
    int pos1 = lower_bound(a, a + 10, b) - a; // 返回数组中第一个大于或等于b的位置
    int pos2 = upper_bound(a, a + 10, b) - a; // 返回数组中第一个大于b的位置
    
    // int极值
    #include <climits>
    int a = INT_MAX;
    int b = INT_MIN;
    long long c = LLONG_MAX;
    

    vector

    #include <vector>
    using namespace std;
    int main()
    {
        vector<int> obj;
        vector<int> obj(n, 0); // 一维向量初始化
        vector<vector<int>> obj(n, vector<int>(n, 0)); // 二维向量初始化
        obj.push_back(1); // 向末尾添加一个元素
        obj.emplace_back(1); // emplace_back能通过参数构造对象,不需要拷贝或者移动内存
        obj.pop_back();   // 删除末尾的元素
        length = obj.size();
        obj.clear(); // 清楚容器内所有元素
        vector<int> cut_obj(obj.begin() obj.begin() + 5); // vector截取
        
        head = obj.begin(); // 容器的头指针
        tail = obj.end();   // 容器最后一个元素位置+1的指针
        sort(obj.begin(), obj.end()); // 从小到大排序 #include<algorithm>
        reverse(obj.begin(), obj.end()); // 翻转
    
        //vector拼接
        vector<int> a = {0,1};
        vector<int> b = {2,3};
        b.insert(b.begin(), a.begin(), a.end()); // b 拼接 a
        b.insert(b.end(), a.begin(), a.end());  // a 拼接 b
    
        #include<algorithm>
        maxValue = *max_element(obj.begin(), obj.end()); // 最大值 
        minValue = *min_element(obj.begin(), obj.end()); // 最小值
        maxIndex = max_element(obj.begin(), obj.end()) - obj.begin(); // 第一个最大值的下标
        minIndex = min_element(obj.begin(), obj.end()) - obj.begin(); // 第一个最小值的下标
        
        #include<numeric> 
        sum = accumulate(obj.begin(), obj.end(), 0);
        
        obj.erase(it); //返回指向删除后下一个的迭代器
    
        all_of(obj.begin(), obj.end(), [](int& item)(return 100 < item;)); // 是否容器中所有元素都满足条件
        any_of(obj.begin(), obj.end(), [](int& item)(return 100 < item;)); // 是否容器中存在元素满足条件
        none_of(obj.begin(), obj.end(), [](int& item)(return 100 < item;)); // 是否容器中所有元素都不满足条件
        
        // 判断元素是否存在
        auto it = find(obj.begin(), obj.end(), 5);
        if (it != obj.end()) cout << "True" << endl;
        else cout << "False" << endl;
    }
    

    map

    #include <map>
    using namespace std;
    int main()
    {
        map<int, string> mapA;
        mapA.insert(pair<int, string>(1, "student_one")); // 插入方式一
        mapA[2] = "student_two"; // 插入方式二
        
        // map的遍历
        int n = mapA.size();
        for (auto iter = mapA.begin(); iter != mapA.end(); iter++)  {
            cout << iter->first << ' ';
            cout << iter->second << endl;  
        }
        for (auto& c : mapA) {
            cout << c.first << " " << c.second << endl;
        }
        for (auto& [a, b] : mapA) {
            cout << a << " " << b << endl;
        }
        
        // 查询
        auto iter = mapA.find(2); //返回迭代器 没有则返回mapA.end()
        
        // 删除
        mapA.erase(iter);
        mapA.erase(1);
        mapA.erase(mapA.begin(), mapA.end());
    }
    

    set

    #include <set>
    int main()
    {
        set<int> s; // set内部由红黑树实现
        int a = 1;
        s.insert(a);
        s.erase(a);
        s.count(a); // 存在返回1,否则返回0
        auto iter = s.lower_bound(a); // 返回第一个大于等于a的元素的迭代器,没有返回s.end(),时间复杂度O(logn)
        iter = s.upper_bound(a);      // 返回第一个大于a的元素的迭代器,没有返回s.end(),时间复杂度O(logn)
        // 区别于 auto iter = lower_bound(s.begin(), s.end(), a); 时间复杂度是O(n+logn)(存疑)
        
        set<int> result;
        set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(result, result.begin())); // 交集
        set_union(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(result, result.begin())); // 并集
        set_difference(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(result, result.begin())); // 补集
    
    }
    

    排序

    #include<algorithm>
    bool campare(int a, int b)
    {
        return a < b; // 升序排列 a>b为降序排列
    }
    int main()
    {
        int a[20] = {...};
        sort(a, a + 20, compare);
    
        sort(a.begin(), a.end()); // 正序
        sort(a.begin(), a.end(), greater<int>()); // 倒序
    }
    

    优先队列

    #include<queue>
    priority_queue<int, vector<int>, greater<int>> q; // 小根堆
    priority_queue<int, vector<int>, less<int>> q; // 大根堆
    priority_queue<int> q; // 对于基础类型 默认是大根堆
    
    q.push(i);
    q.empty(); // true or false
    q.top(); // 返回队首元素
    q.pop();
    
    // 对于自定义类型
    struct tmp1 {
        int a, b;
        tmp1(int a, int b) : a(a), b(b){};
        bool operator < (const tmp1& x) const {
            return a < x.a;
        }
    };
    priority_queue<tmp1> q; // 大根堆 不满足条件的优先出队
    
    struct cmp {
        bool operator () (tmp1 x1, tmp1 x2) {
            return x1.a < x2.a;
        }
    };
    priority_queue<tmp1, vector<tmp1>, cmp> q;
    

    位运算

    int __builtin_ffs (unsigned int x)       //返回x的最后一位1的是从后向前第几位,比如7368(1110011001000)返回4。
    int __builtin_clz (unsigned int x)       //返回前导的0的个数。
    int __builtin_ctz (unsigned int x)       //返回后面的0个个数,和__builtin_clz相对。
    int __builtin_popcount (unsigned int x)  //返回二进制表示中1的个数。
    int __builtin_parity (unsigned int x)    //返回x的奇偶校验位,也就是x的1的个数模2的结果。
    
    

    字符串

    #include <string>
    #include <cstring>
    
    int main()
    {
        string s = "hello";
        if (s.find('a') == s.npos) {
            cout << "No matched" << endl;
        }
    
        // int转string
        string s = to_string(132);
        
        //string转int
        int a = atoi(s.c_str()); //超过int范围输出上下界
        int b = stoi(s); //默认在int范围内,超过范围报runtime error
    
        // 字符串字典序比较
        cout << (string("139") < string("165")) << endl; // 需要强制转换为string类型,否则比较的则是字符串的起始地址
    }
    
  • 相关阅读:
    Qt判断文件夹是否存在并新建文件夹
    QFileDialog的使用
    C++11 std::chrono库详解
    disconnected no supported authentication methods available(server sent: publickey)
    connect函数的第5参数Qt::ConnectionType
    在C++ 中检查一个文件是否存在的几种方法
    win10打开便签
    1024. Palindromic Number (25)
    1023. Have Fun with Numbers (20)
    1021. Deepest Root (25)
  • 原文地址:https://www.cnblogs.com/AlenDou-blog/p/14456046.html
Copyright © 2020-2023  润新知