i want to read an array of complex numbers (in a+bi form). There are several suggestions I found on the internet, however those methods only result the real part, while the imaginary part is always 0. And infact the real part of the next number is the imaginary of the previous number.
For example, I have an text file as follow:
2+4i
1+5i
7+6i
And here is a suggestion for reading complex data
int nRow = 3;
std::complex<int> c;
std::ifstream fin("test.txt");
std::string line;
std::vector<std::complex<int> > vec;
vec.reserve(nRow);
while (std::getline(fin, line))
{
std::stringstream stream(line);
while (stream >> c)
{
vec.push_back(c);
}
}
for (int i = 0; i < nRow; i++){
cout << vec[i].real() << "\t" << vec[i].imag() << endl;
}
while (1);
return 0;
And the output result is:
2 0
4 0
1 0
Is there a proper way to read a+bi complex numbers from text file? Or do I have to read the data as a string, and then process the string to extract and convert it back to complex numer?
Thanks !