Extraction operator >>

I am working on how to extract the real and imaginary component of a complex number (example 67 - 23i). My professor gave us a clue and said used the extraction operator>>.

I look on the website and never was surprise to see that they have these many choices

//MEMBER FUNCTION
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
istream& operator>> (bool& val );
istream& operator>> (short& val );
istream& operator>> (unsigned short& val );
istream& operator>> (int& val );
istream& operator>> (unsigned int& val );
istream& operator>> (long& val );
istream& operator>> (unsigned long& val );
istream& operator>> (float& val );
istream& operator>> (double& val );
istream& operator>> (long double& val );
istream& operator>> (void*& val ); 
istream& operator>> (streambuf* sb ); 
istream& operator>> (istream& ( *pf )(istream&));
istream& operator>> (ios& ( *pf )(ios&));
istream& operator>> (ios_base& ( *pf )(ios_base&)); 
istream& operator>> (istream& is, char& ch );
istream& operator>> (istream& is, signed char& ch );
istream& operator>> (istream& is, unsigned char& ch );

//GLOBAL FUNCTION
istream& operator>> (istream& is, char* str );
istream& operator>> (istream& is, signed char* str );
istream& operator>> (istream& is, unsigned char* str );


My question is:
1. Can use one of these functions, enter the complex number and immediately get the REAL and IMAGINARY part or do I write a code to parse my complex number myself to find the REAL amd IMAGINARY part?
I'm not too exPerinced in this, but I beleive that your imaginary number would be treated as an int. then you would need to parse the array using the '-' sign as the divider. The best thing to use is std::ifstream.getline()
Last edited on
There are many more than what you've listed, and there are other ways to parse strings besides the various overloads of operator>>, but to follow your requirements, it could be something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <sstream>
#include <complex>

int main()
{
    std::istringstream buf("67 - 23i");

    char minus, i;
    double re, im;
    buf >> re >> minus >> im >> i;
    if(!buf || minus != '-' || i != 'i')
        std::cout << "Parse error\n";
    else
        std::cout << std::complex<double>(re, im) << '\n';
}

demo: http://ideone.com/l084xV
I ... was surprise to see that they have these many choices

And there are even more! Including, from

complex operators
http://www.cplusplus.com/reference/std/complex/complex/operators/

1
2
3
4
5
6
template<class T, class charT, class traits>
  basic_istream<charT,traits>&
    operator>> (basic_istream<charT,traits>& istr, complex<T>& rhs);
template<class T, class charT, class traits>
  basic_ostream<charT,traits>&
    operator<< (basic_ostream<charT,traits>& ostr, const complex<T>& rhs);


but it uses a different format:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <complex>
using namespace std;

int main()
{
    const char input[] = " ( 3 , -7 ) ";
    cout << "input = " << input << "\n\n";

    std::istringstream iis(input);
    std::complex<double> c;
    iis >> c;

    cout << "c: real = " << c.real() << ", imag = " << c.imag() << "\n\n";

    cout << "output = " << c << "\n\n";

    return 0;
}


input =  ( 3 , -7 )

c: real = 3, imag = -7

output = (3,-7)



Last edited on
Topic archived. No new replies allowed.