Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

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