complex_operator_ltltgtgt
complex_operator_ltltgtgt (복소수 스트림 입출력 연산자)
복소수에 대한 스트림 삽입(<<)과 추출(>>) 연산자예요. (real, imaginary) 형태로 출력하고, 여러 입력 형태를 지원해요.
출처: cppreference
본문
<complex> 헤더에 정의되어 있고, 시그니처는 다음과 같아요.
template< class T, class CharT, class Traits >
std::basic_ostream<CharT, Traits>&
operator<<( std::basic_ostream<CharT, Traits>& os, const std::complex<T>& x );
(1)
template< class T, class CharT, class Traits >
std::basic_istream<CharT, Traits>&
operator>>( std::basic_istream<CharT, Traits>& is, std::complex<T>& x );
(2)
-
- 복소수를
(real, imaginary)형태로 os에 써요.
- 복소수를
-
- is에서 복소수를 읽어요. 지원되는 형식은 다음과 같아요.
real(real)(real, imaginary)
여기서 real과 imaginary의 입력은 T로 변환 가능해야 해요.
오류가 발생하면 is.setstate(ios_base::failbit)를 호출해요.
예외 (Exceptions)
스트림 오류 시 std::ios_base::failure를 던질 수 있어요.
매개변수 (Parameters)
- os — 문자 출력 스트림
- is — 문자 입력 스트림
- x — 삽입하거나 추출할 복소수
반환값 (Return value)
-
- os
-
- is
참고 (Notes)
-
- 현재 로케일에서 쉼표가 소수 구분자로 쓰일 수 있으므로 출력이 모호할 수 있어요.
std::showpoint로 소수 구분자가 보이게 강제하면 해결할 수 있어요.
- 현재 로케일에서 쉼표가 소수 구분자로 쓰일 수 있으므로 출력이 모호할 수 있어요.
-
- 입력은 일련의 단순 형식 추출로 수행돼요. 공백 건너뛰기는 각각 동일해요.
가능한 구현 (Possible implementation)
template<class T, class CharT, class Traits>
basic_ostream<CharT, Traits>&
operator<<(basic_ostream<CharT, Traits>& o, const complex<T>& x)
{
basic_ostringstream<CharT, Traits> s;
s.flags(o.flags());
s.imbue(o.getloc());
s.precision(o.precision());
s << '(' << x.real() << ',' << x.imag() << ')';
return o << s.str();
}
예제 (Example)
이 코드를 실행해 봐요.
#include <complex>
#include <iostream>
int main()
{
std::cout << std::complex<double> {3.14, 2.71} << '\n';
}
가능한 출력:
(3.14,2.71)