{"id":3874,"date":"2021-04-29T17:15:20","date_gmt":"2021-04-29T09:15:20","guid":{"rendered":"http:\/\/blog.coolcoding.cn\/?p=3874"},"modified":"2021-04-29T19:08:50","modified_gmt":"2021-04-29T11:08:50","slug":"%e7%94%a8%e6%88%b7%e5%ae%9a%e4%b9%89%e7%9a%84%e5%ad%97%e9%9d%a2%e9%87%8f","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=3874","title":{"rendered":"[c++]\u7528\u6237\u5b9a\u4e49\u7684\u5b57\u9762\u91cf"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>\/\/ --------------------------------------\n\/\/ \u5f15\u81ea xstring\u6587\u4ef6\ninline namespace literals { inline namespace string_literals {\n_NODISCARD inline string operator\"\" s(const char* _Str, size_t _Len) \n{\n  return string(_Str, _Len);\n}\n_NODISCARD inline wstring operator\"\" s(const wchar_t* _Str, size_t _Len) \n{\n  return wstring(_Str, _Len);\n}\n\/\/ --------------------------------------\n\n#include &lt;string>\nusing namespace std::string_literals;\nauto s = \"hello world\"s;<\/code><\/pre>\n\n\n\n<p>C++\u5141\u8bb8\u5b9a\u4e49\u81ea\u5df1\u7684\u5b57\u9762\u91cf\u3002\u7528\u6237\u5b9a\u4e49\u7684\u5b57\u9762\u91cf\u5e94\u8be5\u4ee5\u4e0b\u5212\u7ebf\u5f00\u5934\uff0c\u5e76\u901a\u8fc7\u7f16\u5199\u5b57\u9762\u91cf\u8fd0\u7b97\u7b26\u51fd\u6570\uff08literal operators\uff09\u6765\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p>\u5b57\u9762\u91cf\u8fd0\u7b97\u7b26\u80fd\u591f\u4ee5\u751f\u6a21\u5f0f\uff08raw\uff09\u6216\u8005\u719f\u6a21\u5f0f\uff08cooked\uff09\u8fd0\u884c\u3002\u5728\u751f\u6a21\u5f0f\u4e2d\uff0c\u5b57\u9762\u91cf\u8fd0\u7b97\u7b26\u63a5\u6536\u4e00\u4e2a\u5b57\u7b26\u4e32\u5e8f\u5217\uff0c\u5728\u719f\u6a21\u5f0f\u4e2d\u5b57\u9762\u91cf\u8fd0\u7b97\u7b26\u63a5\u6536\u4e00\u4e2a\u7ecf\u8fc7\u89e3\u91ca\u7684\u7279\u5b9a\u7c7b\u578b\uff08\u59821.23f\u30013u\u7b49\uff09\u3002<\/p>\n\n\n\n<p>\u81ea\u5b9a\u4e49\u683c\u5f0f\u5982\u4e0b\uff1a\u8fd4\u56de\u7c7b\u578b operator&#8221;&#8221; \u5b57\u9762\u91cf\u540d(\u53c2\u6570\u8868) {}\uff0c\u5176\u4e2d\u5b57\u9762\u91cf\u540d\u5efa\u8bae\u4ee5\u4e0b\u5212\u7ebf\u5f00\u5934\u3002\u5728C++11\u6807\u51c6\u4e2d\uff0c\u5bf9\u5b57\u9762\u91cf\u64cd\u4f5c\u7b26\u7684\u4f7f\u7528\u5b9a\u4e86\u4e00\u4e9b\u89c4\u5219\uff1a<\/p>\n\n\n\n<p>\u5982\u679c\u5b57\u9762\u91cf\u4e3a\u6574\u5f62\u6570\uff0c\u90a3\u4e48\u64cd\u4f5c\u7b26\u51fd\u6570\u53ea\u53ef\u63a5\u53d7unsigned long long\u6216\u8005const char\u4f5c\u4e3a\u53c2\u6570\uff0c\u5f53unsigned long long\u65e0\u6cd5\u5bb9\u7eb3\u8be5\u5b57\u9762\u91cf\u7684\u65f6\u5019\uff0c\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u5c06\u8be5\u5b57\u9762\u91cf\u8f6c\u5316\u4e3a\u4ee5&#8217;\\0&#8217;\u7ed3\u5c3e\u7684\u5b57\u7b26\u4e32\uff0c\u5e76\u8c03\u7528\u4ee5const char\u4e3a\u53c2\u6570\u7684\u7248\u672c\u8fdb\u884c\u5904\u7406\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u5b57\u9762\u91cf\u4e3a\u6d6e\u70b9\u6570\uff0c\u64cd\u4f5c\u7b26\u51fd\u6570\u53ea\u53ef\u63a5\u53d7long double\u6216\u8005const char\u4e3a\u53c2\u6570\uff0c\u4e0e\u6574\u5f62\u4e00\u6837\uff0c\u5728\u5f53long double\u65e0\u6cd5\u5bb9\u7eb3\u65f6\u8c03\u7528\u4ee5const char\u4e3a\u53c2\u6570\u7684\u7248\u672c\u8fdb\u884c\u5904\u7406\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u5b57\u9762\u91cf\u4e3a\u5b57\u7b26\u4e32\uff0c\u64cd\u4f5c\u7b26\u51fd\u6570\u53ea\u53ef\u63a5\u53d7(const char*, size_t)\u4e3a\u53c2\u6570\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u5b57\u9762\u91cf\u4e3a\u5b57\u7b26\uff0c\u5219\u64cd\u4f5c\u7b26\u51fd\u6570\u53ea\u63a5\u53d7\u4e00\u4e2achar\u4e3a\u53c2\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nclass Int {\nprivate:\n\tint value;\npublic:\n\tInt(int v) : value(v) {}\n};\n \nInt operator\"\" _I(unsigned long long i) {\n\treturn Int(i);\n}\n \nInt operator\"\" _II(const char* chs, size_t n) {\n\tint v = stoi(chs);\n\treturn Int(v);\n}\n \nint main(int argc, char* argv[]) {\n\tcout &lt;&lt; typeid(74).name() &lt;&lt;  \" , \" &lt;&lt; typeid(74_I).name() &lt;&lt; \" , \" &lt;&lt; typeid(\"74\"_II).name();\n\treturn 0;\n}<\/code><\/pre>\n\n\n\n<p>C++14\u5b9a\u4e49\u4e86\u5982\u4e0b\u6807\u51c6\u7684\u7528\u6237\u5b9a\u4e49\u5b57\u9762\u91cf\u3002s\u7528\u4e8e\u521b\u5efastd::sting\uff1bh\u3001min\u3001s\u3001ms\u3001us\u3001ns\u7528\u4e8e\u521b\u5efastd::chrono::duration\u65f6\u95f4\u6bb5\u3002i\u3001il\u3001if\u5206\u522b\u7528\u4e8e\u521b\u5efa\u590d\u6570complex\u3001complex\u548ccomplex\u590d\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Person\n{\npublic:\n    Person(const std::string&amp; name): _name(name){}\n    std::string name() const {\n        return _name;\n    }\nprivate:\n    std::string name;\n}\n\ninline Person operator \"\" _psn (const char *name, size_t n) \n{\n    return Person(std::string(name, n));\n}\n\nint main()\n{\n    auto person = \"Xiao Ming\"_psn;\n    std::cout&lt;&lt; \"The person' name is \" &lt;&lt; person.name() &lt;&lt; std::endl;\n    return 0;\n}\n\n\n\/\u53ef\u7531\u5e38\u91cf\u5b57\u7b26\u4e32\u201d{\\\u201dname\\\u201d:\\\u201dXiao Ming\\\u201d}\u201d \u6784\u9020\u51fa\u4e00\u4e2a\u5305\u542bkey\u4e3aname, value\u4e3a\u201dXiao Ming\u201d\u7684json\u5bf9\u8c61\nauto j = \"{\\\"name\\\":\\\"Xiao Ming\\\"}\"_json;\nstd::cout &lt;&lt; j[\"name\"] &lt;&lt; std::endl;<\/code><\/pre>\n\n\n\n<p>C++\u5141\u8bb8\u81ea\u5b9a\u4e49\u5b57\u9762\u91cf\uff0c\u5b57\u9762\u91cf\u4ee5\u4e0b\u5212\u7ebf\u5f00\u5934\uff0c\u7b2c1\u4e2a\u5b57\u6bcd\u5fc5\u987b\u5c0f\u5199\uff1b\u4f8b\u5982\uff1a<br>_s, _i, _miles;<\/p>\n\n\n\n<p>\u53ef\u901a\u8fc7\u7f16\u5199\u5b57\u9762\u91cf\u8fd0\u7b97\u7b26(literal operators)\u6765\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p>\u5b57\u9762\u91cf\u8fd0\u7b97\u80fd\u4ee5\u751f\uff08raw\uff09\u6a21\u5f0f\uff0c\u6216\u8005\u719f\uff08cooked\uff09\u6a21\u5f0f\u8fd0\u884c\uff1b<\/p>\n\n\n\n<p><strong>\u751f\u6a21\u5f0f\u5373\u539f\u59cb\u6a21\u5f0f\uff0c\u719f\u6a21\u5f0f\u5373\u89e3\u91ca\u540e\u6a21\u5f0f\uff1b<\/strong><\/p>\n\n\n\n<p>\u751f\u6a21\u5f0f\u4e2d\uff0c\u5b57\u9762\u91cf\u63a5\u53d7\u4e00\u4e2a\u5b57\u7b26\u5e8f\u5217\uff1b<\/p>\n\n\n\n<p>\u719f\u6a21\u5f0f\u4e2d\uff0c\u5b57\u9762\u91cf\u63a5\u53d7\u4e00\u79cd\u7ecf\u8fc7\u89e3\u91ca\u7684\u7279\u5b9a\u7c7b\u578b\uff1b<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C++\u5141\u8bb8\u5b9a\u4e49\u81ea\u5df1\u7684\u5b57\u9762\u91cf\u3002\u7528\u6237\u5b9a\u4e49\u7684\u5b57\u9762\u91cf\u5e94\u8be5\u4ee5\u4e0b\u5212\u7ebf\u5f00\u5934\uff0c\u5e76\u901a\u8fc7\u7f16\u5199\u5b57\u9762\u91cf\u8fd0\u7b97\u7b26\u51fd\u6570\uff08literal op [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/3874"}],"collection":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3874"}],"version-history":[{"count":6,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/3874\/revisions"}],"predecessor-version":[{"id":3883,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/3874\/revisions\/3883"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3874"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3874"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3874"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}