I/O Summary
1. File Reading
Some graphs have a large amount of data and are suitable for file reading.
int main(){ //If there is an oj system (online decision), file reading is ignored, otherwise files are used as standard input. #ifdef ONLINE_JUDGE #else freopen("data.txt", "r", stdin); //Enter data from data.txt #endif .... return 0; }
Under the same folder as the. cpp file, create a txt text named data to store the input data.
2. About scanf() and printf()
2-1 parameter:
- Type conversion specifiers:% d,% lld,% f,% lf (nothing special, the format of input and output)
- The transformation specification modifier (modifier 1, added between% and d), such as:
Modifier | Effect |
---|---|
number | Specified width (see Example 2-1-1 if less than actual width is automatically updated upward) |
x.y | Limit precision (same number, x is integer partial digit, y decimal partial digit): Output is a little useful??? Is this to be verified? |
* | For scanf(): suppress assignment, for printf(): character width wildcard |
- For printf():
Modifier | Effect |
---|---|
+ | Displays a +/- sign: If it is positive, it shows a + sign. |
Blank space | Positive: Symbol bits show a space, negative: - sign |
0 | 0 Fill |
* | Specifies character width |
- For scanf():
Modifier | Effect |
---|---|
* | Input suppression, see Example 2-1-2 |
- Example 2-1-1:
int main(){ int in, other; scanf("%4d", &in); //Input: 123, 123456789 printf("%d\n", in); //Obtained: 123, 1234 scanf("%d", &other); //123: Continue to input, 123456789: other directly receives the rest (56789) printf("%d", other); //123456789: Direct Output Residual int out = 1234; printf("|%2d|\n", out); //Output: | 1234| printf("|%5d|", out); //Output: | 1234| return 0; }
- Example 2-1-2
int main(){ char s[5]; int in; scanf("%*d %d", &in); //Input: 123 12345 printf("|%*d|", 8, in); //Get: | 12345| scanf("%*s %d", &in); //Input: abc 12345 printf("|%*d|", 8, in); //Get: | 12345| return 0; }
2-2 return value:
- About scanf():
Returns the number of items successfully read in and returns 0 when an input mismatch occurs.
If the end of the file is read, then EOF is returned: for cases where the number of inputs is not fixed, such as Example 2-2-1
- Example 2-2-1:
while(scanf("%d", &in) != EOF){} //Read until the end while(~scanf("%d", &in)){} //Equivalent to the previous one: because EOF is -1 (11111111111) and 0 (00000000) after the inverse.
- About printf();
Returns the number of characters successfully printed on the screen:
int main(){ int x = 103; char s[10] = "abcde"; cout << printf("%d %s", x, s); //Output: 103 abcde9 - > 9:9 characters (space wrap, digit) return 0; }
3. Characters and strings:
3-1 characters:
- Read in single character: getchar(); output single character: putchar();
ch = getchar(); putchar(ch);
- Fill out the blanks and change lines:
int main(){ char ch; char valid[5]; //Output 1: balabala this_is_valid //Enter 2: | this_is_valid | The first place is a space. while((ch = getchar()) != ' ') continue; //Filter out blanks: Input before discarding the first blank (skipping the blank) can also be filtered \n... scanf("%s", valid); //Output 1: this_is_valid //Output 2: this_is_valid printf("%s", valid); return 0; }
TIPS 1: If you use scanf("% c", & c); have used scanf() before reading characters, you should pay attention to using getchar() or cin.ingore() to absorb the previous line break'\ n'(also a character).
3-2 Read in a line:
getline(cin, line);
Regarding the return value of geline(): getline (cin, stri) returns cin, CIN is converted to bool value true (read success) or false (read failure), see Example 4.-
4. Stream Input and Output: Control Format
4-1 sprintf() and sscanf():
TIPS 1: Can only be used for char*, string to use. c_str() into char arrays
- sscanf(): can achieve multiple segmentation at one time, such as:
/*Write the contents of character array STR in the format of "% d:%lf,%s" to int variable n, double variable db, char array variable str2.*/ int main(){ string line; int father, child; //Input: 12 is the father of 3 getline(cin, line); sscanf(line.c_str(), "%d is the father of %d", &father, &child); //Output: father:12 child:3 cout << "father:" << father << " child:" << child; return 0; }
- Format conversion (and format control), such as:
sscanf(str, "%lf", &tmp); //Write str to tmp in floating point form. If the format does not match, do not write sprintf(str, "%.2f", tmp); //Write tmp (floating point form) into str in. 2f format. If tmp does not match. 2f format, such as char tmp []: output 0.00, but instead of "% d", output 7405072 does not know what the principle is.
4-2 stringstream:
- Effect:
-
For example, read in a sentence: think of a word, a word processing (divided by spaces): 3 is my favorite number
-
Want to process a row of data in a unified way (e. g. 1234............................................ And, average and so on)
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { string line; //Input: 3 is my favorite number while(getline(cin,line)){ //Note 1: See 3-2 string x; stringstream ss(line); while(ss>>x) { //Take one word in each round: 3, is, my... cout << x; } } return 0; }
- About String: Details can be consulted: String Summary