operator<<,>>(std::basic_string)
cppreference.net에서 가져옴
| 헤더 <string>
|
||
template< class CharT, class Traits, class Allocator >
std::basic_ostream<CharT, Traits>&
operator<<( std::basic_ostream<CharT, Traits>& os,
const std::basic_string<CharT, Traits, Allocator>& str );
|
에 정의됨 (1) | |
template< class CharT, class Traits, class Allocator >
std::basic_istream<CharT, Traits>&
operator>>( std::basic_istream<CharT, Traits>& is,
std::basic_string<CharT, Traits, Allocator>& str );
|
(2) | |
1) FormattedOutputFunction처럼 동작한다. 센트리 객체를 생성하고 검사한 후, 출력 형식 채움을 결정한다.
그런 다음 결과 시퀀스 seq (str의 내용에 채움을 더한 것) 의 각 문자를 출력 스트림 os에 마치 os.rdbuf()->sputn(seq, n)을 호출하는 것처럼 삽입한다. 여기서 n는 std::max(os.width(), str.size())이다.
마지막으로, os.width(0)을 호출하여 std::setw의 효과(있는 경우)를 취소한다.
|
|
(C++17부터) |
2) FormattedInputFunction처럼 동작한다. 센트리 객체를 생성하고 검사한 후(선행 공백을 건너뛸 수 있음), 먼저
str을 str.erase()로 지운 다음, is에서 문자를 읽고 str에 마치 str.append(1, c)를 통해 추가한다. 다음 조건 중 하나가 참이 될 때까지 계속한다:
N문자가 읽힘. 여기서N은is.width()이고,is.width() > 0인 경우. 그렇지 않으면N는str.max_size()이다,- 스트림
is에서 파일 끝 조건이 발생함, 또는 std::isspace(c, is.getloc())이true인 경우 (이 공백 문자는 입력 스트림에 남는다). 여기서c는is의 다음 문자이다.
문자가 추출되지 않으면 std::ios::failbit가 is에 설정되며, 이는 std::ios_base::failure를 던질 수 있다.
is.width(0)을 호출하여 std::setw의 효과(있는 경우)를 취소한다. 예외
1)
출력 중 예외가 발생하면
std::ios_base::failure
를 throw할 수 있습니다.
2)
문자가
is
로부터 추출되지 않으면(예: 스트림이 파일 끝에 도달했거나 공백만으로 구성된 경우), 또는 입력 중 예외가 발생하면
std::ios_base::failure
를 던질 수 있습니다.
매개변수
| os | - | 문자 출력 스트림 |
| is | - | 문자 입력 스트림 |
| str | - | 삽입되거나 추출될 문자열 |
반환값
1)
os
2)
is
예제
이 코드 실행하기
#include <iostream> #include <sstream> #include <string> int main() { std::string greeting = "Hello, whirled!"; std::istringstream iss(greeting); std::string hello_comma, whirled, word; iss >> hello_comma; iss >> whirled; std::cout << greeting << '\n' << hello_comma << '\n' << whirled << '\n'; // Reset the stream iss.clear(); iss.seekg(0); while (iss >> word) std::cout << '+' << word << '\n'; }
출력:
Hello, whirled! Hello, whirled! +Hello, +whirled!
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 25 | C++98 | n 이 os. width ( ) 와 str. size ( ) 중 더 작은 값이었음 | n 이 둘 중 더 큰 값임 |
| LWG 90 | C++98 |
std::
isspace
(
c, getloc
(
)
)
로 공백을 확인했으나,
getloc
이
<string>
에 선언되지 않음
|
getloc
(
)
를
is. getloc ( ) 로 대체함 |
| LWG 91 | C++98 |
operator>>
가
FormattedInputFunction 으로 동작하지 않음 |
FormattedInputFunction 으로 동작함 |
| LWG 211 | C++98 |
operator>>
가 문자를 추출하지 않을 때
failbit
를 설정하지 않음
|
failbit
를 설정함
|
| LWG 435 | C++98 |
문자가
os.
rdbuf
(
)
-
>
sputn
(
str.
data
(
)
, n
)
로 삽입되었고,
LWG issue 25 의 해결로 인해 os. width ( ) 가 str. size ( ) 보다 클 때 동작이 정의되지 않음 |
패딩을 먼저 결정하고
패딩된 문자 시퀀스를 대신 삽입함 |
| LWG 586 | C++98 |
operator<<
가
FormattedOutputFunction 으로 동작하지 않음 |
FormattedOutputFunction 으로 동작함 |
참고 항목
|
(C++17)
|
string view에 대한 스트림 출력을 수행합니다
(함수 템플릿) |