Common Input/Output Methods for C++ in ACM Competition Mode

Common Input/Output Primitives

cin

cin is C++'s standrad input stream object. It automatically treats spaces, tabs, and newlines as input separators.

#include <iostream>
using namespace std;

int main() {
    int input_num;
    cin >> input_num;
    cout << input_num << endl;
    return 0;
}

getline()

When reading strings that contain spaces, cin will terminate early at the first whitespace. getline() solves this problem by reading an entire line of input, stopping only when it encounters a newline character.

#include <iostream>
#include <string>
using namespace std;

int main() {
    string input_str;
    getline(cin, input_str);
    cout << input_str << endl;
    return 0;
}

getchar()

getchar() reads a single character directly from the input buffer.

#include <iostream>
using namespace std;

int main() {
    char input_char;
    input_char = getchar();
    cout << input_char << endl;
    return 0;
}

Common Use Cases in Algorithm Problems

1D Array Input

Fixed-Length 1D Array

First read the number of elemnets, then read all space-separated elements. You can change the vector's template type to support other data types such as strings.

#include <iostream>
#include <vector>

using namespace std;

int main() {
    int len;
    cin >> len;
    vector<int> input_arr(len);
    for (int i = 0; i < len; ++i) {
        cin >> input_arr[i];
    }
    for (int val : input_arr) {
        cout << val << ' ';
    }
    return 0;
}

Variable-Length 1D Array

For cases where the array length is not given beforehand, stop reading when the end of the input line is reached. Adjust the vector template type to match your required input data.

#include <iostream>
#include <vector>

using namespace std;

int main() {
    int current_val;
    vector<int> result;
    while (cin >> current_val) {
        result.push_back(current_val);
        if (getchar() == '\n') break;
    }
    for (int val : result) {
        cout << val << ' ';
    }
    return 0;
}

Fixed-Size 2D Array

First input the number of rows and columns, then input all elements separated by spaces and newlines.

#include <iostream>
#include <vector>

using namespace std;

int main() {
    int rows, cols;
    cin >> rows >> cols;
    vector<vector<int>> grid(rows, vector<int>(cols));
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            cin >> grid[i][j];
        }
    }

    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            cout << grid[i][j] << ' ';
        }
    }
    return 0;
}

Input with Non-Whitespace Separators

For inputs like 1,2,3 that use separators other than whitespace, read the entire line first then split it into individual values.

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main() {
    string input_line;
    getline(cin, input_line);
    vector<int> output;
    int right = 0;
    int line_len = input_line.size();
    for (int left = 0; left < line_len; ++left) {
        right = left;
        while (right < line_len && input_line[right] != ',') {
            right++;
        }
        string num_str = input_line.substr(left, right - left);
        output.push_back(stoi(num_str));
        left = right;
    }
    for (int val : output) {
        cout << val << ' ';
    }
    return 0;
}

Linked List Input/Output Example

We use the reverse linked list problem to demonstrate common linked list I/O patterns:

Given the head of a singly linked list, reverse the list and return the new head.

Sample Input 1:

5
1 2 3 4 5

Sample Output 1:

5 4 3 2 1

Sample Input 2:

2
1 2

Sample Output 2:

2 1

Sample Input 3:

0

Sample Output 3:

(empty line)

Constraints:

  • Number of nodes ranges from [0, 5000]
  • -5000 <= Node.val <= 5000
#include <iostream>
using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(nullptr) {}
};

ListNode* reverseLinkedList(ListNode* head) {
    ListNode* prev = nullptr;
    ListNode* curr = head;
    while (curr != nullptr) {
        ListNode* temp = curr->next;
        curr->next = prev;
        prev = curr;
        curr = temp;
    }
    return prev;
}

void printList(ListNode* head) {
    ListNode* curr = head;
    while (curr != nullptr) {
        cout << curr->val << " ";
        curr = curr->next;
    }
    cout << endl;
}

int main() {
    int node_count, input_val;
    while (cin >> node_count) {
        ListNode* dummy = new ListNode(0);
        ListNode* curr = dummy;
        if (node_count == 0) {
            cout << endl;
            continue;
        }
        for (int i = 0; i < node_count; ++i) {
            cin >> input_val;
            curr->next = new ListNode(input_val);
            curr = curr->next;
        }
        ListNode* reversed_head = reverseLinkedList(dummy->next);
        printList(reversed_head);
    }

    return 0;
}

Tags: C++ ACM Programming Input Output Competitive Programming Algorithm Problems

Posted on Thu, 24 Sep 2026 16:39:10 +0000 by VLE79E