Online C++ compiler

Write C++ with classes, templates and the STL, give it input and run it. Errors come with a plain explanation, and any run can be stepped through in the visualizer.

Opening the compiler…

About the C++ compiler

How to use the C++ compiler

Write your program in the editor or start from a template: cin and cout, a class, inheritance with virtual functions, overloading, vector and sort, string reversal, a matrix, stack and queue, a linked list, and fstream. Press Run or Ctrl+Enter. You see the output, the time it took and main's exit code; text sent to cerr is shown in red.

The Tests tab keeps pairs of input and expected output. Run all tests runs each one with the same limits, marks it passed or failed, and shows a line-by-line difference when the output is not what you expected. Tests are saved with your code.

How input works

The Input tab is the program's standard input. cin >> reads one value at a time, skipping spaces and new lines; getline(cin, line) reads a whole line. After reading a number with cin >>, call cin.ignore() before getline, or getline reads the rest of the number's line. while (cin >> x) reads until the input ends.

What runs your code

C++ runs in your browser on StepVioz's C++ interpreter. It covers what courses and labs use: classes, constructors and destructors, inheritance, virtual functions, operator overloading, templates, exceptions, references, new and delete, and the STL: string, vector, map, set, unordered_map, unordered_set, multiset, deque, stack, queue, priority_queue (also with greater<int>), pair, sort, sstream, fstream and iomanip. Some newer features are not supported yet, such as lambdas and member templates; the error says what to write instead.

Each run may take 5 seconds; then it is stopped. Output is kept up to 10,000 lines or 1 MB, and the rest is cut with a notice. Programs cannot reach the network, and no packages or libraries can be installed: what a language brings with it is what you can use. Memory mistakes (an index past a vector's end, a NULL pointer, a dangling reference) stop the program with an explanation instead of crashing. unordered_map and unordered_set keep their items in sorted order, so a program that prints them may list them in a different order than g++.

See it step by step

Visualize this run opens the code and its input in the C++ visualizer, which shows objects, the call stack and the heap one line at a time, including which virtual function a call reaches. It follows the first 1,000 steps.

Questions

Is this g++?
No, it is StepVioz's C++ interpreter, running in your browser. For the C++ it supports, correct programs print what g++ prints.
Does #include <bits/stdc++.h> work?
Yes, it includes every header the compiler has.
Can I read and write files with fstream?
Yes. ofstream, ifstream and fstream use files kept in memory for the length of one run.
Why is a big loop slow?
An interpreter runs a few million simple steps per second. Course programs finish quickly; contest programs with very large inputs may hit the 5-second limit.
How do I sort with my own order without a lambda?
Write a small function such as bool byScore(const P& a, const P& b) and pass its name to sort, or use greater<int>() for descending order.

See also