问题
I am doing something like:
int real_part, imaginary_part;
cout<<"Please enter realpart and imaginary part"<<endl;
cin>>real_part;
cin>>imaginary_part;
complex<double> mycomplex (real_part, imaginary_part);
cout<<mycomplex<<endl; // I want to display like -2+5i but I get (-2, 5)
I am very new to c++
How can I display with i
like -2+5i
? Or I have to add i
char with imagginary part ?
回答1:
You can use std::real()
and std::imag()
to format as you like, see complex here.
Of course, you will have to check for sign yourself.
Something like this:
std::cout
<< std::real(mycomplex)
<< (std::imag(mycomplex) >= 0.0 ? "+" : "")
<< std::imag(mycomplex)
<< " i"
<< std::endl;
回答2:
You can simply write :
cout<< mycomplex.real << std::showpos << mycomplex.imag << "i" << endl;
回答3:
For the sake of completeness with the other answer. You can use std::showpos
to more easily format the output into something signed
cout << real(mycomplex) << std::showpos << imag(mycomplex) << "i";
回答4:
If you're feeling really sneaky, or are using libraries that you dont want to or cant modify everywhere (ie I use this to print Eigen matrices in an octave / matlab compatible format), you can specialize the put-to operator for your type before including <complex>
. I suspect that this is against the standard, because it mucks around in std::
, but it works in g++(7.3.1) and clang++(5.0):
/*
* this stuff can go in a header to make std::complex<> available
*/
typedef double real_t;
#include <iostream>
#define SPECIALIZED_COMPLEX_PUTTO
#ifdef SPECIALIZED_COMPLEX_PUTTO
#ifdef _LIBCPP_BEGIN_NAMESPACE_STD // for clang++ libs
_LIBCPP_BEGIN_NAMESPACE_STD
#else
namespace std {
#endif
template <typename T> class 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);
// specialization for real_t, instantiate later
template<>
basic_ostream<char>&
operator<<(basic_ostream<char> & o, const complex<real_t> & x);
#ifdef _LIBCPP_END_NAMESPACE_STD // for clang++
_LIBCPP_END_NAMESPACE_STD
#else
}
#endif
#include <complex>
/*
* below here can go in a .cpp file
*/
#ifdef SPECIALIZED_COMPLEX_PUTTO
#ifdef _LIBCPP_BEGIN_NAMESPACE_STD // for clang++ libs
_LIBCPP_BEGIN_NAMESPACE_STD
#else
namespace std {
#endif
template<>
basic_ostream<char>&
operator<<(basic_ostream<char> & o, const complex<real_t> & x)
{
basic_ostringstream<char> s;
s.flags(o.flags());
s.imbue(o.getloc());
s.precision(o.precision());
s << x.real() << std::showpos << x.imag() << 'i';
return o << s.str();
}
#ifdef _LIBCPP_END_NAMESPACE_STD // for clang++
_LIBCPP_END_NAMESPACE_STD
#else
}
#endif
int main(int argc, char * argv[])
{
std::complex<real_t> x(1.1,-2.2);
std::cout << x << "\n";
}
outputs
1.1-2.2i
alternately, an example main()
for the Eigen case:
#include <Eigen/Dense>
int main(int argc, char * argv[])
{
Eigen::Matrix<std::complex<real_t>, 3,3> x;
Eigen::IOFormat OctaveFmt(Eigen::StreamPrecision, 0, ", ", ";\n", "", "", "[", "]");
//srand((unsigned int) time(0));
x.setRandom();
std::cout << x.format(OctaveFmt) << "\n";
}
outputs a matrix in a format suitable for copy/paste into octave/matlab:
[ 0.680375-0.211234i, -0.329554+0.536459i, -0.270431+0.0268018i;
0.566198+0.59688i, -0.444451+0.10794i, 0.904459+0.83239i;
0.823295-0.604897i, -0.0452059+0.257742i, 0.271423+0.434594i]
Edit: added macros for apple clang libs.
来源:https://stackoverflow.com/questions/40245110/c-display-complex-number-with-i-in-imaginary-part