1. Cin2, Cin.get ()3, Cin.getline ()4, Getline ()5, gets ()6, GetChar ()
Attached: Cin.ignore (); Cin.get ()//skip a character such as unwanted carriage return, space, etc.
1, cin>>
Usage 1: The most basic and most commonly used use, enter a number:
#include using namespace Std;Main (){int a,b;cin>>a>>b;cout}
Input: 2[carriage return]3[carriage return]Output: 5
Note that:>> will filter out invisible c
When learning C + +, these input functions get a little confused, here to do a summary, in order to review their own, but also hope to be able to help later, if there is a mistake in the place also ask you a lot of advice (this article all programs through the VC 6.0 run)1. Cin2, Cin.get ()3, Cin.getline ()4, Getline ()5, gets ()6, GetChar ()Attached: Cin.ignore (); Cin.get ()//skipping a character, such as the unwanted carriage return, space, and other characters1,
Original works, reproduced please specify the source: http://www.cnblogs.com/shrimp-can/p/5241544.html1.cin>>1) The most common is to get the input of a character or number, such asint A, B;cin>>a>>b;Note that:cin>> automatically filters out invisible characters (such as Space return tab, etc.). If you do not want to filter out whitespace characters, you can use NOSKIPWS flow control.The following program,
①CIN>>: Unable to receive the space, tap key and the space, tap key, carriage return delimiter;②cin.get (): Can receive a space, tap key and a carriage return as a terminator;One: You can enter a single characterFormat:Char ch;Ch=cin.get ();/cin.get (CH);Two: Can input stringFormat:Cin.get (character array name, number of elements)③getline (): Can receive a space, tap key and a carriage return as a terminator;Format:String str;//strings VariableGetlin
Cin Slow is a reason, in fact, the default time, CIN and stdin always keep in sync, that is to say, these two methods can be mixed, without worrying about the file pointer confusion, and cout and stdout are the same, the two mixed will not output order confusion. Because of this compatibility feature, CIN has a lot of extra overhead and how to disable this featur
Cin. Fail ()
Cin. Fail: determines whether the stream operation has failed. If the input fails, true is returned.
Int;Cin>;If (CIN. Fail ())
{
Cout }
Else
{
..}
If you want to enter an integer, you do not enter an integer (such as letters)
Enter a string of int 1.
0. How can we extract int and 10 respectively?
W
standard library of C has a function. The isspace (char c) function can detect whether a character is a blank character and must contain the header file of cctype.
View two situations of scanf:
Case 1: After ival1 is input, no matter how many whitespaces are entered, the system will continue to wait for ival2. The reason is that the whitespace is regarded as the input separator and will be ignored.
Int ival1, ival2;
Case 2: After the ival is input, the variable ch also reads data from the stdi
CIN and cout Slow is a reason, the default time cin and stdin always keep in sync, while cout and stdout are same, the two mixed will not output sequence confusion.The compatibility of the two causes CIN to have a lot of extra overhead.The statement that disables this attribute isStd::ios::sync_with_stdio (FALSE);Cancelling the sync will speed up a lot, but still
Input:ExecutingSTD: CIN> VDiscards any whitespace characters in the standard input stream, then reads from the standard input into variableV. It returnsSTD: CIN, Which has TypeIstream, In order to allow chained input operations. Int _ Tmain ( Int Argc, _ tchar * Argv []) { // Ask for the person's name STD: cout " Please enter your first name: " ; // Read the name STD :: String Name; // Define name
At the bottom of the poj 2406 question, a message is displayed: "This problem has huge input, use scanf instead of CIN to avoid time limit exceed ."
Think of the previous issues with tree arrays that often times out with CIN. After switching to scanf, we can use AC. It seems that the efficiency of CIN and scanf is quite different.
Reading others said Using STD:
In the process of data input using CIN operations, there are often situations where the defined data types do not match the data types of the keyboard input, so how do you solve this problem? Here's a simple example,#include using namespace Std;int main (int argc,char **argv){Long x;//here defines a long type of variable Xcin>>x;while (Cin.fail ()){Cin.clear ();//reset flag to reset all state values in the stream
CIN is a C + + input stream that can be read by >>.Judging the end of reading, there are generally two methods, depending on the contract with the input.1 ends with a special value.If you enter an integer, ending with-1, then when you read-1, make sure the read is finished.Code:intn;while(1){cin>>n;if(n==-1)break;//处理输入的合法值。}2 ends with EOF, which is the file terminator.This approach applies to file as inpu
When you learn C + +, you must be confused about the functions of the input and output functions, now to summarize the simple use of various functions.CIN has a buffer, the input buffer, that is built. The first input process is this, when the input data is stored in the input buffer at the end of the keyboard input, and the CIN function takes the data directly from the input buffer. Because the CIN functio
Q:sublime is configured in Ubuntu environment, can use GCC compiled to run the program, found that cin,scanf such a command does not executeThe A:sublime console does not support such an input operation, the solution is to call the Linux local terminal, execute the program on the terminal,The online example of Windows down CMD has been much better, but seldom found the configuration method in the Linux environment,The following is my own summary of th
{ "cmd": ["g++","${file}","- o","${file_path}/${file_base_name}"], "File_regex":"^(.. [^:]*]:([0-9]+):? ([0-9]+)?:? (.*)$", "Working_dir":"${file_path}", "selector":"source.c, source.c++", "variants": [ { "name":"Run", "cmd": ["Bash","- C","g++ ' ${file} '-O ' ${file_path}/${file_base_name} ' open-a terminal.app ' ${file_path}/${file_base_name} ' "] } ]}The terminal +xcode under the Mac is very close to the programming environment under L
C ++ most people regard cin: sync () as misuse of the cache function, cinsync
One hundred degrees, most people think of the cin: sync () function as a function to clear the buffer. However, if we use the VS2017 compiler, we will find that the buffer cannot be cleared. Why?
Http://en.cppreference.com/w/cpp/io/basic_istream/sync
Follow the instructions in this standard document:
This function is used:Sync
Given n strings, the n strings are arranged in dictionary order, where the permutation function is the C + + library function sort, resulting in the following two questions, look big guy answer#include #include#includestring>#includeusing namespacestd;/***********************************************q1: Why do you define an array of classes that cannot be sorted with the sort function? The array is out of bounds, the solution *********************************************int Main () {string str
Check InputCIN will check the input format and return FALSE if the input does not match the expected format.
cout
The above check can be placed in a try, catch statement.
try{while
(cin >> input)
sum + = input;
catch (Ios_base::failure f) {
cout
string Input: Getline (), get (), ignore ()Getline () reads the entire row, reads a specified number of characters, or stops reading when the specified character (the default line break
Turn from: https://www.cnblogs.com/luolizhi/p/5746775.html1, cin>>Usage 1: The most basic and most commonly used use, enter a number:
#include
Input: 2[carriage return]3[carriage return]Output: 5Note that:>> will filter out invisible characters (such as Space return, TAB, etc.)cin>>noskipws>>input[j];//do not want to skip whitespace characters, then use NOSKIPWS flow control
Usage 2: Accept a string, the
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.