#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <sys/time.h>
#include <vector>
#include <deque>
#include <math.h>
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 = s.find_first_not_of(delim, find_pos + delim_len);
}
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', ' ');
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::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() {
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()
{
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();
}
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;
}
static int get_decimal_point_value(double& num) {
double a;
for(int i=1; i<16; i++) {
a = num * pow(10, i);
if (a == (int)a)
return i;
}
return -1;
}
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);
}
void Sleep_ms(double millisecond) {
if (millisecond == 0)
return;
usleep(millisecond * 1000);
}
void Sleep_us(long us) {
if (us == 0)
return;
usleep(us);
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人