The Surprising Costs of Voidvoid() member functions There’s nothing inherently evil about void functions void greeting() { cout << "I love my teacher" << endl; } int main() { greeting(); // Ok, innocuous initializations Consider int main() { int n = 0; if (cin >> n && is_odd(n)) cout << "How odd to see " << n << endl; } ## The case of the unnecessary initializations initializations Consider int main() { int n = 0; if(cin >> n && is_odd(n)) cout << "How odd to see " << n << endl; ## The case of the unnecessary initializations0 码力 | 189 页 | 60.10 MB | 1 年前3
Taming the C++ Filter Viewprint(const T& coll) { for (const auto& elem : coll) { std::cout << elem << ' "; } std::cout << '\n'; } std::vectorcoll1{0, 8, 15, 47, 11, 42 void print(const auto& coll) { for (const auto& elem : coll) { std::cout << elem << ' "; } std::cout << '\n'; } std::vector coll1{0, 8, 15, 47, 11, 42, 1}; auto& coll) { for (const auto& elem : coll) { std::cout << elem << ' "; } std::cout << '\n'; } std::vector col11{0, 8, 15, 47, 11, 42 0 码力 | 43 页 | 2.77 MB | 1 年前3
Back to Basics: Pointers5 int main() { int x = 7; int* px = &x; std::cout << "x is: " << x << std::endl; std::cout << *px is: " << *px << std::endl; return 5 int main() { int x = 7; int* px = &x; std::cout << "x is: " << x << std::endl; std::cout << *px is: " << *px << std::endl; return -o prog 3 #include 5 int main() { int x = 7; $$ int*px=\&x; $$ std::cout std::cout "x is: " << x << std::endl; "*px is: " << *px << std::endl; 0 码力 | 152 页 | 5.61 MB | 1 年前3
C++高性能并行编程与优化 - 课件 - 13 C++ STL 容器全解之 vector5, 7}; auto n = std::count_if(a.begin(), a.end(), std::bind2nd(std::less(), 4)); std::cout << n << std::endl; return 0; } 5, 7}; auto n = std::count_if(a.begin(), a.end(), std::bind2nd(std::less(), 4)); std::cout << n << std::endl; return 0; } 5, 7}; auto n = std::count_if(a.begin(), a.end(), std::bind2nd(std::less(), 4)); std::cout << n << std::endl; return 0; } 0 码力 | 90 页 | 4.93 MB | 2 年前3
Object Lifetime: From Start to FinishConstructors & Destructors 1 struct Foo { 2 Foo() { std::cout << "Foo()" << std::endl; } 3 ~Foo() { std::cout << "~Foo()" << std::endl; } 4 }; 5 6 int main() ## Constructors & Destructors struct Foo { Foo() { std::cout << "Foo() << std::endl; } ~Foo() { std::cout <<%~Foo() << std::endl; } }; int main() { Foo a; ## Constructors & Destructors struct Foo { Foo() { std::cout << "Foo() << std::endl; } ~Foo() { std::cout <<%~Foo() << std::endl; } }; int main() { Foo a; //0 码力 | 214 页 | 9.34 MB | 1 年前3
Modern C++ Tutorial: C++11/14/17/20 On the Fly#include "foo.h" #include#include int main() { [out = std::ref(std::cout << "Result from C code: " << add(1, 2))]() { out.get() << " probably still don’t understand the following small piece of code above, namely: [out = std::ref(std::cout << "Result from C code: " << add(1, 2))]() { out.get() << ".\n"; decltype(0)="">::value) std::cout << "NULL == 0" << std::endl; if (std::is_same ::value) std::cout << "NULL == (void *)0" 0 码力 | 92 页 | 1.79 MB | 2 年前3
Modern C++ Iterators= {10, 20, 30, 40}; // iterate over elements with index: for (int i = 0; i < 4; ++i) { std::cout << arr[i] << '\n'; // iterate over elements with pointer:  for (int* p = arr; p < arr + 4; ++p) { std::cout << *p << '\n'; int* p = arr + 4; // OK std::cout << *p; // Undefined Behavior C++ josuttis | eckstein 4 begin() til end() int arr[] = {10, 20, 30, 40}; for (int* p = arr; p < arr + 4; ++p) { std::cout << *p << '\n'; }  { cout << "A::Foo()" } void Bar() { Foo(); } }; struct B : A { int b}; string str2}; B() { Bar(); ... } virtual ~B() { Bar(); ... } virtual void Foo() { cout << c}; string str3}; C() { Bar(); ... } virtual ~C() { Bar(); ... } virtual void Foo() { cout << "C::Foo()" } }; ## C++ Under the Hood - v table ptr struct A { hidden v_table* string str1{}; A() { Bar(); ... } virtual ~A() { Bar(); ... } virtual void Foo() { cout << "A::Foo()" } void Bar() { Foo(); } }; struct B : A { int b{}; string0 码力 | 168 页 | 13.55 MB | 1 年前3
Back To Basics Lifetime ManagementGadget { public: int i; }; Gadget gadget; std::cout << gadget.i << "\n"; struct Gadget { int i; }; Gadget gadget; std::cout << gadget.i << "\n"; struct Gadget : i(0) {} }; Gadget gadget; std::cout << gadget.i << "\n"; struct Gadget { int i = 0; }; default member initializer Gadget gadget; std::cout << gadget.i << "\n"; "\n"; struct Gadget { int i; Gadget(int i) : i(i) {} }; Gadget gadget(42); std::cout << gadget.i << "\n"; struct Gadget { int i; Gadget(int i) : i(i) {} }; Gadget0 码力 | 66 页 | 8.43 MB | 1 年前3
现代C++ 教程:高速上手C++11/14/17/20#include "foo.h" #include#include int main() { [out = std::ref(std::cout << "Result from C code: " << add(1, 2))]() { out.get() << " 的在命令行中使用 clang++ -std=c++2a 也可以阅读本书。 如果你是首次接触现代 C++,那么你很可能还看不懂上面的那一小段代码,即: [out = std::ref(std::cout << "Result from C code: " << add(1, 2))]() { out.get() << ".\n"; decltype(0)="">::value) std::cout << "NULL == 0" << std::endl; if (std::is_same ::value) std::cout << "NULL == (void *)0" 0 码力 | 83 页 | 2.42 MB | 2 年前3
共 255 条
- 1
- 2
- 3
- 4
- 5
- 6
- 26
相关搜索词
void函数成员函数函数调用C++coutFilter Viewscall-by-valuecall-by-referenceLazy EvaluationPipelines指针内存地址动态数组函数指针智能指针vector容器STL内存分配插入操作对象生命周期初始化存储分配析构函数非空泛初始化Modern C++C++11/14/17/20nullptrconstexprstructured bindings迭代器容器范围算法类型v-table构造函数栈帧基指针复杂性历史原因Lifetime Management三五零规则C++11C++14C++17C++20现代C++结构化绑定Lambda表达式













