C++ 程式中的原始字串文字


在本文中,我們將討論 C++ 中的原始字串文字、其含義和示例。

C++ 中存在跳脫字元,如 “\n” 或 “\t”。當我們嘗試列印跳脫字元時,它不會在輸出中顯示。使用 R”(包含跳脫字元的字串)” 使用的原始字串文字可以在輸出螢幕上顯示跳脫字元。在字串前面使用 R 之後,跳脫字元將顯示在輸出中。

示例

讓我們透過一個示例來理解這一點

 線上示例

#include <iostream>
using namespace std;
int main(){
   string str = "tutorials\npoint\n" ;
   // A Raw string
   string str_R = R"(tutorials\npoint\n)";
   cout <<"String is: "<<str << endl;
   cout <<"Raw String is: "<<str_R;
   return 0;
}

輸出

如果我們執行以上程式碼,將生成以下輸出 -

String is: tutorials
point
Raw String is: tutorials\npoint\n

示例

 線上示例

#include <iostream>
using namespace std;
int main(){
   string str = "tutorials\ttoint\t" ;
   // A Raw string
   string str_R = R"(tutorials\tpoint\t)";
   cout <<"String is: "<<str << endl;
   cout <<"Raw String is: "<<str_R;
   return 0;
}

輸出

如果我們執行以上程式碼,將生成以下輸出 -

String is: tutorials toint
Raw String is: tutorials\tpoint\t

更新時間:2020 年 4 月 22 日

530 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始使用
廣告
© . All rights reserved.