Back to home
C++ programming language logo

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

C++ tutorial: five steps from blank editor to working program

This walkthrough covers the practical C++ subset used in coursework and competitive programming: iostream, vectors and strings, references, structs and classes, and the STL algorithms.

  1. 1. iostream instead of printf

    cin and cout handle types automatically, so there are no format specifiers to get wrong. '\n' is cheaper than endl because endl also flushes.

    #include <iostream>
    using namespace std;
    
    int main() {
        int marks = 87;
        cout << "Marks: " << marks << "\n";
        return 0;
    }
  2. 2. Prefer vector and string over raw arrays

    vector grows on demand and knows its own size; string handles memory for you. Together they remove most memory bugs beginners hit in C.

    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main() {
        vector<int> v = {5, 3, 9};
        v.push_back(1);
        for (int x : v) cout << x << " ";
        cout << "\nsize: " << v.size() << "\n";
        return 0;
    }
  3. 3. Pass by reference to avoid copies

    A & parameter lets the function work on the caller's object. Add const when the function only reads it — that is the standard signature for large containers.

    #include <iostream>
    #include <vector>
    using namespace std;
    
    int sum(const vector<int> &v) {
        int t = 0;
        for (int x : v) t += x;
        return t;
    }
    
    int main() { cout << sum({1, 2, 3}) << "\n"; }
  4. 4. Group data with structs and classes

    struct and class differ only in default access. Constructors initialise members, and member functions keep the related logic beside the data.

    #include <iostream>
    using namespace std;
    
    struct Student {
        string name;
        int marks;
        bool passed() const { return marks >= 50; }
    };
    
    int main() {
        Student s{"Diya", 72};
        cout << s.name << " " << s.passed() << "\n";
    }
  5. 5. Let the STL do the work

    sort, find, count, accumulate and max_element cover most of what you would otherwise hand-write, and they are already optimised.

    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    int main() {
        vector<int> v = {4, 1, 9, 2};
        sort(v.begin(), v.end());
        cout << *max_element(v.begin(), v.end()) << "\n";
        for (int x : v) cout << x << " ";
    }

Practice exercises with solutions

Try each one in the editor above before opening the solution — the struggle is where the learning happens.

Beginner

Read two integers from input and print their sum, difference and product.

Hint: cin >> a >> b reads both in one statement.

Show solution
#include <iostream>
using namespace std;

int main() {
    int a, b;
    cin >> a >> b;
    cout << a + b << " " << a - b << " " << a * b << "\n";
}
Intermediate

Sort a vector of student names by marks, highest first.

Hint: Pass a lambda comparator to sort.

Show solution
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    vector<pair<string,int>> v = {{"Aarav",82},{"Diya",91}};
    sort(v.begin(), v.end(), [](auto &a, auto &b){ return a.second > b.second; });
    for (auto &p : v) cout << p.first << " " << p.second << "\n";
}
Advanced

Count the frequency of each word in a sentence using a map.

Hint: istringstream splits on whitespace for you.

Show solution
#include <iostream>
#include <sstream>
#include <map>
using namespace std;

int main() {
    string text = "code play code learn";
    istringstream in(text);
    map<string,int> freq;
    string w;
    while (in >> w) freq[w]++;
    for (auto &p : freq) cout << p.first << ": " << p.second << "\n";
}

Why learn C++?

C++ gives you C-level control plus containers and algorithms, which is why competitive programmers and game engineers pick it.

Learning it teaches both manual resource management and modern abstractions, so moving to Rust, Java or C# afterwards is straightforward.

What C++ is used for

  • Game engines and graphics (Unreal, Godot)
  • Competitive programming and Olympiad work
  • High-frequency trading and simulation software
  • Desktop applications and browser internals

Ready for structured practice? The 17-chapter C++ course takes these ideas one at a time, with a quiz and a certificate at the end.