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

180it 2021-09-19 PM 1584℃ 0条

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

main.cpp:

#include <iostream>
#include "util.h"

int main() {
    std::cout << "sandeepin poi!" << std::endl;
    // 时间戳
    std::cout << "Timestamp:"<< "\n";
    std::cout << "us(ll):" << currentTimeUs() << "\n";
    std::cout << "us(str):" << currentTimeUsStr() << "\n";
    std::cout << "ms(ll):" << currentTimeMs() << "\n";
    std::cout << "ms(str)::" << currentTimeMsStr() << "\n\n";

    // 读写文件
    std::cout << "File IO:"<< "\n";
    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:

Copy
#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:

Copy
#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 << "\n";
    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 << "\n";
    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 << "\n";
}

void show(const std::vector<std::string> &vct) {
    for (auto i : vct) {
        std::cout << i << "\n";
    }
}

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

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

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

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