新浪博客

C++好玩的字面变量(更新)

2024-03-18 16:28阅读:
#include
#include
using namespace std;
using ull = unsigned long long;
ull operator '' _K(const ull v)
{
return v * 1024;
}
ull operator '' _M(const ull v)
{
return v * 1024 * 1024;
}
ull operator '' _G(const ull v)
{
return v * 1024 * 1024 * 1024;
}
ull operator '' _d(const char* v,size_t n)
{
return atoi(v);
}
ull operator '' _K(const char* v,size_t n)
{
return atoi(v)*1024;
}
ull operator '' _M(const char* v,size_t n)
{
return atoi(v)*1024*1024;
}
long double operator ''_degree(const long double degree)
{
return degree*3.1415926535897/180.0;
}
long double operator ''_degree(const ull degree)
{
return degree*3.1415926535897/180.0;
}
int main()
{
ull mem_size = 1_K;
//等价于1024拉
ull softd_size = 1_M; // 等价于1048576
ull total = 1_G + softd_size + 1_K;
ull size = '1234567'_d; //等价于1234567

cout<<'mem: '<<mem_size<<endl;
cout<<'soft: ' << softd_size << endl;
cout << 'total size: ' << total << endl;
cout << 'size=' << size <<endl;

ull dd= '1'_M;
cout<<dd<<endl;
cout<<'180degree = '<<180_degree<<endl; //180°
}
============================================
输出:
mem: 1024 soft: 1048576 total size: 1074791424 size=1234567 1048576 180degree = 3.14159

我的更多文章

下载客户端阅读体验更佳

APP专享