C++20에는 다음을 포함하여 다양한 새로운 기능과 개선 사항이 포함되어 있습니다. 체재() 기능. 이 기사에서는 std::format을 사용하여 C++20에서 문자열 형식을 지정하는 방법을 살펴보겠습니다.
C++20 – 표준::형식
std::format은 형식 문자열 내부의 자리 표시자를 제공된 인수 값으로 대체하여 문자열 형식을 지정하는 방법을 제공하는 C++20에 도입된 새로운 함수입니다. 자리 표시자는 형식 문자열 내에서 {}를 사용하여 표현됩니다.
통사론:
std::string std::format(std::string_view format_string, Args... args);>
반환 유형: 이 함수는 형식화된 출력을 포함하는 std::string을 반환합니다.
C++20에서는 기본 문자열에 대한 보기를 제공하는 std::string_view라는 새로운 데이터 유형이 도입되었습니다. 문자열에 대한 포인터와 유사하게 작동하지만 추가 안전 및 편의 기능이 있습니다. Args…는 가변 매개변수를 나타냅니다. 즉, std::format 함수는 모든 유형의 가변 개수 인수를 사용할 수 있습니다.
C++ 20의 예 - std::format
예시 1:
다음 코드는 std::format을 사용하여 변수에 대한 자리 표시자로 문자열 형식을 지정하는 방법을 보여줍니다.
사랑과 좋아의 차이
C++
// C++ Program to implement> // C++ 20 - std::format> #include> #include> > using> namespace> std;> > int> main()> {> > // Declare variables> > int> num = 42;> > std::string name => 'John'> ;> > > // Use std::format to format a string with placeholders> > // for variables> > std::string formatted_str = std::format(> > 'My name is {} and my favorite number is {}'> , name,> > num);> > > // Print formatted string to console> > std::cout << formatted_str << std::endl;> > > return> 0;> }> |
SQL 데이터 유형
>
>
산출
My name is John and my favorite number is 42>
위의 예에는 두 개의 자리 표시자({})가 포함된 형식 문자열이 있습니다. 변수 name과 num의 값을 std::format 함수에 전달합니다. 이 함수는 자리 표시자를 변수 값으로 바꿉니다. 결과 문자열은 formatted_str 변수에 저장된 후 콘솔에 인쇄됩니다.
예시 2:
다음 예에서는 std::format의 위치 인수를 이해합니다.
C++
빠른 정렬
// C++ Program to implement> // C++ 20 - std::format> #include> #include> > int> main()> {> > // Declare an integer variable named num> > // and initialize it with the value 42> > int> num = 42;> > // Declare a string variable named name> > // and initialize it with the value 'John'> > std::string name => 'John'> ;> > > // Call the std::format function to create a formatted> > // string with placeholders for num and name The first> > // placeholder is represented by {0} and is replaced by> > // the value of num The second placeholder is> > // represented by {1} and is replaced by the value of> > // name> > std::string formatted_str = std::format(> > 'My name is {1} and my favorite number is {0}'> , num, name);> > > // Print the formatted string to the console> > std::cout << formatted_str << std::endl;> > return> 0;> }> |
자바 xor
>
>
산출
My name is John and my favorite number is 42>
위의 예에서는 std::format 함수의 인수 순서를 반대로 하고 자리 표시자에 위치 인덱스를 추가했습니다. 첫 번째 자리 표시자는 num 값으로 대체되고 두 번째 자리 표시자는 name 값으로 대체됩니다.
예시 3:
다음 예시에서는 std::format이 문자열 형식 지정을 위한 다양한 옵션을 제공하는 방법을 살펴보겠습니다. 여기에서 {} 자리 표시자를 사용하여 각 인수에 대한 형식 지정 옵션을 지정할 수 있습니다.
C++
순방향 연결
// C++ Program to implement> // C++ 20 - std::format> #include> #include> > int> main()> {> > > // Declare and initialize a double variable.> > double> num = 3.14159;> > > // Declare and initialize a string variable.> > std::string name => 'John'> ;> > > // Format a string with two placeholders, one for a> > // double and another for a string. The first> > // placeholder formats the double with two decimal> > // places and the second placeholder truncates the> > // string to two characters.> > std::string formatted_str = std::format(> > 'My name is {1:.2s} and pi is {0:.2f}'> , num, name);> > > // Print the formatted string to the console.> > std::cout << formatted_str << std::endl;> > > return> 0;> }> |
>
>
산출
My name is Jo and pi is 3.14>
위의 예에서는 자리 표시자에 서식 옵션을 추가했습니다. 첫 번째 자리 표시자는 소수점 이하 두 자리의 부동 소수점 숫자로 형식화되고, 두 번째 자리 표시자는 최대 너비가 2자인 문자열로 형식화됩니다.
결론
std::format은 사용자 정의 유형의 형식을 지정하는 기능을 포함하여 광범위한 형식 지정 옵션을 지원합니다. sprintf 및 printf와 같은 C++의 이전 문자열 형식 지정 옵션보다 더 효율적입니다.