Last active
November 7, 2020 06:42
-
-
Save SolemnJoker/3593b7a2d44fd5f25f22f54201e0a925 to your computer and use it in GitHub Desktop.
[c++ unit] #c++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ExecuteOnExit{ | |
public: | |
ExecuteOnExit() = default; | |
// movable | |
ExecuteOnExit(ExecuteOnExit&& ) = default; | |
ExecuteOnExit& operator=(ExecuteOnExit&& ) = default; | |
// non copyable | |
ExecuteOnExit(const ExecuteOnExit& e) = delete; | |
void operator=(const ExecuteOnExit& f) = delete; | |
template <typename F, typename... Args> | |
ExecuteOnExit(F&& f, Args&&... args) { | |
func_ = std::bind(std::forward<F>(f), std::forward<Args>(args)...); | |
} | |
~ExecuteOnExit() noexcept { | |
if (func_) func_(); | |
} | |
private: | |
std::function<void ()> func_; | |
}; | |
#define _CONCAT(a,b) a##b | |
#define _MAKE_DEFER(line) ExecuteOnExit _CONCAT(defer,line) = [&]() | |
#define DEFER _MAKE_DEFER(__LINE__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static double get_cur_time(){ | |
struct timeval tv; | |
gettimeofday(&tv, NULL); | |
double ts = tv.tv_sec + tv.tv_usec/(1000.0*1000.0); | |
return ts*1000; | |
} | |
class ElapseTime{ | |
public: | |
ElapseTime() = default; | |
// movable | |
ElapseTime(ElapseTime&& ) = default; | |
ElapseTime& operator=(ElapseTime&& ) = default; | |
// non copyable | |
ElapseTime(const ElapseTime& e) = delete; | |
void operator=(const ElapseTime& f) = delete; | |
double operator()() const{ | |
return elpased_time_; | |
}; | |
template <typename F, typename... Args> | |
ElapseTime(F&& f, Args&&... args) { | |
std::function<void ()> func_ = std::bind(std::forward<F>(f), std::forward<Args>(args)...); | |
double start = get_cur_time(); | |
func_(); | |
double end = get_cur_time(); | |
elpased_time_ = end - start; | |
} | |
double get_elpased_time(){ | |
return elpased_time_; | |
} | |
~ElapseTime() { | |
} | |
private: | |
double elpased_time_; | |
}; | |
static std::ostream& operator<<(std::ostream &os,const ElapseTime& et){ | |
os<<et(); | |
return os; | |
}; | |
#define ELAPED_TIME(name) ElapseTime name = [&]() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//1. | |
//#include <iostream> | |
//#include <sstream> | |
double a=35.000,b=14.967; | |
std::ostringstream ostr; | |
ostr << "a+b=" << a << "+" << b << "=" << a+b; | |
std::string str = ostr.str(); | |
//2. | |
char buf[1024]; | |
sprintf(buf,"a+b=%f+%f=%f",a,b,a+b); | |
memset(buf,0,sizeof(buf)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//#include <fstream> | |
//#include <streambuf> | |
//#include <string> | |
ifstream ifs(config_file); | |
if (ifs.is_open()){ | |
DEFER{ifs.close();}; | |
string buf((istreambuf_iterator<char>(ifs)),istreambuf_iterator<char>()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline | |
std::vector<std::string> split(const std::string& str, char seperator) { | |
std::vector<std::string> results; | |
std::string::size_type start = 0; | |
std::string::size_type sep = str.find(seperator); | |
while (sep != std::string::npos) { | |
if (start < sep) | |
results.emplace_back(str.substr(start, sep - start)); | |
start = sep + 1; | |
sep = str.find(seperator, start); | |
} | |
if (start != str.size()) | |
results.emplace_back(str.substr(start)); | |
return results; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//#include <time.h> | |
//#include <chrono> | |
//毫秒级 | |
std::time_t getTimeStamp() | |
{ | |
std::chrono::time_point<std::chrono::system_clock,std::chrono::milliseconds> tp = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());//获取当前时间点 | |
std::time_t timestamp = tp.time_since_epoch().count(); //计算距离1970-1-1,00:00的时间长度 | |
return timestamp; | |
} | |
//秒级 | |
//#include <time.h> | |
time(nullptr) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline | |
std::string& trim(std::string &&s) { | |
if (s.empty()) | |
{ | |
return s; | |
} | |
s.erase(0,s.find_first_not_of(" ")); | |
s.erase(s.find_last_not_of(" ") + 1); | |
if(s.back() == '\r' || s.back() == '\n'){ | |
s.erase(s.size() -1); | |
} | |
return s; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment