C++
Online C++ Compiler
Code compiles with G++ 16 (GCC) as -std=gnu++17, and run settings switch to C++98, 11, 14, 20, 23 or the C++26 preview. cin and getline read what you type in the console while the program runs.
Try it aboveHow C++ runs here
- Versions
- C++98, 11, 14, 17 (default), 20, 23, 26 preview, G++ 16
- Build
g++ -O2 -std=gnu++17 -o a.out *.cpp- Run
./a.out- Limits per run
- 256 MB memory, 15 s of CPU time on 2 cores, 2 MB of output
- Time
- Up to 10 minutes a run (30 on a paid plan); waits 2 minutes for input (5 on a paid plan)
- Sandbox
- Each run gets its own Linux sandbox with no network access. Text files your program writes next to your code open as new tabs.
- Good to know
- Every .cpp file is compiled together; bits/stdc++.h is precompiled.
A program that reads input
This counts how often each word appears and lists the most frequent first:
#include <bits/stdc++.h> using namespace std; int main() { int n; cout << "How many words? "; cin >> n; map<string, int> freq; cout << "Enter " << n << " words: "; for (int i = 0; i < n; i++) { string w; cin >> w; freq[w]++; } vector<pair<string, int>> byCount(freq.begin(), freq.end()); sort(byCount.begin(), byCount.end(), [](const auto &a, const auto &b) { return a.second != b.second ? a.second > b.second : a.first < b.first; }); for (const auto &[word, count] : byCount) cout << word << ' ' << count << '\n'; }
$ ./a.out How many words? 7 Enter 7 words: to be or not to be ok be 2 to 2 not 1 ok 1 or 1
cin >> w reads one word at a time whether the words come on one line or several. The structured binding in the last loop, auto &[word, count], is C++17, which is why it compiles under the default standard.
cin, getline and fast input
cin >> age stops before the Enter that followed the number. A getline() right after it reads that empty rest of the line, so the name is lost:
cout << "Age: "; cin >> age; cout << "Full name: "; getline(cin, name);
$ ./a.out Age: 30 Full name: [] is 30
getline(cin >> ws, name) skips the leftover newline and any spaces first:
#include <iostream> #include <string> using namespace std; int main() { int age; string name; cout << "Age: "; cin >> age; cout << "Full name: "; getline(cin >> ws, name); cout << "[" << name << "] is " << age << '\n'; }
$ ./a.out Age: 30 Full name: Ada Lovelace [Ada Lovelace] is 30
- For judge-sized input, ios::sync_with_stdio(false); cin.tie(nullptr); makes cin and cout much faster. Untying cin also means cout isn’t flushed before each read, so a prompt appears only at the end:
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cout << "n = "; cin >> n; long long sum = 0; for (int i = 0, x; i < n; i++) { cin >> x; sum += x; } cout << sum << '\n'; }
$ ./a.out 3 4 5 6 n = 15
- That’s fine on a judge, which shows nothing until the end anyway. For an interactive program, leave cin tied, or flush with cout << flush before reading.
- endl writes a newline and flushes; '\n' only writes the newline. Over thousands of lines, '\n' is noticeably faster.
- The console can’t send end of input, so while (cin >> x) keeps waiting after the last value. Read a count first, or end the input with a word that isn’t a number.
Several .cpp files
The build compiles every .cpp file together, so a class or function can sit in its own source file with a header:
// geometry.hpp #pragma once struct Rect { double w, h; }; double area(const Rect &r); // geometry.cpp #include "geometry.hpp" double area(const Rect &r) { return r.w * r.h; } // main.cpp #include <iostream> #include "geometry.hpp" int main() { Rect r{3, 4.5}; std::cout << "Area: " << area(r) << '\n'; }
$ ./a.out Area: 13.5
- Run builds the same program whichever tab is open, since every .cpp file goes into it. Only one of them may define main().
- Templates must be defined in the header. A template function defined only in a .cpp file links with undefined reference errors when another file uses it.
- Declare a function in a header and forget to define it, and the linker says undefined reference to `area(int, int)' (with that function’s name and parameters).
What isn’t available
- Third-party libraries: Boost, fmt, Eigen, nlohmann/json and OpenCV aren’t installed. Use the standard library, or std::format from C++20 in place of fmt.
- GCC’s own extras are there: <bits/stdc++.h>, and the policy-based data structures in <ext/pb_ds/assoc_container.hpp> for an order-statistics tree.
- Windows and Visual Studio headers: windows.h, conio.h and stdafx.h stop with No such file or directory, and _getch(), scanf_s() and system("pause") don’t exist on Linux.
- Network access: socket code compiles, but connections fail because runs are offline.
Errors you’re likely to see
- “error: 'cout' was not declared in this scope; did you mean 'std::cout'?”: write std::cout, or add using namespace std; after the includes.
- “error: 'vector' was not declared in this scope”: the header is missing. Add #include <vector>, or include <bits/stdc++.h> to get the whole library.
- “error: no match for 'operator<<' (operand types are 'std::ostream' {aka 'std::basic_ostream<char>'} and 'std::vector<int>')”: cout can’t print a whole vector. Loop over it and print each element.
- “error: 'ranges' has not been declared” or “'format' was not declared in this scope”: C++20 code compiled as C++17. Select C++20 or later in run settings.
- “terminate called after throwing an instance of 'std::out_of_range'” followed by what(): vector::_M_range_check: __n (which is 3) >= this->size() (which is 3): v.at(3) on a vector of three. The run ends with Return Code 134.
- Return Code 139 with no message: a segmentation fault, typically v[i] past the end (which, unlike at(), isn’t checked) or a dangling pointer.
Each message starts with main.cpp:7:5:, a link to that line. After a failed run, Fix with AI explains the error and proposes a change you review before it applies.
C++ standards worth switching for
C++20 adds ranges, views and std::format. Under C++20 this sorts, filters and transforms without writing a loop:
#include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {5, 3, 8, 1, 9, 2}; ranges::sort(v); auto even_squares = v | views::filter([](int x) { return x % 2 == 0; }) | views::transform([](int x) { return x * x; }); for (int s : even_squares) cout << format("{:>4}", s); cout << format("\nmax {}, has 8: {}\n", ranges::max(v), ranges::binary_search(v, 8)); }
$ ./a.out 4 64 max 9, has 8: true
- C++17, the default, already has structured bindings, std::optional, std::variant, if constexpr and std::filesystem.
- C++23 adds std::print and std::println from <print>, which format like std::format and write straight to the console.
- Contest code usually starts with #include <bits/stdc++.h> and using namespace std;. The header is precompiled here, so including all of it doesn’t slow the build.
Questions
Is bits/stdc++.h supported?
Yes. It’s a GCC header, and it’s precompiled for each standard, so including it costs almost no compile time.
Which C++ version does it use?
C++17 (gnu++17) with G++ 16. Pick C++98, 11, 14, 20, 23 or the C++26 preview in run settings.
How do I read a whole line with spaces?
Use getline(cin >> ws, line). The >> ws part skips the newline that an earlier cin >> left behind.
Why does my prompt appear after I type the input?
cin.tie(nullptr) stops cout from flushing before each read. Remove it for interactive programs, or write cout << flush after the prompt.
Can I use Boost?
No. Only the standard library and GCC’s own extensions are installed.
How do I use several files?
Add .cpp and .hpp files with the + after the tabs. Every .cpp file is compiled into the same program, and headers are included with #include "name.hpp".