Great — here's a complete textual cheat sheet covering all common and rare
competitive programming input formats in C++, with:
• Clear format descriptions
• Realistic example inputs
• Fully working C++ input code
• Separate sections (not a single template)
• Covers rare/advanced formats like EOF, comma-separated, strings
with spaces, etc.
1. Single Integer
Format:
A single integer x
Example Input:
42
C++ Code:
int x;
cin >> x;
2. Array of Integers
Format:
First line: n
Second line: n space-separated integers
Example Input:
5
10 20 30 40 50
C++ Code:
int n;
cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; ++i) cin >> arr[i];
3. Multiple Test Cases with Arrays
Format:
First line: T test cases
Each test case:
• Integer n
• n space-separated integers
Example Input:
2
3
123
4
5678
C++ Code:
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
}
4. Full Line with Spaces
Format:
A single line of text (can include spaces)
Example Input:
this is a sentence
C++ Code:
cin.ignore(); // if needed (e.g. after reading an int)
string s;
getline(cin, s);
5. Multiple Lines of Strings
Format:
First line: n
Next n lines: one string each
Example Input:
3
apple
banana
cherry pie
C++ Code:
int n;
cin >> n;
cin.ignore(); // to handle newline
vector<string> lines(n);
for (int i = 0; i < n; ++i)
getline(cin, lines[i]);
6. 2D Integer Matrix
Format:
First line: r c
Next r lines: c space-separated integers
Example Input:
23
123
456
C++ Code:
int r, c;
cin >> r >> c;
vector<vector<int>> mat(r, vector<int>(c));
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j)
cin >> mat[i][j];
7. 2D Character Grid
Format:
First line: r c
Next r lines: c space-separated characters
Example Input:
22
ab
cd
C++ Code:
int r, c;
cin >> r >> c;
vector<vector<char>> grid(r, vector<char>(c));
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j)
cin >> grid[i][j];
8. Graph Edges
Format:
First line: n m (nodes and edges)
Next m lines: u v (edges)
Example Input:
43
12
23
34
C++ Code:
int n, m;
cin >> n >> m;
vector<pair<int, int>> edges(m);
for (int i = 0; i < m; ++i)
cin >> edges[i].first >> edges[i].second;
9. Input Until EOF
Format:
Any number of space-separated integers until EOF
Example Input:
5 10 15 20
Ctrl+D (Linux/Mac) or Ctrl+Z (Windows)
C++ Code:
int x;
while (cin >> x) {
// process x
}
10. Comma-Separated Integers
Format:
One line of comma-separated integers
Example Input:
1,2,3,4,5
C++ Code:
string line;
getline(cin, line);
stringstream ss(line);
string temp;
vector<int> nums;
while (getline(ss, temp, ',')) {
nums.push_back(stoi(temp));
}
11. Space-Separated Characters
Format:
One line with space-separated characters
Example Input:
abcd
C++ Code:
vector<char> ch(4);
for (int i = 0; i < 4; ++i)
cin >> ch[i];
12. Unknown Number of Integers on One Line
Format:
One line with any number of space-separated integers
Example Input:
7 14 21 28 35
C++ Code:
string line;
getline(cin, line);
stringstream ss(line);
int x;
vector<int> nums;
while (ss >> x)
nums.push_back(x);
13. Mixed: Integer then Line
Format:
First line: integer
Second line: string (may contain spaces)
Example Input:
42
hello world!
C++ Code:
int x;
cin >> x;
cin.ignore();
string s;
getline(cin, s);
If you want this exported to a new PDF later, I can help with that too. Let me
know if you'd like examples for more exotic formats (like JSON-style input,
hex/binary, adjacency matrices, etc.) — but this covers virtually all competitive
programming input cases.
Want the output handling cheat sheet next?