Kodewick
Start learning →

Online C++ Compiler

Free, no g++ to install, no sign-in. Run it, then watch what is inside every vector, map and object.

Try:
Online C++ CompilerFull screen · your code and output are still here
C++
Examples
vector.cpp
#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> v;
    for (int i = 1; i <= 5; i++)
        v.push_back(i * i);
    int sum = 0;
    for (int x : v) sum += x;
    cout << "size " << v.size() << ", sum " << sum << endl;
    return 0;
}
Output

Press Run to see the result

g++ 13 · C++14Ln 1, Col 1Spaces: 4Learn C++ →Change it to keep it in My code
Not just run

Watch your C++ run: vectors, objects and pointers, step by step

Containers show what they hold; objects show their members; `new` puts a block on the heap and a pointer is an arrow to it.

▶ Plays right here, no sign-in
Debug & share

Run it, pause it, replay it, share it

As handy as VS Code on your computer, plus what it does not have: every step drawn.

Step

One line at a time, every value beside it

Press Step and the code stops at each line: the lit line runs next, and every variable’s value sits beside it. Go back too, with the ← → keys.

One line at a time, every value beside it
Replay an error

It crashed? See exactly how it went wrong

Under the error, press “See how it went wrong”: the visualizer opens at the failing step, in red, with the reason in plain words. Step back through everything before it.

It crashed? See exactly how it went wrong
Share

A link, a GIF, a picture of the code: post it to X

A link opens this very code. Or export a GIF of it running (light or dark, wide or tall) or a picture of the code, and post it to X, a chat, or embed it in a blog.

A link, a GIF, a picture of the code: post it to X
Examples

12 C++ examples: one click runs them above

Each one can be stepped through and visualized. Change it, run it again.

Reading input with cin

cin waits for two numbers; type them in the output panel.

#include <iostream>
using namespace std;

int main() {
    int a, b;
    cin >> a >> b;
    cout << a << " + " << b << " = " << a + b << endl;
    return 0;
}

vector

push_back, a range-for loop, size().

#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> v;
    for (int i = 1; i <= 5; i++)
        v.push_back(i * i);
    int sum = 0;
    for (int x : v) sum += x;
    cout << "size " << v.size() << ", sum " << sum << endl;
    return 0;
}

Strings

Joining, finding, a substring, reversing.

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
    string s = "hello";
    s += " world";
    cout << s << " " << s.size() << endl;
    cout << s.find("world") << endl;
    cout << s.substr(0, 5) << endl;
    reverse(s.begin(), s.end());
    cout << s << endl;
    return 0;
}

Counting with a map

How many times each word appears.

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main() {
    string words[] = {"apple", "pear", "apple", "fig", "pear", "apple"};
    map<string, int> cnt;
    for (const string &w : words) cnt[w]++;
    for (auto &p : cnt)
        cout << p.first << " " << p.second << endl;
    return 0;
}

sort with a lambda

Sort students by score, highest first.

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

struct Stu {
    string name;
    int score;
};

int main() {
    vector<Stu> v = {{"Lin", 88}, {"Zhou", 95}, {"Chen", 79}};
    sort(v.begin(), v.end(), [](const Stu &a, const Stu &b) {
        return a.score > b.score;
    });
    for (auto &s : v) cout << s.name << " " << s.score << endl;
    return 0;
}

A class with a constructor

Members, a constructor, a method; see this in the visualizer.

#include <iostream>
using namespace std;

class Counter {
    int n;
public:
    Counter(int start) : n(start) {}
    void add() { n++; }
    int get() const { return n; }
};

int main() {
    Counter c(10);
    c.add();
    c.add();
    cout << c.get() << endl;
    return 0;
}

Constructors and destructors

When objects are made and when they go away.

#include <iostream>
using namespace std;

struct Box {
    int id;
    Box(int i) : id(i) { cout << "make " << id << endl; }
    ~Box() { cout << "drop " << id << endl; }
};

int main() {
    Box a(1);
    {
        Box b(2);
    }
    Box *c = new Box(3);
    delete c;
    return 0;
}

Swap with references

The same swap as in C, without the *.

#include <iostream>
using namespace std;

void swapInts(int &a, int &b) {
    int t = a;
    a = b;
    b = t;
}

int main() {
    int x = 3, y = 5;
    swapInts(x, y);
    cout << x << " " << y << endl;
    return 0;
}

stack and queue

Last in first out, first in first out.

#include <iostream>
#include <stack>
#include <queue>
using namespace std;

int main() {
    stack<int> s;
    queue<int> q;
    for (int i = 1; i <= 3; i++) {
        s.push(i);
        q.push(i);
    }
    cout << "stack top " << s.top() << ", queue front " << q.front() << endl;
    return 0;
}

Recursion: Fibonacci

Watch the calls stack up and come back.

#include <iostream>
using namespace std;

int fib(int n) {
    if (n <= 2) return 1;
    return fib(n - 1) + fib(n - 2);
}

int main() {
    cout << fib(6) << endl;
    return 0;
}

Knapsack DP

The classic dynamic programming table, one row at a time.

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

int main() {
    int w[] = {1, 3, 4}, v[] = {15, 20, 30};
    int cap = 4;
    vector<int> dp(cap + 1, 0);
    for (int i = 0; i < 3; i++)
        for (int c = cap; c >= w[i]; c--)
            dp[c] = max(dp[c], dp[c - w[i]] + v[i]);
    cout << dp[cap] << endl;
    return 0;
}

vector out of bounds

.at() throws; see the step where it happens.

#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> v = {1, 2, 3};
    for (int i = 0; i <= 3; i++)
        cout << v.at(i) << endl;
    return 0;
}
Three steps

How to run C++ online

  1. 1
    Write code, or pick an exampleThe editor works like VS Code: completion, matching brackets, Ctrl+/ to comment.
  2. 2
    Press Run, or Ctrl + EnterThe output shows beside the code; when the program calls cin, answer it right there.
  3. 3
    To see how it runs: Step, or VisualizeYour code is kept in this browser; find it again in My code.
What works

C++, running in your browser

✓ Works

  • C++14: classes, references, lambdas, auto, range-for
  • The STL: vector, string, map, set, stack, queue, priority_queue, sort …
  • cin / cout, and C’s stdio too
  • new / delete, with leaks shown
  • Compile errors in g++’s words

✕ Not yet

  • Threads, files on your computer, the network
  • Libraries outside the standard library (Boost …)
  • Writing your own templates; the newest C++ (C++20 modules, coroutines …)
  • Programs that never end (stopped after 30 s)
FAQ

About the online C++ compiler

Is it free? Do I need an account?

No and no. Open the page and write C++; your code stays in your own browser.

Is this real g++?

It is our own C++ engine running in your browser, made to give the same output as g++ 13 for C++14 programs. It stops at undefined behaviour and tells you where, which g++ does not.

Can I use #include <bits/stdc++.h>?

Yes, as in competitive programming.

My program reads with cin. Where do I type?

When it reaches cin, a box appears in the output: type, press Enter, it carries on.

What does the visualizer show?

Each function’s variables, what is in every vector, map and string, objects’ members, and arrows for pointers and references.

Does it work on a phone?

Yes. Tap the code to write full screen, with keys for Tab, braces, semicolons and <<.

Where is my code kept?

In this browser, under My code. Share anything important as a link.

How do I share it?

Share → Copy link, or export a GIF of it running, or a picture of the code, and post it to X.

More

Four more languages, the same tools