cin 读输入
cin 等你输入两个数,在输出面板里直接输入。
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << a << " + " << b << " = " << a + b << endl;
return 0;
}免费,不用装 g++,不用登录。写完就跑,还能看清每个 vector、map、对象里装着什么。
容器里装了什么一目了然,对象的成员一个个列出来;`new` 在堆上放一块,指针就是一根箭头。
像在本地用 VS Code 一样顺手,还多了本地没有的:把每一步画出来。
按「单步」,代码停在每一行:亮的是马上要跑的那行,旁边是此刻每个变量的值。也能往回退,← → 键就能走。

报错下面有「看看怎么错的」:可视化停在出错的那一步,红色标出来,还用人话说清楚原因。前面每一步都能倒回去看。

一个链接打开就是这段代码;也能导出运行过程的动图(浅色 / 深色、横版 / 竖版)和代码图片,发到 X、小红书、群聊,或嵌进博客。

每个都能单步、能可视化,改一改再跑。
cin 等你输入两个数,在输出面板里直接输入。
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << a << " + " << b << " = " << a + b << endl;
return 0;
}push_back、范围 for、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;
}拼接、查找、取子串、反转。
#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;
}数每个单词出现了几次。
#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;
}按分数从高到低给学生排序。
#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;
}成员、构造函数、方法;在可视化里看 this。
#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;
}对象什么时候建、什么时候没。
#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;
}和 C 一样的交换,但不用写 *。
#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;
}后进先出,先进先出。
#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;
}看调用一层层叠起来,再一层层返回。
#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;
}经典动态规划,一行一行填表。
#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() 会抛异常;看是哪一步出的事。
#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;
}都不用。打开就能写 C++、能跑;代码存在你自己的浏览器里。
这是我们自己写、在浏览器里运行的 C++ 引擎,对 C++14 程序的输出和 g++ 13 一样。遇到未定义行为它会停下来告诉你在哪一行,这一点 g++ 不会。
能,和打比赛时一样。
运行到 cin 时,输出面板里会出现输入框,输完按回车,程序接着跑。
每层函数的变量、每个 vector / map / string 里装的东西、对象的成员,指针和引用都是箭头。
能。点一下代码就全屏写,有 Tab、花括号、分号、<< 这些专门的按键。
存在这个浏览器里,「我的代码」能找回来;重要的可以分享成链接存一份。
「分享」→ 复制链接,或者导出运行过程的动图、代码图片,一键发到 X。