C++ 中的原生字串文字


在 C++11 及以上版本中,有一個概念叫做 Raw 字串。在字串中,我們使用 \n、\t 等不同的字元。它們有不同的含義。\n 用於將游標返回下一行,\t 生成製表符等。

如果我們希望在輸出中列印這些字元而不看到它們產生的效果,我們可以使用 Raw 字串模式。要將一個字串轉換為 Raw 字串,我們必須在字串前新增 "R"。

Input: A string "Hello\tWorld\nC++"
Output: "Hello\tWorld\nC++"

演算法

Step 1: Get the string
Step 2: Use R before string to make it raw string
Step 3: End

示例程式碼

#include<iostream>
using namespace std;
main() {
   string my_str = "Hello\tWorld\nC++";
   string raw_string = R"Hello\tWorld\nC++";
   cout << "Normal String: " << endl;
   cout << my_str <<endl;
   cout << "RAW String: " << endl;
   cout << raw_string;
}

輸出

Normal String:
Hello World
C++
RAW String:
Hello\tWorld\nC++

更新於: 30-Jul-2019

426 次瀏覽

開啟您的 職業

完成課程取得認證

開始操作
廣告
© . All rights reserved.