最近学习时C++遇到个小麻烦,我想在程序中放一段JSON代码,比如说读取steem区块链创世块信息的JSON: >`{"jsonrpc":"2.0","method":"call","params":["databae_api","get_block",[1]],"id":1}`  (图源 :[pixabay](https://pixabay.com/)) 如果在Python中,我可以直接这样写: >`j1 = '{"jsonrpc":"2.0","method":"call","params":["databae_api","get_block",[1]],"id":1}'` 但是在C++中,这样是行不通的。如果想在字符串中包含双引号,我们需要使用转义字符**`\`**,也就是说,我们想存储上边的JSON字符串,我们需要写成类似这样的方式: >`string s1 = "{\"jsonrpc\": \"2.0\", \"method\": \"call\", \"params\": [\"database_api\", \"get_block\", [1]], \"id\": 1}";` 如果我们的数据中包含很多需要转义的内容,那**`\`**会输入到让人抓狂,一不小心漏一个或者多一个,那么要么编译不过去,要么会导致程序行为和期待的不一致。如果能像Python那样方式使用,就爽歪歪了。 好消息是,在C++11标准中,引入了`raw_characters`概念,使用方式如下: >**`prefix(optional) R "delimiter( raw_characters )delimiter"`** 详情可以参考:[string literal](http://en.cppreference.com/w/cpp/language/string_literal) [string literal](http://en.cppreference.com/w/cpp/language/string_literal)里介绍得很详细,支持很多功能,我一向的原则是用啥学啥,所以其它的先不研究了,只学一下raw strings就行了。 # 简单例子 先来个简单的例子,如果想实现之前说的,类似Python的写法,该如何做呢?答案很简单: >`string s3 = R"({"jsonrpc":"2.0","method":"call","params":["databae_api","get_block",[1]],"id":1})";` 其实就是把字符串原封不动地放到`R"()"`当中即可。妈妈再也不用担心我数转义字符累坏眼睛了。 # 多行文本 继续那Python举例,在Python中,我们是可以传入多行文本的,比如说:  那么这个`R"()"`是否也能这样用呢?答案是肯定的:  # 定界符/delimiter 通过上边的简单例子以及多行文本的例子,我们发现这个`R"()"`就是个超级法宝啊,我用我用我使劲用,结果我擦,咋崩溃了呢? 好吧,让我们来看一个让人崩溃的例子: >`string s = R"(Let's test R"()")";` 我的目的是在s中存入:**`Let's test R"()"`**,按照我们之前的学习,把内容放到 `R"()"`中就应该可以了,可是为啥编译不过去呢?答案就是编译器处理的时候遇到字符串中的第一个`)"`以为已经是字符串结尾了,那后边真正的结束符`)"`,编译器反而不知道如何处理了。 这种情况也不能赖编译器傻,毕竟已经比我聪明多了。好吧,不开玩笑,这种情况,我们需要用到***`定界符/delimiter`***,让编译器知道真正的起止位置。 定界符可以用任何内容,比如`<abcd>`,那么`R"()"`就变成`R"<abcd>()<abcd>"`,上述代码就可以写成 >`string s4 = R"<abcd>(Let's test R"()")<abcd>";` `cout << s4 << endl;` 上述代码将会输出: 好像应该写成:`Let's test R"()"!`更好看,不过我们重点是介绍raw strings,其它细节滥就滥吧(其实是我懒得改)。 # 参考链接 * [string literal](http://en.cppreference.com/w/cpp/language/string_literal)
author | oflyhigh |
---|---|
permlink | c-11-raw-strings |
category | programming |
json_metadata | {"tags":["programming","cn-programming","study","learning","cn"],"image":["https://steemitimages.com/DQmRkLq6rRew3mHfx4vYGWyqpC8wSebLPeC2iZCXAdpuGkR/image.png","https://steemitimages.com/DQmUr63mnp7UnTPULqm5BapKEfPk6L3DzWLPbqiKt6fuqXw/image.png","https://steemitimages.com/DQmbtFL7NBo1BqNgwvhqwoFenmxLY9qyY54ufmWt6P7uqAa/image.png","https://steemitimages.com/DQmcN4iE6xJAh9GSsMrm4zZDbt2zq6jXi1QoUjz4ZtbJyDh/image.png"],"links":["https://pixabay.com/","http://en.cppreference.com/w/cpp/language/string_literal"],"app":"steemit/0.1","format":"markdown"} |
created | 2018-04-22 05:06:48 |
last_update | 2018-04-22 05:06:48 |
depth | 0 |
children | 4 |
last_payout | 2018-04-29 05:06:48 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 136.360 HBD |
curator_payout_value | 24.198 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 2,237 |
author_reputation | 6,275,463,754,702,711 |
root_title | "每天进步一点点:C++11 中的原生字符串/ raw strings" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 51,423,135 |
net_rshares | 24,565,089,275,010 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
pharesim | 0 | 81,909,605,674 | 0.17% | ||
abit | 0 | 194,198,453,843 | 100% | ||
adm | 0 | 10,481,341,846,647 | 60% | ||
slowwalker | 0 | 2,350,587,193,888 | 20% | ||
blockchainbilly | 0 | 18,894,047,984 | 50% | ||
deanliu | 0 | 1,177,332,711,981 | 100% | ||
joythewanderer | 0 | 183,385,790,886 | 40% | ||
ace108 | 0 | 248,262,693,659 | 25% | ||
laoyao | 0 | 35,390,197,145 | 100% | ||
somebody | 0 | 1,265,943,406,586 | 100% | ||
midnightoil | 0 | 111,627,230,611 | 100% | ||
xiaohui | 0 | 704,813,332,560 | 100% | ||
oflyhigh | 0 | 2,317,442,269,202 | 100% | ||
xiaokongcom | 0 | 10,358,631,880 | 100% | ||
yulan | 0 | 14,854,713,580 | 100% | ||
chinadaily | 0 | 225,652,620,248 | 100% | ||
helene | 0 | 674,232,994,376 | 100% | ||
ffcrossculture | 0 | 33,342,167,894 | 100% | ||
ethansteem | 0 | 186,113,770,735 | 100% | ||
englishtchrivy | 0 | 56,840,015,096 | 22% | ||
profitgenerator | 0 | 469,504,413 | 100% | ||
damarth | 0 | 80,440,379,193 | 3% | ||
jianghao | 0 | 231,676,028 | 10% | ||
drac59 | 0 | 1,611,727,783 | 90% | ||
mrtv2 | 0 | 49,872,742,697 | 100% | ||
steemtruth | 0 | 8,246,929,254 | 10% | ||
lalala | 0 | 76,994,635,933 | 100% | ||
dapeng | 0 | 52,508,928,133 | 30% | ||
devilwsy | 0 | 2,229,102,735 | 100% | ||
janiceting | 0 | 2,214,345,198 | 100% | ||
abraomarcos | 0 | 2,297,678,492 | 100% | ||
lydiachan | 0 | 21,131,226,249 | 100% | ||
newhope | 0 | 1,854,724,403,986 | 24% | ||
dragon40 | 0 | 2,247,283,471 | 10% | ||
blackbunny | 0 | 80,544,499,216 | 100% | ||
ripperone | 0 | 1,163,973,595,901 | 25% | ||
elizacheng | 0 | 2,254,491,707 | 10% | ||
bxt | 0 | 158,054,034,346 | 100% | ||
lingfei | 0 | 55,769,862,490 | 100% | ||
yyyy | 0 | 455,588,158 | 100% | ||
austinsandersco | 0 | 780,364,216 | 70% | ||
kingofdew | 0 | 44,289,716,770 | 100% | ||
emcvay | 0 | 176,863,321 | 10% | ||
mandagoi | 0 | 2,815,676,159 | 21% | ||
wylo | 0 | 613,964,238 | 100% | ||
jkkim | 0 | 60,586,510 | 10% | ||
ebejammin | 0 | 5,858,911,885 | 100% | ||
nanosesame | 0 | 36,425,413,516 | 30% | ||
cryptohustler | 0 | 22,205,128,212 | 100% | ||
exec | 0 | 82,552,932,374 | 100% | ||
eval | 0 | 797,519,158 | 100% | ||
speeding | 0 | 3,706,109,707 | 100% | ||
tsinrong | 0 | 615,281,493 | 100% | ||
walkinharmony | 0 | 15,636,319,289 | 50% | ||
asterix87 | 0 | 13,737,208,746 | 100% | ||
marram | 0 | 2,489,369,838 | 100% | ||
canbethisone | 0 | 6,691,514,758 | 50% | ||
abetterworld | 0 | 3,159,250,145 | 100% | ||
raili | 0 | 10,806,009,738 | 100% | ||
that1consultant | 0 | 302,353,392 | 100% | ||
sanzo | 0 | 273,612,700 | 100% | ||
davaowhenyo | 0 | 614,503,785 | 100% | ||
allenshayzar | 0 | 614,503,785 | 100% | ||
travelgirl | 0 | 43,875,969,874 | 36% | ||
raku | 0 | 613,122,478 | 100% | ||
resteeming | 0 | 614,860,000 | 100% | ||
ravenousappetite | 0 | 614,503,440 | 100% | ||
aabb | 0 | 10,889,337,037 | 100% | ||
auntigormint | 0 | 537,652,272 | 100% | ||
mrliga | 0 | 18,586,789,487 | 100% | ||
sweethoney | 0 | 253,884,794 | 100% | ||
liangfengyouren | 0 | 1,010,511,804 | 50% | ||
idx | 0 | 17,896,660,897 | 100% | ||
jiangchen | 0 | 8,955,450,876 | 100% | ||
chaofanjun | 0 | 5,650,702,948 | 100% | ||
lancy | 0 | 4,452,421,370 | 92% | ||
bearpaw | 0 | 2,620,000,012 | 100% | ||
freedom-fighter | 0 | 614,503,785 | 100% | ||
technologynepal | 0 | 613,122,478 | 100% | ||
davidke20 | 0 | 2,012,194,032 | 15% | ||
rosatravels | 0 | 38,799,115,836 | 20% | ||
dgorbunov | 0 | 531,688,703 | 100% | ||
khalilad | 0 | 611,243,358 | 100% | ||
ms8988 | 0 | 602,448,519 | 100% | ||
antone | 0 | 360,037,362 | 100% | ||
stakuza | 0 | 513,835,827 | 100% | ||
ikonik | 0 | 393,531,737 | 100% | ||
vfxness | 0 | 67,564,598 | 100% | ||
lemminon | 0 | 611,243,358 | 100% | ||
bobdos | 0 | 3,598,957,753 | 7% | ||
heyeshuang | 0 | 645,908,872 | 100% | ||
razor80 | 0 | 587,563,371 | 100% | ||
fastiduos | 0 | 611,243,358 | 100% | ||
winniex | 0 | 3,747,888,091 | 10% | ||
rebecca80 | 0 | 313,882,503 | 100% | ||
weavingwords | 0 | 58,842,041,650 | 100% | ||
nitro.live | 0 | 3,268,416,324 | 100% | ||
chaerin | 0 | 429,359,775 | 100% | ||
cnbuddy | 0 | 3,233,121,146 | 0.1% | ||
chann | 0 | 2,786,663,308 | 10% | ||
yammyamm | 0 | 314,788,752 | 100% | ||
lebin | 0 | 33,111,984,603 | 30% | ||
coindzs | 0 | 120,406,412 | 100% | ||
saury | 0 | 303,723,844 | 100% | ||
patrickzhou | 0 | 8,695,549,400 | 100% | ||
ewq | 0 | 745,289,769 | 3% | ||
maiyude | 0 | 13,991,777,737 | 10% | ||
mycat | 0 | 153,394,048 | 12% | ||
historylover | 0 | 369,796,861 | 100% | ||
skyqi | 0 | 592,992,500 | 100% | ||
btccurrency1 | 0 | 55,103,324 | 100% | ||
ethanlee | 0 | 11,269,481,695 | 100% | ||
kasperr | 0 | 117,577,169 | 100% | ||
amponchiek | 0 | 409,609,552 | 100% | ||
fishbb | 0 | 762,495,536 | 5% | ||
fanso | 0 | 1,178,673,531 | 100% | ||
aellly | 0 | 60,507,856 | 25% | ||
cryptoknight27 | 0 | 615,076,421 | 100% | ||
abss | 0 | 92,666,914 | 11% | ||
khayziljoy | 0 | 168,548,992 | 100% | ||
hepeng.chn | 0 | 146,635,515 | 100% | ||
hmayak | 0 | 177,272,701 | 100% | ||
cromwelljoe | 0 | 4,697,584,806 | 100% | ||
peanut77 | 0 | 611,252,347 | 100% | ||
xiaoyuanwmm | 0 | 308,206,908 | 100% | ||
moneybaby | 0 | 418,687,650 | 100% | ||
macd107 | 0 | 602,086,541 | 100% | ||
bitming | 0 | 611,217,000 | 100% |
请问,我在发表文章时,出现下面的错误,您知道怎么回事吗?
author | bitming |
---|---|
permlink | re-oflyhigh-c-11-raw-strings-20180423t025053072z |
category | programming |
json_metadata | {"tags":["programming"],"image":["https://steemitimages.com/DQmegNEcoAq7m4ZB2uyKth92zFB8pVcEsV25z4BwmvzgGUY/%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202018-04-23%20%E4%B8%8A%E5%8D%8810.48.52.png"],"app":"steemit/0.1"} |
created | 2018-04-23 02:50:54 |
last_update | 2018-04-23 02:50:54 |
depth | 1 |
children | 1 |
last_payout | 2018-04-30 02:50:54 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 219 |
author_reputation | 14,187,524,641 |
root_title | "每天进步一点点:C++11 中的原生字符串/ raw strings" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 51,591,690 |
net_rshares | 0 |
已解决
author | bitming |
---|---|
permlink | re-bitming-re-oflyhigh-c-11-raw-strings-20180423t031025923z |
category | programming |
json_metadata | {"tags":["programming"],"app":"steemit/0.1"} |
created | 2018-04-23 03:10:27 |
last_update | 2018-04-23 03:10:27 |
depth | 2 |
children | 0 |
last_payout | 2018-04-30 03:10:27 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 3 |
author_reputation | 14,187,524,641 |
root_title | "每天进步一点点:C++11 中的原生字符串/ raw strings" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 51,594,273 |
net_rshares | 0 |
我希望可以在暑假里开始学习C++,但是我还没接触过,希望可以多多指教
author | moneybaby |
---|---|
permlink | re-oflyhigh-c-11-raw-strings-20180422t152111199z |
category | programming |
json_metadata | {"tags":["programming"],"app":"steemit/0.1"} |
created | 2018-04-22 15:21:12 |
last_update | 2018-04-22 15:21:12 |
depth | 1 |
children | 0 |
last_payout | 2018-04-29 15:21:12 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 34 |
author_reputation | 20,023,805,487,967 |
root_title | "每天进步一点点:C++11 中的原生字符串/ raw strings" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 51,503,620 |
net_rshares | 0 |
oflyhigh太厉害了,每天都能做到这么专注还有日更,像你学习
author | tsinrong |
---|---|
permlink | re-oflyhigh-c-11-raw-strings-20180422t070018133z |
category | programming |
json_metadata | {"tags":["programming"],"app":"steemit/0.1"} |
created | 2018-04-22 07:00:21 |
last_update | 2018-04-22 07:00:21 |
depth | 1 |
children | 0 |
last_payout | 2018-04-29 07:00:21 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 32 |
author_reputation | 92,231,012,305 |
root_title | "每天进步一点点:C++11 中的原生字符串/ raw strings" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 51,436,158 |
net_rshares | 0 |