• C和C++常用代码片段整理


    整理一些C和C++常用的代码片段,方便自己快速开发。先总结一些C++的,C++支持函数重载,写得爽些。

    main.cpp:

    #include <iostream>
    #include "util.h"
    
    int main() {
        std::cout << "sandeepin poi!" << std::endl;
        // 时间戳
        std::cout << "Timestamp:"<< "
    ";
        std::cout << "us(ll):" << currentTimeUs() << "
    ";
        std::cout << "us(str):" << currentTimeUsStr() << "
    ";
        std::cout << "ms(ll):" << currentTimeMs() << "
    ";
        std::cout << "ms(str)::" << currentTimeMsStr() << "
    
    ";
    
        // 读写文件
        std::cout << "File IO:"<< "
    ";
        save("test.txt", "jfz poi");
        saveAppend("test.txt", "jfz sandeepin");
        std::vector<std::string> txtVec = read("test.txt");
    
        // 展示数据
        show("read result:");
        show(txtVec);
    
        // 类型转换
    
        // string操作
    
        return 0;
    }
    
    

    util.h:

    #ifndef JFZLIBRARY_UTIL_H
    #define JFZLIBRARY_UTIL_H
    
    #include <string>
    #include <vector>
    
    long long currentTimeUs();
    
    long long currentTimeMs();
    
    std::string currentTimeUsStr();
    
    std::string currentTimeMsStr();
    
    void save(const std::string &fileName, const std::string &str);
    
    void saveAppend(const std::string &fileName, const std::string &str);
    
    std::vector<std::string> read(const std::string &fileName);
    
    void show(const std::string &text);
    
    void show(const std::vector<std::string> &vct);
    
    #endif //JFZLIBRARY_UTIL_H
    
    

    util.cpp:

    #include <sys/time.h>
    #include <string>
    #include <fstream>
    #include <vector>
    #include <iostream>
    
    long long currentTimeUs() {
        struct timeval currentTimeMillis{};
        gettimeofday(&currentTimeMillis, nullptr);
        long long sec = currentTimeMillis.tv_sec;
        long long usec = currentTimeMillis.tv_usec;
        long long time = sec * 1000000 + usec;
        return time;
    }
    
    long long currentTimeMs() {
        return currentTimeUs() / 1000;
    }
    
    std::string currentTimeUsStr() {
        return std::to_string(currentTimeUs());
    }
    
    std::string currentTimeMsStr() {
        return std::to_string(currentTimeMs());
    }
    
    void save(const std::string &fileName, const std::string &str) {
        if (fileName.empty()) {
            return;
        }
        std::ofstream file;
        if (file.bad()) {
            return;
        }
        file.open(fileName, std::ios::ate);
        file << str << "
    ";
        file.close();
    }
    
    void saveAppend(const std::string &fileName, const std::string &str) {
        if (fileName.empty()) {
            return;
        }
        std::ofstream file;
        if (file.bad()) {
            return;
        }
        file.open(fileName, std::ios::app);
        file << str << "
    ";
        file.close();
    }
    
    std::vector<std::string> read(const std::string &fileName) {
        std::vector<std::string> out;
        if (fileName.empty()) {
            return out;
        }
        std::ifstream inFile;
        inFile.open(fileName, std::ios::in);
        if (inFile.bad()) {
            return out;
        }
        std::string str;
        while (getline(inFile, str)) {
            out.push_back(str);
        }
        return out;
    }
    
    
    void show(const std::string &text) {
        std::cout << text << "
    ";
    }
    
    void show(const std::vector<std::string> &vct) {
        for (auto i : vct) {
            std::cout << i << "
    ";
        }
    }
    
    

    接下来总结一些C语音的,主要是在底层编码的时候用上:

    https://jfz.me/blog/2020/c-cpp-common-code.html

  • 相关阅读:
    [Cocoa]XCode下的iOS单元测试
    [Cocoa]深入浅出Cocoa之Framework
    [MAC技巧]在Dock上添加最近使用的应用程序
    [软件工程]Scrum 学习笔记
    [Cocoa, 医疗]Dicom Image Viewer for iPad
    [Cocoa]iOS 开发者账户,联机调试,发布应用事宜
    [医疗开发]医疗相关名词解析
    [Cocoa]XCode中定制Prefix.pch文件
    [Cocoa]苹果Cocoa编码规范
    饮食贴士远离便秘瘦小腹 生活至上,美容至尚!
  • 原文地址:https://www.cnblogs.com/sandeepin/p/c-cpp-common-code.html
Copyright © 2020-2023  润新知