C++ get the difference between cin, getchar, get, getline

source:https://www.cnblogs.com/shrimp-can/p/5241544.html

1.cin>>

1)The most common is to get a character or number of input, such as

int a,b;

cin>>a>>b;

Note: cin>> will automatically filter out invisible characters (such as the space carriage return tab, etc.). If you don't want to filter out whitespace characters, you can use the noskipws stream to control it.

The following program does not filter out invisible characters. The input space characters are stored in input[1] and can also be output.

2)Get the input string, you can use an array or string type. Such as

char a[20];

cin>>a;

cout<<a<<endl;

Or string type:

string s;

cin>>s;

cout<<s<<endl;

Note: When you encounter a space, a carriage return, etc., you will end up getting the input string, and the following string will be filtered out (stored in the input stream). If you need to enter a string later, it will be obtained from the previously stored string.

In case of space, carriage return, etc.

Po is stored in the string.

 

2.cin.get()

1)Cin.get (character variable name), used to receive characters, only get one character, can receive spaces, end of carriage return

2)Cin.get (array name, number of characters received), used to receive a string, can receive a space, and end the carriage return.

Note: The last character of the array will be '\0', and the number of received characters is n. If the input string is greater than or equal to n, the actual input received is the first n-1 characters of the string, including spaces. Does not include a carriage return, it will end when you encounter a carriage return), it will automatically add a '\0' to the back.

3)Cin.get(), with no arguments, is mainly used to discard unwanted characters in the input stream, or to discard the carriage return, that is, to discard a character in the input stream.

Without cin.get(), the following s will continue to read from the input stream.

With cin.get(), the s after h is omitted.

3.cin.getline()

Actually cin.getline (receive string to m, receive number n, end character). Receive a string, receive spaces, etc. The last character is ‘\0’. The terminator can be set by setting the third parameter. The default is carriage return. m cannot be a string type.

Note: Actually received one less than n because the last character is '\0'.

4.getline()

Used for the string class. Use the header file #include<string> as needed. Getline(cin,string s), receive a string, can receive spaces, enter, etc.

The difference with cin.getline(): 1.cin.getline() receives an array of input strings, and getline() is of type string.

2.cin.getline() can receive spaces, but can't receive carriage returns; getline() can receive spaces and carriage returns

3.cin.getline() will be '\0' at the end of the array, getline() will not

 

5.gets()

Gets(m) is used for the string class and needs to include #include<string>. Can receive spaces and end the carriage return. Can be used for multidimensional arrays.

 

6.getchar()

m=getchar(), including #include<string>

reference:

http://blog.csdn.net/jiangxinnju/article/details/20492453

No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

Powered by Blogger.