c++备忘知识
原创
©著作权归作者所有:来自51CTO博客作者蓝枫居士的原创作品,请联系作者获取转载授权,否则将追究法律责任
获得当前的时间戳
单位是毫秒
chrono是c++11提出的
因此 编译的时候得加上 -std=c++11 如下:
g++ getTime.cpp -o getTime -std=c++11
#include<iostream>
#include <chrono>
using namespace::std;
using namespace::chrono;
int main() {
auto time_now = system_clock::now();
auto duration_in_ms = duration_cast<milliseconds>(time_now.time_since_epoch());
cout << duration_in_ms.count() << "毫秒" << endl;
return 0;
}
字符串转化为数字 数字转化为字符串
char str[10];
int a=1234321;
sprintf(str,"%d",a);
--------------------
char str[10];
double a=123.321;
sprintf(str,"%.3lf",a);
char str[]="1234321";
int a;
sscanf(str,"%d",&a);
.............
char str[]="123.321";
double a;
sscanf(str,"%lf",&a);
字符数组 与string 相互转化
char ch [] = "ABCDEFG";
string str(ch);//也可string str = ch;
或者
char ch [] = "ABCDEFG";
string str;
str = ch;//在原有基础上添加可以用str += ch;
char buf[10];
string str("ABCDEFG");
length = str.copy(buf, 9);
buf[length] = '\0';
或者
char buf[10];
string str("ABCDEFG");
strcpy(buf, str.c_str());//strncpy(buf, str.c_str(), 10);