// Online C++ compiler to compile and run C++ code in the browser.
// Write C++ code in this online editor — no install needed.
#include <iostream>
using namespace std;
class Greeter {
public:
void greet() {
cout << "Hello, Tutorsbot!" << endl;
}
};
int main() {
Greeter g;
g.greet();
return 0;
}
Paste C++ code, press Run, and GCC compiles and runs it in about a second. The code is built as C++17, bits/stdc++.h is available for contest-style programs, and compile errors arrive with the line, column and a caret under the problem.
#include <bits/stdc++.h>
using namespace std;
int main() {
istringstream in("5\n4 1 7 3 9");
cin.rdbuf(in.rdbuf());
int n;
cin >> n;
vector<int> a(n);
for (auto& x : a) cin >> x;
sort(a.begin(), a.end());
cout << "sorted:";
for (int x : a) cout << ' ' << x;
cout << "\nmax: " << a.back() << '\n';
}
Code is compiled with GCC as C++17. In practice that gives you structured bindings (for (auto& [key, value] : m)), std::optional, std::variant, class template argument deduction, if constexpr and every data structure and algorithm in the STL.
C++20 features are not available. std::views and ranges, concepts and std::format fail to compile, so a filter over a vector is written with a loop or std::copy_if. If you are following a C++20 course, check the error message first: 'std::views' has not been declared means the code is fine and the standard is the problem.
Only the standard library is installed. Boost headers, conio.h, graphics.h and windows.h are missing, and the compiler stops with fatal error: ... No such file or directory.
Most contest solutions start with #include <bits/stdc++.h> and using namespace std;, and both work here, so you can paste a Codeforces or CodeChef solution as it is. Add ios::sync_with_stdio(false); cin.tie(nullptr); at the top of main if your template has it. It does no harm here.
Programs get no keyboard input, so test with the sample input instead. Wrap it in an istringstream and point cin at it, as the example above does:
istringstream in("5\n4 1 7 3 9");
cin.rdbuf(in.rdbuf());Everything after those two lines reads from cin as usual. Delete them before submitting to the judge. Without them, cin >> n fails silently and n keeps whatever value it had, which is why an uninitialised int n; can print a random number.
Many colleges still teach C++ with Turbo C++, and code from those courses needs a few changes before a modern compiler accepts it:
#include <iostream.h> becomes #include <iostream>, followed by using namespace std; or std:: before cout.void main() becomes int main().clrscr() and getch() from conio.h are removed. Output stays on screen anyway, so the program needs no pause at the end.graphics.h cannot run here, as there is no window. Keep those in Turbo C++ or a local setup with a graphics library.Code written for Dev C++ usually runs unchanged, because Dev C++ uses GCC too.
A compile error looks like file0.code.cpp:4:5: error: expected ',' or ';' before 'std'. The numbers are the line and column, and the source line is printed underneath with a caret. The error is often on the line above: in that example the missing semicolon belongs at the end of line 3. Every failed compile also ends with chmod: cannot access 'a.out'. That line only means no program was built, so fix the first error and ignore it.
Runtime problems look different. terminate called after throwing an instance of 'std::out_of_range' comes from .at() on a vector with a bad index. A plain v[10] on a 3-element vector does not throw; it reads memory it should not, so prefer .at() while you debug. A crash with no message at all is often a segmentation fault from a null pointer or very deep recursion.
O(n^2) loop over a million elements will not finish, which is a useful warning before you submit to a judge with a 1-second limit.istringstream fix above) and no network access.main.ofstream can be read back with ifstream during the same run, which is enough to practise file handling.Interviewers and contest problems come back to the same STL tools again and again. A useful set to run and modify here:
priority_queue<int, vector<int>, greater<int>> as a min-heap, for "k smallest" and Dijkstra problems.unordered_map for counting, and map when you need the keys in order. Print both and compare the output order.lower_bound and upper_bound on a sorted vector for binary search questions.vector<vector<int>> with BFS using a queue, and DFS with recursion.struct with an operator<, so sort and set can order your own type.Change the input in the istringstream, run again, and check the edge cases: an empty list, one element, and duplicate values.