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

main.cpp:

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

using namespace std;

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

    // 读写文件
    cout << "File IO:" << "\n";
    save("test.txt", "jfz poi");
    saveAppend("test.txt", "jfz sandeepin");
    vector<string> txtVec = read("test.txt");

    // 展示数据
    show("read result:");
    show(txtVec);

    // 类型转换

    // string操作

    // Set封装
    show("TreeSet:");
    // set<string> treeSet;
    unordered_set<string> treeSet;
    // 增
    treeSet.insert("jfz");
    treeSet.insert("poi");
    treeSet.insert("sandeepin");
    treeSet.insert("Sandeepin");
    // 删
    treeSet.erase("sandeepin");
    // 查(包含)
    if (treeSet.find("poi") != treeSet.end()) {
        show("contains:poi");
    }
    // 查(遍历)
    for (const auto &item : treeSet) {
        show(item);
    }
    // 清空
    treeSet.clear();
    show(to_string(treeSet.size()));
    // 封装
    vector<string> vct = {"789", "123", "456"};
    setAddAll(treeSet, vct);
    show(treeSet);
    setAdd(treeSet, "000");
    setRemove(treeSet, "456");
    show(to_string(setContains(treeSet, "456")));
    show(to_string(setContains(treeSet, "000")));
    show(treeSet);
    setRemoveAll(treeSet);
    show(treeSet);

    // Map封装
    show("TreeMap:");
    map<string, string> treeMap;
    // unordered_map<string, string> treeMap;
    // 增
    treeMap.insert(pair<string, string>("jfz", "poi"));
    treeMap["123"] = "456";
    treeMap["abc"] = "hahaha";
    // 删
    treeMap.erase("123");
    // 查(包含)
    if (treeMap.find("abc") != treeMap.end()) {
        show("contains:abc");
    }
    // 查(遍历)
    for (const auto &item : treeMap) {
        show(item.first + " " + item.second);
    }
    // 清空
    treeMap.clear();
    show(to_string(treeMap.size()));
    // 封装
    treeMap["123"] = "456";
    treeMap["kxy"] = "opq";
    treeMap["_we3"] = "Sandeepin";
    show(treeMap);
    mapPut(treeMap, "000", "999");
    show(mapGet(treeMap, "000"));
    mapPut(treeMap, "000", "888");
    show(mapGet(treeMap, "000"));
    mapRemove(treeMap, "123");
    show(to_string(mapContainsKey(treeMap, "kxy")));
    show(to_string(mapContainsKey(treeMap, "ffff")));
    show(to_string(mapContainsValue(treeMap, "Sandeepin")));
    show(to_string(mapContainsValue(treeMap, "zaqwsxcde")));
    show(treeMap);
    mapRemoveAll(treeMap);
    show(treeMap);

    return 0;
}

util.h:

#ifndef JFZLIBRARY_UTIL_H
#define JFZLIBRARY_UTIL_H

#include <string>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>

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);
void show(std::set<std::string> &set);
void show(std::unordered_set<std::string> &set);
void show(std::map<std::string, std::string> &map);
void show(std::unordered_map<std::string, std::string> &map);

/* TreeSet */
void setAdd(std::set<std::string> &set, const std::string &item);
void setRemove(std::set<std::string> &set, const std::string &item);
bool setContains(std::set<std::string> &set, const std::string &item);
void setAddAll(std::set<std::string> &set, const std::vector<std::string> &vct);
void setRemoveAll(std::set<std::string> &set);

/* HashSet */
void setAdd(std::unordered_set<std::string> &set, const std::string &item);
void setRemove(std::unordered_set<std::string> &set, const std::string &item);
bool setContains(std::unordered_set<std::string> &set, const std::string &item);
void setAddAll(std::unordered_set<std::string> &set, const std::vector<std::string> &vct);
void setRemoveAll(std::unordered_set<std::string> &set);

/* TreeMap */
void mapPut(std::map<std::string, std::string> &map, const std::string &key, const std::string &value);
std::string mapGet(std::map<std::string, std::string> &map, const std::string &key);
void mapRemove(std::map<std::string, std::string> &map, const std::string &key);
bool mapContainsKey(std::map<std::string, std::string> &map, const std::string &key);
bool mapContainsValue(std::map<std::string, std::string> &map, const std::string &value);
void mapRemoveAll(std::map<std::string, std::string> &map);

/* HashMap */
void mapPut(std::unordered_map<std::string, std::string> &map, const std::string &key, const std::string &value);
std::string mapGet(std::unordered_map<std::string, std::string> &map, const std::string &key);
void mapRemove(std::unordered_map<std::string, std::string> &map, const std::string &key);
bool mapContainsKey(std::unordered_map<std::string, std::string> &map, const std::string &key);
bool mapContainsValue(std::unordered_map<std::string, std::string> &map, const std::string &value);
void mapRemoveAll(std::unordered_map<std::string, std::string> &map);

#endif //JFZLIBRARY_UTIL_H

util.cpp:

#include <sys/time.h>
#include <string>
#include <fstream>
#include <vector>
#include <iostream>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>

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 (const auto &i : vct) {
        std::cout << i << "\n";
    }
}

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

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

void show(std::map<std::string, std::string> &map) {
    for (const auto &i : map) {
        std::cout << i.first << " " << i.second << "\n";
    }
}

void show(std::unordered_map<std::string, std::string> &map) {
    for (const auto &i : map) {
        std::cout << i.first << " " << i.second << "\n";
    }
}

/* TreeSet */

void setAdd(std::set<std::string> &set, const std::string &item) {
    set.insert(item);
}

void setRemove(std::set<std::string> &set, const std::string &item) {
    set.erase(item);
}

bool setContains(std::set<std::string> &set, const std::string &item) {
    return set.find(item) != set.end();
}

void setAddAll(std::set<std::string> &set, const std::vector<std::string> &vct) {
    for (const auto &item : vct) {
        set.insert(item);
    }
}

void setRemoveAll(std::set<std::string> &set) {
    set.clear();
}

/* HashSet */

void setAdd(std::unordered_set<std::string> &set, const std::string &item) {
    set.insert(item);
}

void setRemove(std::unordered_set<std::string> &set, const std::string &item) {
    set.erase(item);
}

bool setContains(std::unordered_set<std::string> &set, const std::string &item) {
    return set.find(item) != set.end();
}

void setAddAll(std::unordered_set<std::string> &set, const std::vector<std::string> &vct) {
    for (const auto &item : vct) {
        set.insert(item);
    }
}

void setRemoveAll(std::unordered_set<std::string> &set) {
    set.clear();
}

/* TreeMap */
void mapPut(std::map<std::string, std::string> &map, const std::string &key, const std::string &value) {
    map[key] = value;
}

std::string mapGet(std::map<std::string, std::string> &map, const std::string &key) {
    if (map.find(key) != map.end()) {
        return map[key];
    }
    return "null";
}

void mapRemove(std::map<std::string, std::string> &map, const std::string &key) {
    map.erase(key);
}

bool mapContainsKey(std::map<std::string, std::string> &map, const std::string &key) {
    return map.find(key) != map.end();
}

bool mapContainsValue(std::map<std::string, std::string> &map, const std::string &value) {
    for (const auto &i : map) {
        if (i.second == value) {
            return true;
        }
    }
    return false;
}

void mapRemoveAll(std::map<std::string, std::string> &map) {
    map.clear();
}

/* HashMap */

void mapPut(std::unordered_map<std::string, std::string> &map, const std::string &key, const std::string &value) {
    map[key] = value;
}

std::string mapGet(std::unordered_map<std::string, std::string> &map, const std::string &key) {
    if (map.find(key) != map.end()) {
        return map[key];
    }
    return "null";
}

void mapRemove(std::unordered_map<std::string, std::string> &map, const std::string &key) {
    map.erase(key);
}

bool mapContainsKey(std::unordered_map<std::string, std::string> &map, const std::string &key) {
    return map.find(key) != map.end();
}

bool mapContainsValue(std::unordered_map<std::string, std::string> &map, const std::string &value) {
    for (const auto &i : map) {
        if (i.second == value) {
            return true;
        }
    }
    return false;
}

void mapRemoveAll(std::unordered_map<std::string, std::string> &map) {
    map.clear();
}

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