Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      operator<<,>>(std::complex)

      From cppreference.com
      <cpp‎ |numeric‎ |complex
       
       
       
       
      Defined in header<complex>
      template<class T,class CharT,class Traits>

      std::basic_ostream<CharT, Traits>&

          operator<<(std::basic_ostream<CharT, Traits>& os,conststd::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)
      1) Writes toos the complex number in the form(real, imaginary).
      2) Reads a complex number fromis. The supported formats are
      • real
      • (real)
      • (real, imaginary)

      where the input forreal andimaginary must be convertible toT.

      If an error occurs callsis.setstate(ios_base::failbit).

      Contents

      [edit]Exceptions

      May throwstd::ios_base::failure on stream errors.

      [edit]Parameters

      os - a character output stream
      is - a character input stream
      x - the complex number to be inserted or extracted

      [edit]Return value

      1)os
      2)is

      [edit]Notes

      1) As the comma may be used in the current locale as decimal separator, the output may be ambiguous. This can be solved withstd::showpoint which forces the decimal separator to be visible.
      2) The input is performed as a series of simple formatted extractions. Whitespace skipping is the same for each of them.

      [edit]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();}

      [edit]Example

      Run this code
      #include <complex>#include <iostream> int main(){std::cout<<std::complex<double>{3.14,2.71}<<'\n';}

      Possible output:

      (3.14,2.71)
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/numeric/complex/operator_ltltgtgt&oldid=150875"

      [8]ページ先頭

      ©2009-2025 Movatter.jp