zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    nexus搭建maven私服
    eclipse 使用git
    多启动引导工具_YUMI – Multiboot USB Creator
    减压Zip与创建Zip文档
    将设置集成到右键联级菜单
    在Windows 10上清除和管理TPM (受信任的平台模块)
    删除Windows运行记录
    计算文本HasH
    在右键新建中添加"Windows PowerShell脚本"
    调整Windows 10字号
  • 原文地址:https://www.cnblogs.com/sandeepin/p/c-cpp-common-code.html
Copyright © 2011-2022 走看看