Namespaces
Variants

std:: quoted

From cppreference.net
< cpp ‎ | io ‎ | manip
헤더 파일에 정의됨 <iomanip>
template < class CharT >

/*unspecified*/ quoted ( const CharT * s,

CharT delim = CharT ( '"' ) , CharT escape = CharT ( ' \\ ' ) ) ;
(1) (C++14부터)
template < class CharT, class Traits, class Allocator >

/*unspecified*/ quoted ( const std:: basic_string < CharT, Traits, Allocator > & s,

CharT delim = CharT ( '"' ) , CharT escape = CharT ( ' \\ ' ) ) ;
(2) (C++14부터)
template < class CharT, class Traits >

/*unspecified*/ quoted ( std:: basic_string_view < CharT, Traits > s,

CharT delim = CharT ( '"' ) , CharT escape = CharT ( ' \\ ' ) ) ;
(3) (C++17부터)
template < class CharT, class Traits, class Allocator >

/*unspecified*/ quoted ( std:: basic_string < CharT, Traits, Allocator > & s,

CharT delim = CharT ( '"' ) , CharT escape = CharT ( ' \\ ' ) ) ;
(4) (C++14부터)

CSV나 XML에서 볼 수 있는 따옴표로 묶인 문자열의 삽입 및 추출을 허용합니다.

1-3) 표현식에서 사용될 때 out << quoted ( s, delim, escape ) , 여기서 out char_type CharT 와 같은 출력 스트림이고, (2,3) 오버로드의 경우 traits_type Traits 와 같으면, FormattedOutputFunction 으로 동작하며, out 에 다음과 같이 구성된 문자 시퀀스 seq 를 삽입합니다:
a) 먼저, 문자 delim 가 시퀀스에 추가됩니다.
b) 그런 다음 출력할 다음 문자가 delim 이거나 escape 와 동일한 경우를 제외하고(스트림의 traits_type :: eq 에 의해 결정됨), s 의 모든 문자에 대해 먼저 escape 의 추가 사본을 덧붙입니다.
c) 마지막으로, delim seq 에 한 번 더 추가됩니다.
그런 다음, seq. size ( ) < out. width ( ) 인 경우, out. width ( ) - seq. size ( ) 개의 채움 문자 out. fill ( ) 를 시퀀스의 끝에 추가합니다( ios_base :: left out. flags ( ) 에 설정된 경우) 또는 시퀀스의 시작 부분에 추가합니다(다른 모든 경우).
마지막으로, 결과 시퀀스의 각 문자를 다음 호출과 동일하게 출력합니다: out. rdbuf ( ) - > sputn ( seq, n ) , 여기서 n = std:: max ( out. width ( ) , seq. size ( ) ) 이며, out. width ( 0 ) 을 통해 std::setw 의 효과가 있을 경우 이를 취소합니다.
4) 표현식에서 in >> quoted ( s, delim, escape ) 가 사용될 때, 여기서 in char_type CharT 와 같고 traits_type Traits 와 같은 입력 스트림인 경우, 다음 규칙에 따라 in 으로부터 문자를 추출합니다:
a) 추출된 첫 번째 문자가 delim 와 같지 않으면(스트림의 traits_type::eq 에 의해 결정됨), 단순히 in >> s 를 수행합니다.
b) 그 외의 경우 (첫 번째 문자가 구분 기호인 경우):
1) 입력 스트림에서 skipws 플래그를 끕니다.
2) 대상 문자열을 비우기 위해 s. clear ( ) 를 호출합니다.
3) in 에서 연속적인 문자들을 추출하여 s 에 추가합니다. 단, escape 문자가 추출될 때마다 이를 무시하고 다음 문자를 s 에 추가합니다. 추출은 ! in == true 가 되거나 이스케이프 처리되지 않은 delim 문자를 찾을 때 중단됩니다.
4) 최종(이스케이프되지 않은) delim 문자를 버립니다.
5) 입력 스트림의 skipws 플래그를 원래 값으로 복원합니다.

목차

매개변수

s - 삽입하거나 추출할 문자열
delim - 구분자로 사용할 문자, 기본값은 "
escape - 이스케이프 문자로 사용할 문자, 기본값은 \

반환값

설명된 동작이 일어나도록 지정되지 않은 타입의 객체를 반환합니다.

예외

std::ios_base::failure 를 던집니다. 단, operator >> 또는 operator << 가 예외를 던지는 경우에 한합니다.

참고 사항

Feature-test 매크로 표준 기능
__cpp_lib_quoted_string_io 201304L (C++14) std::quoted

예제

#include <iomanip>
#include <iostream>
#include <sstream>
void default_delimiter()
{
    const std::string in = "std::quoted() quotes this string and embedded \"quotes\" too";
    std::stringstream ss;
    ss << std::quoted(in);
    std::string out;
    ss >> std::quoted(out);
    std::cout << "Default delimiter case:\n"
                 "read in     [" << in << "]\n"
                 "stored as   [" << ss.str() << "]\n"
                 "written out [" << out << "]\n\n";
}
void custom_delimiter()
{
    const char delim{'$'};
    const char escape{'%'};
    const std::string in = "std::quoted() quotes this string and embedded $quotes$ $too";
    std::stringstream ss;
    ss << std::quoted(in, delim, escape);
    std::string out;
    ss >> std::quoted(out, delim, escape);
    std::cout << "Custom delimiter case:\n"
                 "read in     [" << in << "]\n"
                 "stored as   [" << ss.str() << "]\n"
                 "written out [" << out << "]\n\n";
}
int main()
{
    default_delimiter();
    custom_delimiter();
}

출력:

Default delimiter case:
read in     [std::quoted() quotes this string and embedded "quotes" too]
stored as   ["std::quoted() quotes this string and embedded \"quotes\" too"]
written out [std::quoted() quotes this string and embedded "quotes" too]
Custom delimiter case:
read in     [std::quoted() quotes this string and embedded $quotes$ $too]
stored as   [$std::quoted() quotes this string and embedded %$quotes%$ %$too$]
written out [std::quoted() quotes this string and embedded $quotes$ $too]

참고 항목

(C++20)
인수의 형식화된 표현을 새 문자열에 저장합니다
(함수 템플릿)