C + + Input/Output Summary _ Input

Source: Internet
Author: User

1. the nature of the input and output

The input and output in C + + is carried out through the stream, and the output input is done by convection, typically directing a stream (redirect), emptying the stream, and adding new elements to the stream. C + + considers the input output as a byte stream, extracts bytes from the stream as input, and inserts bytes into the byte stream when output.

2. using CIN and its methods for input

  The Cin object represents the standard input as a byte stream, usually using the keyboard to generate the byte stream, and the commonly used cin Input method is cin>>abc; and the IStream class overloads the extraction operator, which can recognize these basic types unsigned/signed Char&, Char&, Short&, unsigned short&, int&,unsigned int&,long&, unsigned long&, long Long &, unsigned long long &, float& double &, long double&; for these basic types directly with CIN and extract the operator >> can be achieved. One problem is that the >> separator skips line breaks and ignores characters like blank, tab, and so on, so a program is problematic, and the loop in this code will run forever, and simply using this method to input character data is a big problem.

And for a string to be entered, if you enter a string using a method such as cin>>input;, the input will stop if you encounter blank wrapping, tabulation, and so on.

1 CharA;2 inttimes=0;3Cin>>A;4  while(a!='\ n')5 {6cout<<A;7Cin>>A;8times++;9 }Tencout<<times;

3. C + + for string, character, string class input//A good way to solve the white space, newline, etc. are not read.

  C + + pairs of strings, characters and string classes are commonly used when entering the Get (), getline () function, they are called unformatted input, because they only read characters, including whitespace, newline characters, tabs, and so on, will not do data conversion, read what is what.

The get () function defined in IStream and the Getline () function:

With get for single character input, in the case of parameters or no parameters, the Get function reads an input character, no matter what the character is a newline, the tabulation can be read directly in.

where get (void) converts the character to a shape and returns, for the Get (char&) function returns a reference to the IStream object that called it, so that you can use a method similar to Cin.get (a). Get (b). get (c); It is important to note that get does not ignore line breaks, so pay attention to the case where newline characters are accidentally read in.

Cin.get (CH) returns false if the end of the file is read, or the tail of the keyboard input is not readable, and the rest of the normal condition returns TRUE.

Comparison of Cin.get () and Cin.get (CH)
Characteristics Cin.get (CH) Ch=cin.get ()
Transport input Characteristics Direct assignment to Ch Assign the function return value to CH
After reading to a character, the function returns to References to IStream objects Character encoding, which is actually an int value
return value of function when end of file is reached Convert to False Eof

Note: The end of the keyboard input simulation is the same as the actual end of the file

Get () function

The get () function defined in IStream has a total of six overloads: When used as long as any one of the parameter rules is met

1. Int_type __clr_or_this_call get ();//no argument, only one character is read, the return value is int type

2. _myt& __clr_or_this_call Get (_elem *_str, streamsize _count);//Accept two parameters, a string header address, an int parameter to control the maximum number of read characters, to hold the end of the string /0, this parameter is larger than the read string, the function default delimiter is/N.

3. _myt& __clr_or_this_call Get (_elem *_str, Streamsize _count, _elem _delim);//The same as above, except that the function delimiter is set to its own _delim.

4._myt& __clr_or_this_call Get (_elem& _ch);//The Receive parameter is a char type reference, the function is to assign the value directly to Ch;

When testing a function, it is a bit of a discovery that a string cannot be referenced because the array is a collection of several elements, so an alias for an array cannot be established.

If this block is char &bbb = input[10]; You cannot create a reference to an array.
This effect is a reference to input[0].

5._myt& __clr_or_this_call Get (_mysb& _strbuf);

6._myt& __clr_or_this_call Get (_mysb& _strbuf, _elem _delim);//The return value of a function other than the first function is a reference to the IStream object that called it.

The above functions are used only when the idea is to use cin.get(some parameters or not), the Get () function will not read the newline characters when the word read the newline character or the original default delimiter, and these things will only remain within the primitive stream,

Char input[]char  ch;cin. Get (Input,ten,'a'<< input << endl;cin. Get (CH); cout<<ch<<endl;

Input SDSDSDA

Output SDSDSD

A

Getline () function

The Getline () function in IStream altogether two overloads

1. _myt& __clr_or_this_call getline (_elem *_str, streamsize _count);
2. _myt& __clr_or_this_call getline (_elem *_str, Streamsize _count, _elem _delim);

These two functions are similar to the 2, 3 overloads in the Get () function, except that the Getline () function automatically discards delimiters such as line breaks.

Char input[]char  ch;cin.getline (input,ten,'A  '<< input << endl;cin. Get (CH); cout<<ch<<endl;

Input SDSDSDA

Output SDSDSD

Because the delimiter is discarded, the delimiter A is discarded, and the following cin.get (CH) reads a newline character, so the output is just a newline character.

Input to the String class

You should simply cin>> to enter a string that will stop when you encounter whitespace, tabulation, line breaks, and so on.

Use Getline () to read the String class, Getline () in the VS2010 function definition in a string file, a total of two refactoring, and are all inline functions

1. Template<class _elem,
Class _traits,
Class _alloc> Inline
Basic_istream<_elem, _traits>& getline (
Basic_istream<_elem, _traits>& _istr,
Basic_string<_elem, _traits, _alloc>& _str,
Const _elem _delim)//Use the delimiter of your own definition, its parameters are stream oriented class, the second parameter is the output target string, the third parameter is a custom delimiter

2. Template<class _elem,
Class _traits,
Class _alloc> Inline
Basic_istream<_elem, _traits>& getline (
Basic_istream<_elem, _traits>& _istr,
Basic_string<_elem, _traits, _alloc>& _str)//The default delimiter is \ n

Both functions do not have a maximum number of string characters, because the string class automatically resizes itself according to the size of the string.

C + + Input/Output Summary _ Input

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.