Kodewick
開始學 →

C++ 線上執行

免費,不用裝 g++,不用登入。寫完就跑,還能看清每個 vector、map、物件裡裝著什麼。

試試:
學 資料結構與演算法(C++ 版) →
C++ 線上執行全屏 · 程式碼和輸出都還在
C++
例子
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;
}
輸出

按「執行」看結果

不只是執行

看它怎麼跑:vector、物件、指標,一步一步畫出來

容器裡裝了什麼一目瞭然,物件的成員一個個列出來;`new` 在堆上放一塊,指標就是一根箭頭。

▶ 就在這裡播放,不用登入
除錯和分享

能跑,能停,能回放,能發出去

像在本地用 VS Code 一樣順手,還多了本地沒有的:把每一步畫出來。

單步

一行一行地跑,每個變數的值都擺在旁邊

按「單步」,程式碼停在每一行:亮的是馬上要跑的那行,旁邊是此刻每個變數的值。也能往回退,← → 鍵就能走。

一行一行地跑,每個變數的值都擺在旁邊
出錯回放

出錯了?直接看它是怎麼錯的

報錯下面有「看看怎麼錯的」:視覺化停在出錯的那一步,紅色標出來,還用人話說清楚原因。前面每一步都能倒回去看。

出錯了?直接看它是怎麼錯的
分享

連結、動圖、程式碼圖片,一鍵發到 X

一個連結開啟就是這段程式碼;也能匯出執行過程的動圖(淺色 / 深色、橫版 / 豎版)和程式碼圖片,發到 X、小紅書、群聊,或嵌進部落格。

連結、動圖、程式碼圖片,一鍵發到 X
例子

12 個 C++ 例子,點一下就在上面執行

每個都能單步、能視覺化,改一改再跑。

cin 讀輸入

cin 等你輸入兩個數,在輸出面板裡直接輸入。

#include <iostream>
using namespace std;

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

vector

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;
}

map 計數

數每個單詞出現了幾次。

#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 和 lambda

按分數從高到低給學生排序。

#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;
}

背包 DP

經典動態規劃,一行一行填表。

#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 越界

.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++

  1. 1
    寫程式碼,或選一個例子編輯器和 VS Code 一樣:自動補全、括號配對、Ctrl+/ 註解。
  2. 2
    點「執行」,或按 Ctrl + Enter結果在旁邊的輸出裡;程式要 cin 時直接在輸出裡回答。
  3. 3
    想看過程:點「單步」或「視覺化」程式碼自動存在這個瀏覽器裡,「我的程式碼」裡能找回來。
支援什麼

C++,在你的瀏覽器裡跑

✓ 支援

  • C++14:類、引用、lambda、auto、範圍 for
  • STL:vector、string、map、set、stack、queue、priority_queue、sort……
  • cin / cout,也能用 C 的 stdio
  • new / delete,洩漏會指出來
  • 編譯錯誤用 g++ 的原話

✕ 暫不支援

  • 執行緒、讀寫電腦上的檔案、聯網
  • 標準庫以外的庫(Boost 等)
  • 自己寫模板;最新的 C++ 特性(C++20 模組、協程等)
  • 停不下來的程式(30 秒後自動停)
常見問題

關於 C++ 線上執行

要錢嗎?要註冊嗎?

都不用。開啟就能寫 C++、能跑;程式碼存在你自己的瀏覽器裡。

這是真的 g++ 嗎?

這是我們自己寫、在瀏覽器裡執行的 C++ 引擎,對 C++14 程式的輸出和 g++ 13 一樣。遇到未定義行為它會停下來告訴你在哪一行,這一點 g++ 不會。

能用 #include <bits/stdc++.h> 嗎?

能,和打比賽時一樣。

程式用 cin 讀,在哪輸入?

執行到 cin 時,輸出面板裡會出現輸入框,輸完按回車,程式接著跑。

「視覺化」能看到什麼?

每層函式的變數、每個 vector / map / string 裡裝的東西、物件的成員,指標和引用都是箭頭。

手機上能用嗎?

能。點一下程式碼就全屏寫,有 Tab、花括號、分號、<< 這些專門的按鍵。

程式碼存在哪?

存在這個瀏覽器裡,「我的程式碼」能找回來;重要的可以分享成連結存一份。

怎麼發給別人?

「分享」→ 複製連結,或者匯出執行過程的動圖、程式碼圖片,一鍵發到 X。

還有

另外四種語言,同樣能跑、能看