sizeof array
在 C++ 中,對於陣列的大小處理,sizeof 在函式中有個容易踩雷的陷阱:如果你把陣列當作參數傳入函式,它會退化成指標。這會導致 sizeof 傳回的是指標的大小,而不是陣列的實際大小。 Example #include <iostream> void test1(char (&a)[10]) { std::cout << "test1 called with array of size: " << sizeof(a) << " bytes" << st...
Search for a command to run...
Articles tagged with #cpp
在 C++ 中,對於陣列的大小處理,sizeof 在函式中有個容易踩雷的陷阱:如果你把陣列當作參數傳入函式,它會退化成指標。這會導致 sizeof 傳回的是指標的大小,而不是陣列的實際大小。 Example #include <iostream> void test1(char (&a)[10]) { std::cout << "test1 called with array of size: " << sizeof(a) << " bytes" << st...
事情源於一個 error: #include <algorithm> #include <iostream> int main() { int64_t a = 2; std::cout << std::min(1L, a) << std::endl; // mac 上編不過 std::cout << std::min(1LL, a) << std::endl; // ubuntu 20.04 上編不過 return 0; } 無論是 1L 還是 1LL 都沒...
目標 std::priority_queue 會從最大值開始 pop,但假如我希望可以從最小值拿 ... ? 方法 反元素 假如臨時想不起來後面的做法也暫時沒有 Document 可以查(例如在打比賽??),然後元素又是可以取反元素的那種,那大概可以立刻想出這種臨時的方式: push 進去時先取反元素 pop 出來後再做反元素 說不上甚麼好處,但是就很容易想到。 std::greater<T> 首先, std::priority_queue 完整的 template 參數是: templ...
std::enable_shared_from_this
TL;DR 沒有 Side Effect 的無窮迴圈是未定義行為。 起源 Source: https://twitter.com/PR0GRAMMERHUM0R/status/1623366075357270019/photo/1 可以先猜猜這段 code 會有什麼輸出(x86_64 clang 15.0.0 -O1) #include <iostream> int main() { while (1); return 0; } void unreachable() { ...
2022 FB Hackercup Round2 比賽時只寫出 A1、 D1,A2是賽後補的。進不了 Round3 ,不過名次應該可以拿到 T-shirt。 感想:怎麼兩題都線段樹 🤣 Problem A: Perfectly Balanced Prob A2 題目 給定一個數列,要支援以下兩種操作: 單點修改:修改單個數字 區間查詢:確認該區間是否可以分成兩半,其中一半剛好和另一半只差一個數字。 Ex: [2, 2, 3] 可以被分成 [2] 和 [2, 3] [1, 3, 1, 1,...