序文
コーディング プロセス中に、文字列の変換、タイムスタンプの取得、ファイルの保存、関数の実行時間のカウントなど、退屈で時間のかかるさまざまな小さな関数に遭遇することがよくあります。
投稿を開いて、これらの小さなツール コードを投稿し、全員がすぐに電話できるようにします。
日時を取得
ファイルの命名に適した、現在の日付と時刻を取得するのに非常に便利です
#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;
}
文字列、整数、浮動小数点数、文字列、文字、文字*の間の変換
周りにあるすべての問題を解決する
//这个是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;
}