Presentation is loading. Please wait.

Presentation is loading. Please wait.

Similar presentations


Presentation on theme: ""— Presentation transcript:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31 void f() { int x{0}; // Construction int y{x};// Copy Construction y = x; // Copy Assignment }// Destruction

32 class MyClass { /*... */ }; void f() { MyClass x{}; // Construction MyClass y{x};// Copy Construction y = x; // Copy Assignment } // Destruction

33 class MyClass { public: MyClass() { puts("Constructing"); } ~MyClass() { puts("Destroying"); } MyClass(MyClass const&) { puts("Copying"); } void operator=(MyClass const&) { puts("Assigning"); } };

34 class MyClass { /*... */ }; void f() { MyClass x{}; // Constructing MyClass y{x}; // Copying y = x; // Assigning }// y.~MyClass() // Destroying // x.~MyClass() // Destroying

35 class MyClass { /*... */ }; void f() { MyClass x; MyClass* p{&x}; // p is a pointer to x // *p is another name for x }

36 void f() { MyClass x; MyClass y; // Different objects have different addresses MyClass* px{&x}; // px points to x MyClass* py{&y}; // py points to y assert(px != py); }

37 void f() { MyClass* p{nullptr}; }

38 void f() { MyClass x{}; MyClass* px1{&x}; MyClass* px2{px1}; assert(px1 == px2); px1 = nullptr; assert(px1 != px2); }

39 // For value types, x and y are distinct int x = 0; int y = x; y = 42; // For reference types, x and y refer to the same object List x = new List (); List y = x; x.Add(1);

40 // x and *p denote the same object int x{0}; int* p{&x}; // Changes the value of x via *p *p = 42;

41

42 void f() { MyClass x{}; // Construction MyClass y{x}; // Copy Construction y = x; // Copy Assignment }// y.~MyClass() // Destruction // x.~MyClass() // Destruction Lifetime of y Lifetime of x

43 MyClass x{}; // Global variable void f() { static MyClass y{}; // Function-local static variable }

44 void f() { MyClass* p{new MyClass{}}; delete p; } Lifetime of *p

45 MyClass* f() { MyClass* p{new MyClass{}}; return p; } void g() { MyClass* p{ f() }; delete p; }

46 void f() { int a = 0; // Copy initialization int b(0); // Direct initialization int c{0}; // Brace initialization int d{}; }

47 int global_int; // Zero-initialized void f() { int local_int; // Not initialized }

48 void f() { int local_int{}; MyClass local_MyClass{}; }

49

50

51

52 std::vector v{ 1, 2, 3, 4, 5, 6 }; for (size_t i{0}; i != v.size(); ++i) { std::cout << v[i] << std::endl; }

53 std::vector v{ 1, 2, 3, 4, 5, 6 }; for (std::vector ::iterator it = v.begin(); it != v.end(); ++it) { std::cout << *it << std::endl; }

54

55

56 std::vector v{ 1, 2, 3, 4, 5, 6 }; for (auto it = v.begin(); it != v.end(); ++it) // std::vector ::iterator { std::cout << *it << std::endl; }

57 std::set s{ 1, 2, 3, 4, 5, 6 }; for (auto it = s.begin(); it != s.end(); ++it) // std::vector ::iterator not anymore! { std::cout << *it << std::endl; }

58 std::map m { { 1, 2 }, { 2, 4 }, { 3, 6 }, { 4, 8 }, { 5, 10 }, { 6, 12 } }; for (auto it = m.begin(); it != m.end(); ++it) { std::cout first second << std::endl; }

59

60 for (auto value : container) { std::cout << value << std::endl; } Called “range for” in C++

61 std::vector v{ 1, 2, 3, 4, 5, 6 }; for (auto it = v.begin(); it != v.begin() + 3; ++it) { std::cout << *it << std::endl; }

62 std::vector v{ 1, 2, 3, 4, 5, 6 }; v.erase(v.begin() + 3); // Removes the 4 v.insert(v.begin() + 2, 10); // Inserts a 10 after the 2 int const x{*(v.begin() + 1)}; // Gets the 2 *(v.begin() + 1) = 9; // Changes the 2 to a 9 int const y{*(v.begin() + 1000)}; // Invalid!

63 What about operations like sort and find? v.sort(); // Nope; doesn't exist v.find(4); // Nope; doesn't exist

64

65 std::vector v{ 1, 3, 5, 2, 4, 6 }; std::sort(v.begin(), v.end());

66 std::vector v{ 1, 3, 5, 2, 4, 6 }; auto const it{std::find(v.begin(), v.end(), 2)}; if (it != v.end()) { std::cout << "I found a two!" << std::endl; }

67 std::vector v{ 1, 2, 3, 4, 5, 6 }; if (std::binary_search(v.begin(), v.end(), 2)) { std::cout << "I found a two!" << std::endl; }

68

69 std::vector v{ 1, 2, 3, 4, 5, 6 }; std::for_each(v.begin(), v.end(), [](int const x) { std::cout << x << std::endl; })

70 std::vector v{ 1, 2, 3, 4, 5, 6 }; std::transform(v.begin(), v.end(), v.begin(), [](int const x) { return x * x; });

71 std::vector v{ 1, 2, 3, 4, 5, 6 }; std::vector w(v.size()); std::transform(v.begin(), v.end(), w.begin(), [](int const x) { return x * x; });

72 std::vector v{ 1, 2, 3, 4, 5, 6 }; std::vector w; std::transform(v.begin(), v.end(), std::back_inserter(w), [](int const x) { return x * x; });

73 std::vector v{ 1, 3, 5, 2, 4, 6 }; std::sort(v.begin(), v.end(), [](int const lhs, int const rhs) { return lhs > rhs; });

74 std::vector v{ 1, 2, 3, 4, 5, 6 }; bool const all_are_even(std::all_of(v.begin(), v.end(), [](int const x) { return x % 2 == 0; }));

75 std::vector v{ 1, 2, 3, 4, 5, 6 }; std::set s(v.begin(), v.end());

76 int a[6]{ 1, 3, 5, 2, 4, 6 }; std::sort(begin(a), end(a));

77 void f(int* const data, size_t const count) { int* const first{data}; int* const last {data + count}; std::sort(first, last); }

78 template bool all_of(Iterator const first, Iterator const last, Function const f) { for (Iterator it{first}; it != last; ++it) { if (!f(*it)) return false; } return true; }

79

80 void f() { MyClass* p{new MyClass{}}; delete p; }

81 void f() { char* const buffer{new char[1024 * 1024]{}}; //...use the buffer... delete[] buffer; }

82 void f() { char* const buffer{new char[1024 * 1024]{}}; //... throw std::runtime_error{"oops“}; //... delete[] buffer; }

83 void f() { char* const buffer{new char[1024 * 1024]{}}; //... return; //... delete[] buffer; }

84 void f() { // Not actual C++ char* const buffer{new char[1024 * 1024]{}}; try { //...use the buffer... } finally { delete[] buffer; }

85 void f() { // Not actual C++ using (char* const buffer{new char[1024 * 1024]{}}) { //...use the buffer... }

86 void f() { scoped_buffer buffer{new char[1024 * 1024]{}}; //...use the buffer... }// buffer.~scoped_buffer()

87 struct scoped_buffer { public: scoped_buffer(char* const p) : _p{p} { } ~scoped_buffer() { delete[] _p; } char* get() const { return _p; } private: scoped_buffer(scoped_buffer const&) = delete; void operator=(scoped_buffer const&) = delete; char* _p; };

88 void f() { scoped_buffer buffer{new char[1024 * 1024]{}}; //...use the buffer... }// buffer.~scoped_buffer()

89 void f(size_t const buffer_size) { std::unique_ptr const buffer{new char[buffer_size]{}}; //...code that uses the buffer... }

90 std::unique_ptr f(size_t const buffer_size) { std::unique_ptr buffer{new char[buffer_size]{}}; return buffer; }

91

92 std::mutex _sync; void f() { _sync.lock(); //...synchronized code... _sync.unlock(); }

93 void f() { std::unique_lock lock(_sync); //...synchronized code... }

94 void f(char const* const file_name) { FILE* file(fopen(file_name, "r")); //...code that uses the file... fclose(file); }

95 void f(char const* const file_name) { std::ifstream file(file_name); //...code that uses the file... }

96

97

98 void f() { int x{0}; // x is an object int& rx{x}; // rx is a reference to x // it is another name for x rx = 42;// Change the memory shared by x and rx std::cout << x << std::endl;// Prints 42 }

99 void f(std::vector & v) { //...Use the vector... }

100 void f() { int const x{0}; x = 42; // Invalid; x is const }

101 void f() { int x(0); int const& rx(x); int const* px(&x); x = 42; // Ok, x is not const rx = 42; // Invalid; rx is const *px = 42; // Invalid; *px is const }

102 void f(std::vector const& v) { //...Use the vector but can't modify it... }

103

104

105 www.microsoft.com/learning http://developer.microsoft.com http://microsoft.com/technet http://channel9.msdn.com/Events/TechEd

106

107


Download ppt ""

Similar presentations


Ads by Google