cin 讀輸入
cin 等你輸入兩個數,在輸出面板裡直接輸入。
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << a << " + " << b << " = " << a + b << endl;
return 0;
}容器裡裝了什麼一目瞭然,物件的成員一個個列出來;`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。