zoukankan      html  css  js  c++  java
  • c++琐碎

    记录点刚学到的, 不成体系

    判断浮点数为0

    #include <cmath>
    
    float a=1.0, b=2.0, c=1.0;
    float delta = power(b, 2) - 4*a*c //使用数学运算, 计算差
    
    //比较float为0时, 不能直接delta==0, 要判断其绝对值小于某个很小的数.
    if (fabs(delta)<1e-6) // fabs, 对float求绝对值.
    {
        cout << "r0==r1"
    }
    
    

    使用多维map(python中称为字典)和vector

    #include <iostream>
    #include <map>
    #include <string>
    #include <vector>
    int main ()
    {
        using namespace std;
    
        //多维vector, 声明与赋值
        vector<vector<string>> multi_v;
    
        multi_v.push_back({"k0", "k00", "k000", "k0000"});
        multi_v.push_back({"k0", "k00", "k000", "k0001"});
        multi_v.push_back({"k0", "k00", "k001", "k0010"});
        multi_v.push_back({"k0", "k00", "k001", "k0011"});
        multi_v.push_back({"k0", "k01", "k010", "k0100"});
        multi_v.push_back({"k0", "k01", "k010", "k0101"});
        multi_v.push_back({"k0", "k01", "k011", "k0110"});
        multi_v.push_back({"k0", "k01", "k011", "k0111"});
        multi_v.push_back({"k1", "k10", "k100", "k1000"});
        multi_v.push_back({"k1", "k10", "k100", "k1001"});
        multi_v.push_back({"k1", "k10", "k101", "k1010"});
        multi_v.push_back({"k1", "k10", "k101", "k1011"});
        multi_v.push_back({"k1", "k11", "k110", "k1100"});
        multi_v.push_back({"k1", "k11", "k110", "k1101"});
        multi_v.push_back({"k1", "k11", "k111", "k1110"});
        multi_v.push_back({"k1", "k11", "k111", "k1111"});
    
        //声明多维map
        map<string, map<string, map<string, map<string, int>>>> my_map_0;
        map<string, map<string, map<string, int>>> my_map_1;
        map<string, map<string, int>> my_map_2;
        map<string, int> my_map_3;
    
        //遍历多维vector, 使用iterator
        for(auto iter0=multi_v.begin(); iter0!=multi_v.end(); iter0++)
        {
            auto v1 = *iter0; //对iter解引用, 获得下一级vector
            for(auto iter1=v1.begin(); iter1!=v1.end(); iter1++)
            {
                auto v2 = *iter1;
                cout << v2 << " ";
            }
            cout << endl;
        }
    
        //遍历多维vector, 使用idx
        for(int i=0; i<multi_v.size(); i++) //.size(), 获取vector中元素个数
        {
            //获取各级key名称
            string k_L0 = multi_v[i][0];
            string k_L1 = multi_v[i][1];
            string k_L2 = multi_v[i][2];
            string k_L3 = multi_v[i][3];
    
            //多维map赋值
            //--第〇维
            if(my_map_0.find(k_L0) == my_map_0.end())
                my_map_0.insert(make_pair(k_L0, my_map_1));
    
            //--第一维
            if(my_map_0[k_L0].find(k_L1) == my_map_0[k_L0].end())
                my_map_0[k_L0].insert(make_pair(k_L1, my_map_2));
    
            //--第二维
            if(my_map_0[k_L0][k_L1].find(k_L2) == my_map_0[k_L0][k_L1].end())
                my_map_0[k_L0][k_L1].insert(make_pair(k_L2, my_map_3));
    
            //--第三维
            if(my_map_0[k_L0][k_L1][k_L2].find(k_L3) == my_map_0[k_L0][k_L1][k_L2].end())
                my_map_0[k_L0][k_L1][k_L2].insert(make_pair(k_L3, i));
        }
    
        //遍历map
        for (auto iter0=my_map_0.begin(); iter0!=my_map_0.end(); iter0++)
        {
            string k_L0 = iter0->first;
            cout << "" << k_L0 << endl;
            for (auto iter1=iter0->second.begin(); iter1!=iter0->second.end(); iter1++)
            {
                string k_L1 = iter1->first;
                cout << "    " << k_L1 << endl;
                for (auto iter2=iter1->second.begin(); iter2!=iter1->second.end(); iter2++)
                {
                    string k_L2 = iter2->first;
                    cout << "        " << k_L2 << endl;
                    for (auto iter3=iter2->second.begin(); iter3!=iter2->second.end(); iter3++)
                    {
                        string k_L3 = iter3->first;
                        int v = iter3->second;
                        cout << "            " << k_L3 << ": " << v << endl;
                    }
                }
            }
        }
    }
    

    输出

    k0 k00 k000 k0000
    k0 k00 k000 k0001
    k0 k00 k001 k0010
    k0 k00 k001 k0011
    k0 k01 k010 k0100
    k0 k01 k010 k0101
    k0 k01 k011 k0110
    k0 k01 k011 k0111
    k1 k10 k100 k1000
    k1 k10 k100 k1001
    k1 k10 k101 k1010
    k1 k10 k101 k1011
    k1 k11 k110 k1100
    k1 k11 k110 k1101
    k1 k11 k111 k1110
    k1 k11 k111 k1111
    k0
        k00
            k000
                k0000: 0
                k0001: 1
            k001
                k0010: 2
                k0011: 3
        k01
            k010
                k0100: 4
                k0101: 5
            k011
                k0110: 6
                k0111: 7
    k1
        k10
            k100
                k1000: 8
                k1001: 9
            k101
                k1010: 10
                k1011: 11
        k11
            k110
                k1100: 12
                k1101: 13
            k111
                k1110: 14
                k1111: 15
    

    格式化字符串

    #include <iostream>
    int main()
    {
        using namespace std;
        char c[300];
        sprintf(c, "%0*X %-*s", 4, 96, 4, 96);
        cout << c << endl; //奇怪, 这段代码没有打印内容, 可能有bug
    }
    

    格式化时间

    #include <ctime> //加载时间库
    #include <iostream>
    int main()
    {
        std::time_t nowtime = std::time(NULL); //获取时间戳
        std::cout << "nowtime = " << nowtime << std::endl; //输出nowtime = 1626935009
    
        struct tm * local;
        local = std::localtime(&nowtime);
        char buf[80];
        strftime(buf, 80, "%Y_%m_%d_%H_%M_%S", local); //将时间格式化
        std::cout << "strftime= " << buf << std::endl; //输出strftime= 2021_07_22_14_23_29
    
        return 0;
    }
    
    

    int 转 string

    #include <string>
    #include <sstream> //使用stringstream需要这个库
    #include <iostream>
    int main()
    {
        //int转string, 使用stringstream
        std::stringstream str_stream;
        int i = 700;
        int j = 800;
        str_stream << i << j; //使用stringstream将两个int拼接.
    
        std::string s0 = str_stream.str(); //将stringstream转为string.
        std::cout << "s0=" << s0 << std::endl; //输出 s0=700800
    
        //int转string, 使用itoa, 好像不好用, 算了.
        //int num = 100;
        //char str0[25];
        //char str1[25];
        //std::itoa(num, str0, 10); //参数: 待转int, 输出char, 进制
        //std::itoa(num, str1, 16); //参数: 待转int, 输出char, 进制
        //std::cout << "str0=" << str0 << std::endl;
        //std::cout << "str1=" << str1 << std::endl;
    
    
        //string转int, 使用atoi
        std::string s1 = "700800";
        int k = std::atoi(s1.c_str());
        std::cout << "k+1=" << k+1 << std::endl; //输出 k+1=700801
    
        return 0;
    }
    

    正则匹配

    #include <iostream>
    #include <regex>
    int main()
    {
        std::string s0 = "012-3456-789";
    
        //设置patten
        std::regex regex_number(R"((d+)-(d+)-(d+))"); //R"()"表示内部是正则字符串, 就表示\, 不需要用\转义
    
        //声明match_results, 用于捕获匹配内容
        std::match_results<std::string::const_iterator> result_number;
    
        //进行匹配
        bool b_match_number = std::regex_match(s0, result_number, regex_number); //注意regex_match会匹配整个字段, 从头到尾
    
        //查看结果
        if (b_match_number)
        {
            std::cout << "match:" << std::endl;
            std::cout << "    0: " << result_number[0] << std::endl; //整个字串: 012-3456-789
            std::cout << "    1: " << result_number[1] << std::endl; //第一个括号: 123
            std::cout << "    2: " << result_number[2] << std::endl; //第二个括号: 3456
            std::cout << "    3: " << result_number[3] << std::endl; //第三个括号: 789
        }
        else
        {
            std::cout << "not match" << std::endl;
        }
    
        //遍历捕获的内容
        for(auto iter=result_number.begin(); iter!=result_number.end(); iter++)
        {
            //使用iter.str()可以得到对应字符串.
            std::cout << "length=" << iter->length() << ", str=" << iter->str() << std::endl;
        }
        //输出:
        //length=12, str=012-3456-789
        //length=3, str=012
        //length=4, str=3456
        //length=3, str=789
    }
    

    读写文件

    #include <fstream>
    #include <iostream>
    #include <string>
    #include <unistd.h> //使用access和F_OK
    int main()
    {
        //读文件
        std::string ifile_name= "test.txt";
        if (access(ifile_name.c_str(), F_OK) == -1) //判断文件是否存在
        {
            std::cout << "file not exists: " << ifile_name << std::endl;
        }
        else
        {
            std::ifstream infile(ifile_name, std::ios::in); //打开读文件, 使用ifstream
            std::string line;
            while(std::getline(infile, line)) //获取每一行内容, 放到变量line中.
            {
                std::cout << line << std::endl;
            }
            infile.close(); //关闭文件
        }
    
    
        //写文件
        std::string ofile_name= "test.txt.out";
        std::ofstream outfile;
        outfile.open(ofile_name); //打开写文件, 使用ofstream
        outfile << "line0" << std::endl; //写入内容
        outfile << "line1" << std::endl;
        outfile << "line2" << std::endl;
        outfile.close(); //关闭
    
        return 0;
    }
    

    获取系统命令的标准输出

    #include <vector>
    #include <string>
    #include <iostream>
    
    std::vector<std::string> run_cmd(const char cmd[])
    {
        using namespace std;
    
        vector<string> all_lines;
    
        FILE * pFile;
        pFile = popen(cmd, "r");
    
        if (pFile==NULL)
        {
            cout << "error." << endl;
        }
        else{
            char line[500];
            while(fgets(line, sizeof(line)-1, pFile)!=NULL)
            {
                all_lines.push_back(line);
            }
        }
        pclose(pFile);
    
        return all_lines;
    }
    
    int main()
    {
        using namespace std;
    
        string cmd = "dir";
        auto all_lines = run_cmd(cmd.c_str());
        for(int i=0; i<all_lines.size(); i++)
        {
            cout << all_lines[i] << endl;
        }
    }
    
  • 相关阅读:
    菜鸟快速自学java00之变量类型
    php 接口和抽象类
    java 三大特性之多态性
    设计模式
    依赖注入
    Java设计模式工厂模式
    php 设计模式之策略模式
    大数的概述
    熟悉常用的Linux操作
    GridView动态添加列
  • 原文地址:https://www.cnblogs.com/gaiqingfeng/p/14990663.html
Copyright © 2011-2022 走看看