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;
}Free, no g++ to install, no sign-in. Run it, then watch what is inside every vector, map and object.
#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;
}
Press Run to see the result
Containers show what they hold; objects show their members; `new` puts a block on the heap and a pointer is an arrow to it.
As handy as VS Code on your computer, plus what it does not have: every step drawn.
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.

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.

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.

Each one can be stepped through and visualized. Change it, run it again.
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;
}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;
}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;
}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 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;
}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;
}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;
}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;
}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;
}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;
}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;
}.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;
}No and no. Open the page and write C++; your code stays in your own browser.
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.
Yes, as in competitive programming.
When it reaches cin, a box appears in the output: type, press Enter, it carries on.
Each function’s variables, what is in every vector, map and string, objects’ members, and arrows for pointers and references.
Yes. Tap the code to write full screen, with keys for Tab, braces, semicolons and <<.
In this browser, under My code. Share anything important as a link.
Share → Copy link, or export a GIF of it running, or a picture of the code, and post it to X.