To Int or to Uint, This is the Question
io www.linkedin.com/in/alexdathskovsky www.cppnext.com https://www.youtube.com/@cppnext-alexdTo INT Or To UINTAlex Dathskovsky | alex.dathskovsky@speedata.io | www.linkedin.com/in/alexdathskovsky linkedin.com/in/alexdathskovsky • what will be the result if we will call my_add(uint64_t(1), int64_t(-2)) ?SURPRISE AGAIN: MORE INTEGER PROMOTION Alex Dathskovsky | alex.dathskovsky@speedata.io dathskovsky@speedata.io | www.linkedin.com/in/alexdathskovsky • Use special types for better performance • int_fastN_t, uint_fastN_tWHAT TO DO? Alex Dathskovsky | alex.dathskovsky@speedata.io | www.linkedin.c0 码力 | 102 页 | 3.64 MB | 5 月前3Back to Basics: Templates Part 2
public: foobar(int n=0); ... void add(T const& t) { stuff.push_back(t); } }; void f1(); void f2(); #endif //- file_2.cpp #include "foobar.h" #includeusing std::string; void f2(int n) { foobar foobar fbs(n); ... } //- main.cpp #include "foobar.h" int main() { f1(); f2(); return 0; } //- file_1.cpp #include "foobar.h" #include #include using std::string; using using std::complex; using cxfloat = complex ; void f1(int n) { foobar fbs(n); foobar fbc(n*s); ... fbs.add("Hello, world"); fbc.add(cxfloat{1.0f, 2.0f}); ... }CppCon 2021 – 0 码力 | 80 页 | 490.15 KB | 5 月前3Building a Coroutine-Based Job System Without Standard Library
task; task<int> sum(int a, int b) { int result = a + b; co_return result; } task<int> sum4(int a, int b, int c, int d) { int ab = co_await sum(a, b); int cd = co_await sum(c, d); int result = co_await { __sum_frame(int a, int b); promise_type promise; int resumeIndex = 0; void(* const resumefn)(void*) = __sum_resume; void(* const destroyfn)(void*); struct param_t { int a; int b; } params; initial_suspend_awaitable; final_suspend_awaitable_t final_suspend_awaitable; }; }; task<int> sum(int a, int b) { auto frame = make_unique<__sum_frame>(a, b); decltype(auto) returnObject = frame->promise0 码力 | 120 页 | 2.20 MB | 5 月前3Object Lifetime: From Start to Finish
ends* 14 Storage DurationNon-vacous initialization 15int num; float pi; bool flag; struct EmptyPoint {}; EmptyPoint ep; struct Point { int x, y; }; Point p; struct ObjWithConstructor { ObjWithConstructor() Foo() { std::cout << "Foo()" << std::endl; } ~Foo() { std::cout << "~Foo()" << std::endl; } }; int main() { Foo a; { Foo b; } } 1 2 3 4 5 6 7 8 9 10 11 21 Constructors & Destructorshttps://godbolt Foo() { std::cout << "Foo()" << std::endl; } ~Foo() { std::cout << "~Foo()" << std::endl; } }; int main() { Foo a; { Foo b; } } 1 2 3 4 5 6 7 8 9 10 11 22 Constructors & Destructorshttps://godbolt0 码力 | 214 页 | 9.34 MB | 5 月前3Hello 算法 1.0.0 C语言版
? + 1, … , ? − 1 : // === File: iteration.c === /* for 循环 */ int forLoop(int n) { int res = 0; // 循环求和 1, 2, ..., n-1, n for (int i = 1; i <= n; i++) { res += i; } return res; } 第 2 章 复杂度分析 下面我们用 while 循环来实现求和 1 + 2 + ⋯ + ? : // === File: iteration.c === /* while 循环 */ int whileLoop(int n) { int res = 0; int i = 1; // 初始化条件变量 // 循环求和 1, 2, ..., n-1, n while (i <= n) { res += i; i++; 每轮进行两次更新,这种情况就不太方便用 for 循环实现: // === File: iteration.c === /* while 循环(两次更新) */ int whileLoopII(int n) { int res = 0; int i = 1; // 初始化条件变量 // 循环求和 1, 4, 10, ... while (i <= n) { res += i; // 更新条件变量0 码力 | 390 页 | 17.63 MB | 1 年前3Forwarding References
com/FWDREF CppCon 2023 | Forwarding References Quiz 6void f(int* ptr); void f(const int* ptr); int i = 42; const int ci = 42; int make_int() { return 42; } f(i); // ??? f(ci); // ??? f(make_int()); // ??? CppCon 2023 | Forwarding References Quiz: Pointers 7void f(int* ptr); void f(const int* ptr); int i = 42; const int ci = 42; int make_int() { return 42; } f(i); ERROR f(make_int()); // ERROR CppCon 2023 | Forwarding References Pointers In order to pass a variable to a function taking a pointer we need to pass its address. 8void f(int* ptr); int i = 42; const0 码力 | 107 页 | 3.72 MB | 5 月前3Hello 算法 1.2.0 简体中文 C语言 版
? + 1, … , ? − 1 : // === File: iteration.c === /* for 循环 */ int forLoop(int n) { int res = 0; // 循环求和 1, 2, ..., n-1, n for (int i = 1; i <= n; i++) { res += i; } return res; } 第 2 章 复杂度分析 下面我们用 while 循环来实现求和 1 + 2 + ⋯ + ? : // === File: iteration.c === /* while 循环 */ int whileLoop(int n) { int res = 0; int i = 1; // 初始化条件变量 // 循环求和 1, 2, ..., n-1, n while (i <= n) { res += i; i++; 每轮进行两次更新,这种情况就不太方便用 for 循环实现: // === File: iteration.c === /* while 循环(两次更新) */ int whileLoopII(int n) { int res = 0; int i = 1; // 初始化条件变量 // 循环求和 1, 4, 10, ... while (i <= n) { res += i; // 更新条件变量0 码力 | 392 页 | 18.52 MB | 9 月前3Hello 算法 1.1.0 C语言版
? + 1, … , ? − 1 : // === File: iteration.c === /* for 循环 */ int forLoop(int n) { int res = 0; // 循环求和 1, 2, ..., n-1, n for (int i = 1; i <= n; i++) { res += i; } return res; } 图 2‑1 是该求和函数的流程框图。 下面我们用 while 循环来实现求和 1 + 2 + ⋯ + ? : // === File: iteration.c === /* while 循环 */ int whileLoop(int n) { int res = 0; int i = 1; // 初始化条件变量 // 循环求和 1, 2, ..., n-1, n while (i <= n) { res += i; i++; 章 复杂度分析 hello‑algo.com 21 // === File: iteration.c === /* while 循环(两次更新) */ int whileLoopII(int n) { int res = 0; int i = 1; // 初始化条件变量 // 循环求和 1, 4, 10, ... while (i <= n) { res += i; // 更新条件变量0 码力 | 391 页 | 18.51 MB | 1 年前3Template Metaprogramming: Type Traits
int do_something_else(int, char const*); 10Metafunctions • Traditional functions have zero+ parameters and return a value (or void) void do_something(); int do_something_else(int, char char const*); • Mechanism for returning a value from a function is "return" int do_something_else(int, char const*) { return 42; } void) void do_something(); int do_something_else(int, char const*); • Mechanism for returning a value from a function is "return" int do_something_else(int, char const*) { return0 码力 | 403 页 | 5.30 MB | 5 月前3Hello 算法 1.0.0b4 C#版
10 + (1 + 5) × ? = 6? + 12 // 在某运行平台下 void algorithm(int n) { int a = 2; // 1 ns a = a + 1; // 1 ns a = a * 2; // 10 ns // 循环 n 次 for (int i = 0; i < n; i++) { // 1 ns ,每轮都要执行 i++ Console.WriteLine(0); algorithm_A(int n) { Console.WriteLine(0); } // 算法 B 时间复杂度:线性阶 void algorithm_B(int n) { for (int i = 0; i < n; i++) { Console.WriteLine(0); } } // 算法 C 时间复杂度:常数阶 void algorithm_C(int n) { for for (int i = 0; i < 1000000; i++) { Console.WriteLine(0); } } 2. 复杂度 hello‑algo.com 16 Figure 2‑1. 算法 A, B, C 的时间增长趋势 相较于直接统计算法运行时间,时间复杂度分析有哪些优势和局限性呢? 时间复杂度能够有效评估算法效率。例如,算法 B 的运行时间呈线性增长,在 ? > 1 时比算法0 码力 | 341 页 | 27.39 MB | 1 年前3
共 1000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 100