Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does anyone actually use stream extraction operators?

Tags:

I've written tons of operator<<(std::ostream &, const T &) functions -- they're incredibly useful.

I've never written an operator>>(std::istream &, T &) function in real code or even used the extraction operators for built-in types (OK, maybe for std::string). Are these appropriate only for short example programs and textbooks? Is operator>> a failed feature of C++?

Questions have been asked about safely overloading stream operators. What I wonder is if anyone does this in practice.

Even for something simple like reading input from a file in C++ I can't suggest using operator>>. It's too difficult to write code that is robust in detecting and handling errors in input (or I don't know how).

If you disagree please show a good example of using operator>> -- maybe by answering that last question I linked to.


Wrapup: Thanks for the responses everyone, lots of good opinions. Manuel's answer made me reconsider my reluctance to using op>> so I accepted that one.
like image 809
Dan Avatar asked Feb 22 '10 08:02

Dan


People also ask

What character do you use as a stream extraction operator?

left-shift operator << is overloaded for stream output and is referred to as the stream insertion operator. right-shift operator >> is overloaded for stream input and is referred to as the stream extraction operator.

What is the use of extraction operator?

The extraction operator ( >> ), which is preprogrammed for all standard C++ data types, is the easiest way to get bytes from an input stream object. Formatted text input extraction operators depend on white space to separate incoming data values.

What is the purpose of insertion and extraction operator in C Plus Plus?

Input/Output Operators Overloading in C++ C++ is able to input and output the built-in data types using the stream extraction operator >> and the stream insertion operator <<. The stream insertion and stream extraction operators also can be overloaded to perform input and output for user-defined types like an object.

What is use of insertion and extraction operators?

The insertion and extraction operators are used to write information to or read information to or read information from, respectively, ostream and istream objects. By default, white space is skipped when the insertion and extraction operators are used.


1 Answers

I think stream extractor operators can be very useful when combined with STL algorithms like std::copy and with the std::istream_iterator class.

Read this answer to see what I'm talking about.

like image 64
Manuel Avatar answered Sep 22 '22 20:09

Manuel