Back to home
cpp programming language icon

C++ Online Compiler

Practice C++ right here — no installs, no signups.

C++ Compiler
Output
Code runs on the Play with Coding execution engine — your code is saved locally for next time.

About the C++ online compiler

C++ gives you C's control plus classes, templates and the Standard Template Library. It is the default language of competitive programming because STL containers and algorithms let you write correct solutions quickly. This online C++ compiler builds and runs your program with the standard library available.

Practise vectors, maps, sorting, iterators, classes with constructors and destructors, references versus pointers, and templates. Compiler errors in C++ are long — the guide below shows how to read the first line and ignore the noise.

  • Competitive programming practice with STL containers
  • Learning object-oriented C++ for university coursework
  • Testing sort/lower_bound/accumulate behaviour
  • Comparing pass-by-value against pass-by-reference

C++ sample programs you can run right now

Copy any snippet into the editor above and press Run. Each one is short on purpose — retype it from memory afterwards.

Vectors and sorting

#include <bits/stdc++.h>
using namespace std;

int main() {
  vector<int> v = {42, 7, 91, 13};
  sort(v.begin(), v.end());
  for (int x : v) cout << x << " ";
  cout << "\nMax: " << *max_element(v.begin(), v.end()) << "\n";
}

sort is O(n log n) and works on any random-access range; max_element returns an iterator, so dereference it.

A class with a constructor

#include <iostream>
using namespace std;

class Rect {
  int w, h;
public:
  Rect(int w, int h) : w(w), h(h) {}
  int area() const { return w * h; }
};

int main() { cout << Rect(4, 5).area() << "\n"; }

The : w(w), h(h) part is a member initialiser list — it initialises members before the constructor body runs.

Common C++ errors and how to fix them

error: 'cout' was not declared in this scope

Why: Missing include or namespace.

Fix: Add #include <iostream> and either using namespace std; or write std::cout.

undefined reference to `main'

Why: No main function was found.

Fix: Every program needs int main() { }.

no matching function for call to ...

Why: Argument types do not match any overload.

Fix: Read the first candidate line in the error — it shows the expected parameter types.

C++ syntax cheatsheet

ConceptSyntax
Include everything#include <bits/stdc++.h>
Vectorvector<int> v = {1, 2, 3};
Mapmap<string, int> m; m["a"] = 1;
Sortsort(v.begin(), v.end());
Range forfor (auto &x : v) { }
Fast IOios::sync_with_stdio(false);

C++ compiler FAQs

Can I use bits/stdc++.h?

Yes — the GCC-style catch-all header works, which is handy for competitive programming practice.

Is the STL fully available?

Yes: vector, map, set, queue, stack, algorithm and the rest of the standard library are all usable.

Keep going after the compiler

Running snippets builds speed; the 17-chapter course builds understanding. Work through the C++ chapters, take the quiz, then claim your certificate.

Other compilers: JavaScript compiler, TypeScript compiler, Python 3 compiler, Java compiler, C compiler, Go compiler, Rust compiler, HTML compiler, CSS compiler, SQLite compiler