前言
大家在编码过程中经常会遇到各种繁琐又费事的小功能,比如转换字符串,获取时间戳,保存文件,统计函数运行时间等等。
开一篇帖子把这些工具性的小代码贴出来,供大家快速调用。
获取日期时间
很方便地获取当前的日期时间,适合用来给文件命名
#include <iostream>
using namespace std;
int main() {
std::cout << "Hello, World!" << std::endl;
//获取当前系统时间并转为string
time_t t = time(0);
char tmp[64];
strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S", localtime(&t));
//输出时间
std::cout << tmp << std::endl;
//输出时间戳
std::cout << t << std::endl;
//t转为string
std::string s = std::to_string(t);
std::cout << s << std::endl;
//tmp转为string
std::string s1 = std::string(tmp);
std::cout << s1 << std::endl;
return 0;
}
获取高精度时间戳
如果需要获取毫秒级时间戳怎么办?用chorono库
优化一下上面的例子,如果需要毫秒级时间戳,可以用chrono获取最后的毫秒数,再拼上去
#include <chrono>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
string get_cur_time_ms() {
auto now = chrono::system_clock::now();
auto in_time_t = chrono::system_clock::to_time_t(now);
stringstream ss;
ss << put_time(localtime(&in_time_t), "%Y-%m-%d %X");
auto now_time_point_int = chrono::duration_cast<chrono::milliseconds>(now.time_since_epoch()).count();
ss << ":" << now_time_point_int%1000;
return ss.str();
}
int main() {
cout << get_cur_time_ms() << endl;
return 0;
}
chrono的精度极高,可以达到纳秒级,我们可以用它统计代码的运行时间
#include <iostream>
#include <chrono>
using namespace std;
int main() {
// 开始计时
auto start = chrono::high_resolution_clock::now();
// 运行你的代码
for (int i = 0; i < 99999; ++i) {
int m = i % 10;
int n = i / 10;
int k = n % 10;
int l = n / 10;
int j = l % 10;
}
// 结束计时
auto finish = chrono::high_resolution_clock::now();
// 统计时间,单位为微秒,也可替换为毫秒 milliseconds 、纳秒 nanoseconds 等。
auto duration = chrono::duration_cast<chrono::microseconds>(finish - start);
cout << "Duration: " << duration.count() << " microseconds" << endl;
//将start和finish转换为int
int start_int = chrono::time_point_cast<chrono::microseconds>(start).time_since_epoch().count();
int finish_int = chrono::time_point_cast<chrono::microseconds>(finish).time_since_epoch().count();
cout << "start_int: " << start_int << endl;
cout << "finish_int: " << finish_int << endl;
return 0;
}
字符串,整形,浮点数,string,char,char*之间互转
解决所有转来转去的问题
//这个是char*,char数组和string互转的库
#include <cstring>
#include <iostream>
using namespace std;
int main() {
std::cout << "Hello, World!" << std::endl;
//将字符串转换为整数
string str = "123";
int num = stoi(str);
cout << num << endl;
//将整数转换为字符串
int integer = 123;
string str_test = to_string(integer);
cout << str_test << endl;
//将浮点数转换为字符串
double d = 123.456;
string str_test2 = to_string(d);
cout << str_test2 << endl;
//将字符串转换为float
string str_test3 = "123.456";
float f = stof(str_test3);
cout << f << endl;
//将string转换为char*
string str_test4 = "123";
char *c = new char[str_test4.length() + 1];
strcpy(c, str_test4.c_str());
cout << c << endl;
//将char*转换为string
char *c_test = "123";
string str_test5 = c_test;
cout << str_test5 << endl;
//将char*转换为int
char *c_test2 = "123";
int i = atoi(c_test2);
cout << i << endl;
//将int转换为char*
int i_test = 123;
char *c_test3 = new char[10];
sprintf(c_test3, "%d", i_test);
cout << c_test3 << endl;
//char数组转换为string
char c_test4[] = "123";
string str_test6 = c_test4;
cout << str_test6 << endl;
//string转换为char数组
string str_test7 = "123";
char c_test5[str_test7.length() + 1];
strcpy(c_test5, str_test7.c_str());
cout << c_test5 << endl;
//char数组转换为char*
char c_test6[] = "123";
char *c_test7 = c_test6;
cout << c_test7 << endl;
//char*转换为char数组
char *c_test8 = "123";
char c_test9[strlen(c_test8) + 1];
strcpy(c_test9, c_test8);
cout << c_test9 << endl;
return 0;
}
写文件
将数据存入文件也是一个经常用得到的操作
这里举个例子,获取当前时间,把内容存入txt文件中
#include <chrono>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
string get_cur_time_ms() {
auto now = chrono::system_clock::now();
auto in_time_t = chrono::system_clock::to_time_t(now);
stringstream ss;
ss << put_time(localtime(&in_time_t), "%Y-%m-%d %X");
auto now_time_point_int = chrono::duration_cast<chrono::milliseconds>(now.time_since_epoch()).count();
ss << ":" << now_time_point_int%1000;
return ss.str();
}
int main() {
ofstream outfile("time.txt");
for (int i = 0; i < 9999; i++) {
string time_str = get_cur_time_ms();
const char* c_time = time_str.c_str();
cout << c_time << endl;
outfile << c_time << endl;
}
outfile.close();
return 0;
}