我的C++常用函数

#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <sys/time.h>
#include <vector>
#include <deque>
#include <math.h>

// use eg: 
//  auto _ =IntervalTimer("Interval of compute:");
class IntervalTimer {
public:
    IntervalTimer(std::string s): text_(s) {
        us_beg_ = get_us();
    }
    ~IntervalTimer() {
        long us_end = get_us();
        std::stringstream ss;
        ss << text_ << " " << us_end - us_beg_ << " us.";
        std::cout << ss.str() << std::endl; 
    }

protected:
    long get_us() {
        struct  timeval tv;
        gettimeofday(&tv, 0x0);
        return tv.tv_usec + tv.tv_sec*1000000;
    }

private:
    long us_beg_;
    std::string text_;
};

static std::vector<std::string> Split(const std::string& s, std::string delim)
{
    std::vector<std::string> elems;
    size_t pos = 0;
    size_t len = s.length();
    size_t delim_len = delim.length();
    if (delim_len == 0) return elems;
    while (pos < len)
    {
        int find_pos = s.find(delim, pos);
        if (find_pos < 0)
        {
            elems.push_back(s.substr(pos, len - pos));
            break;
        }
        elems.push_back(s.substr(pos, find_pos - pos));
        //pos = find_pos + delim_len; 
        pos = s.find_first_not_of(delim, find_pos + delim_len); // Note: 跳过连续的间隔符
    }
    return elems;
}

/* 根据多个分隔符来分隔字符串. 比如 " :,]" */
std::vector<std::string> SplitString(const std::string& str, const std::string& delimiters) {
    std::vector<std::string> tokens;
    size_t prev = 0, pos;
    while ((pos = str.find_first_of(delimiters, prev)) != std::string::npos) {
        if (pos > prev) {
            tokens.push_back(str.substr(prev, pos-prev));
        }
        prev = pos + 1;
    }
    if (prev < str.length()) {
        tokens.push_back(str.substr(prev, std::string::npos));
    }
    return tokens;
}

static std::string Trim(std::string source)
{
        std::replace( source.begin(), source.end(), '\t', ' '); // replace all '\t' to ' '
        std::replace( source.begin(), source.end(), '\n', ' ');
        std::replace( source.begin(), source.end(), '\r', ' ');

    std::string result = source.erase(source.find_last_not_of(" ") + 1);
    return result.erase(0, result.find_first_not_of(" "));
}

static double Stod(std::string s) {
    std::string ss = Trim(s);
    //return ss.empty() ? 0.0 : std::stod(ss);
    return ss.empty() ? 0.0 : std::atof(ss.c_str());
}

static int Stoi(std::string s) {
    std::string ss = Trim(s);
    return ss.empty() ? 0 : std::stoi(ss);
}

static std::string get_format_time(int64_t ms) {
    int microsec = ms%1000;

    time_t t(ms / 1000);
    struct tm *p = localtime(&t);
    char s[64];
    int pos = strftime(s, sizeof(s), "%Y%m%d-%H:%M:%S", p);

    sprintf(&s[pos], ".%03d", microsec);

    return std::string(s);
}

static std::string get_curr_format_time() {
    struct  timeval tv;
    gettimeofday(&tv, 0x0);

    int microsec = tv.tv_usec/1000;
    time_t t(tv.tv_sec);
    struct tm *p = localtime(&t);
    char s[64];
    int pos = strftime(s, sizeof(s), "%Y%m%d-%H:%M:%S", p);

    sprintf(&s[pos], ".%03d", microsec);

    return std::string(s);
}

static std::string get_curr_format_hhmmss() { // eg: 09:30:00
    time_t t = time(NULL); 
    struct tm *p = localtime(&t);
    char s[64];
    strftime(s, sizeof(s), "%H:%M:%S", p);
    return std::string(s);
}

static long long get_morning_timems() // <- 00:00:00 of today.
{
    time_t t = time(NULL); 
    struct tm * tm= localtime(&t);  
    tm->tm_hour = 0;  
    tm->tm_min = 0;  
    tm->tm_sec = 0;  
    return mktime(tm) * 1000; 
}

static long long timems_after_morning(int64_t ms) {
    return ms - get_morning_timems();
}

// days = to_date - from_date
// eg: from_date 20210520, to_date 20210601, return 12.
static int days_between_dates(std::string from_date, std::string to_date) {
    struct tm tm1, tm2;
    strptime((from_date+"-00:00:00").c_str(), "%Y%m%d-%H:%M:%S", &tm1);
    strptime((  to_date+"-00:00:00").c_str(), "%Y%m%d-%H:%M:%S", &tm2);
    time_t t1 = mktime(&tm1);
    time_t t2 = mktime(&tm2);
    if (t1 == (std::time_t)(-1) || t2 == (std::time_t)(-1)) return 0;
    return int(difftime(t2, t1) / (60 * 60 * 24));
}

// 获取当日的分钟数
static double get_curr_munites() { 
    time_t t = time(NULL); 
    struct tm *p = localtime(&t);
    return p->tm_hour * 60 + p->tm_min + (double)p->tm_sec/60.0;
}

// 获取小数点位数. 失败返回-1.
static int get_decimal_point_value(double& num/* eg:2.908 */) {
    double a;
    for(int i=1; i<16; i++) {
        a = num * pow(10, i); //<- must use pow.
        if (a == (int)a)
            return i;
    }
    return -1;
}

// 修改price的精度.
static double make_price_precision(double& price, int decimal_point_value/* 小数点位数 */) {
    int po = pow(10, decimal_point_value);
    return floor(price*po)/po;
}

void Sleep_ms(long millisecond) {
    if (millisecond == 0) 
        return;
    usleep(millisecond * 1000); // #include <unistd.h>
}

void Sleep_ms(double millisecond) {
    if (millisecond == 0) 
        return;
    usleep(millisecond * 1000);
}

void Sleep_us(long us) {
    if (us == 0) 
        return;
    usleep(us);
}
posted @ 2023-04-26 13:42  小鼬就是我  阅读(17)  评论(0编辑  收藏  举报