C to C++ bridge
The C++ tools students coming from C need first: streams, vector, string, pair, sort, references, lambdas, and STL containers. · 14 min read
Concept
If you already know C, C++ is less a new universe than a sharper toolbox. You can still write loops, arrays, structs, and functions. The upgrade is that C++ gives you safer strings, dynamic arrays, standard containers, and algorithms that remove a lot of hand-written bookkeeping.
Input and output
iostream replaces most scanf/printf work. Add fast I/O at the top of main so streams are competitive with C-style I/O:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n);
for (int &x : a) cin >> x;
long long sum = 0;
for (int x : a) sum += x;
cout << sum << '\n';
}
What the fast I/O lines mean
ios::sync_with_stdio(false); disconnects C++ streams (cin, cout) from C streams (scanf, printf). By default they are synchronized so mixing C and C++ I/O behaves predictably, but that safety costs time. Turning synchronization off makes cin/cout much faster. The tradeoff: after doing this, avoid mixing scanf/printf with cin/cout in the same program unless you deeply understand the buffering.
cin.tie(nullptr); removes the automatic flush of cout before every cin. Normally, C++ assumes an interactive conversation: before reading input, it flushes pending output so the prompt appears. Contest programs usually read from a file-like stream, not a human typing after a prompt, so the automatic flush is wasted work. If you write an interactive problem later, flush manually with cout << flush when the judge must see your output immediately.
vector: the CP array
vector<T> is a dynamic array. It stores elements contiguously like a C array, supports O(1) indexing, and grows when you push_back. You can pass it to functions, sort it, and ask for v.size() without manually tracking capacity.
vector<int> v;
v.push_back(10);
v.push_back(20);
cout << v[0] << ' ' << v.size() << '\n';
vector<int> a(n, 0); // n zeroes
sort(a.begin(), a.end());
reverse(a.begin(), a.end());
string, pair, and tuple
string replaces manual C strings for most contest work. pair and tuple group small values without writing a struct every time.
string s;
cin >> s;
cout << s.size() << ' ' << s.substr(0, 3) << '\n';
pair<int, int> cell = {2, 5};
auto [r, c] = cell;
vector<pair<int, int>> edges;
edges.push_back({u, v});
Prefer dynamic library types
Coming from C, it is tempting to write int a[100000] or char s[1000] everywhere. In CP C++, prefer library types unless a problem has a very specific reason not to:
- Instead of static arrays, use `std::vector` in C++.
- In Java, the matching everyday tool is `ArrayList`.
- Instead of C strings (
char[],char*), use `std::string` in C++. - In Java, when you build a string through many appends, use `StringBuilder`.
References and range-for
A reference is an alias. Use const T& to avoid copying large values, and T& when you intentionally want to mutate the original.
void addOne(vector<int>& a) {
for (int &x : a) x++;
}
long long sumOf(const vector<int>& a) {
long long total = 0;
for (int x : a) total += x;
return total;
}
STL containers to learn early
vector: dynamic array, indexing, sorting, prefix sums.string: text as a first-class type.set/map: ordered keys,O(log n)operations.unordered_set/unordered_map: hash tables, averageO(1)operations.queue,stack,deque: BFS, bracket matching, sliding windows.priority_queue: always pull the current largest or smallest item.
Algorithms and lambdas
Many CP solutions become shorter and less buggy when you use standard algorithms. sort, lower_bound, upper_bound, min, max, accumulate, and count are worth learning early. For custom sorting, a lambda keeps the comparison beside the call:
vector<pair<int, int>> intervals;
sort(intervals.begin(), intervals.end(), [](auto a, auto b) {
if (a.first != b.first) return a.first < b.first;
return a.second > b.second;
});