tailieunhanh - Lecture Programming principles and practice using C++: Chapter 18 - Bjarne Stroustrup

After studying this chapter, students will be able to understand: Vector revisited, pointers and free store, destructors, initialization, copy and move, arrays, array and pointer problems, changing size, templates, range checking and exceptions. | Chapter 18 Vectors and Arrays Bjarne Stroustrup Abstract arrays, pointers, copy semantics, elements access, references Next lecture: parameterization of a type with a type (templates), and range checking (exceptions). Stroustrup/Programming Overview Vector revisited How are they implemented? Pointers and free store Destructors Initialization Copy and move Arrays Array and pointer problems Changing size Templates Range checking and exceptions Stroustrup/Programming Reminder Why look at the vector implementation? To see how the standard library vector really works To introduce basic concepts and language features Free store (heap) Copy and move Dynamically growing data structures To see how to directly deal with memory To see the techniques and concepts you need to understand C Including the dangerous ones To demonstrate class design techniques To see examples of “neat” code and good design Stroustrup/Programming vector // a . | Chapter 18 Vectors and Arrays Bjarne Stroustrup Abstract arrays, pointers, copy semantics, elements access, references Next lecture: parameterization of a type with a type (templates), and range checking (exceptions). Stroustrup/Programming Overview Vector revisited How are they implemented? Pointers and free store Destructors Initialization Copy and move Arrays Array and pointer problems Changing size Templates Range checking and exceptions Stroustrup/Programming Reminder Why look at the vector implementation? To see how the standard library vector really works To introduce basic concepts and language features Free store (heap) Copy and move Dynamically growing data structures To see how to directly deal with memory To see the techniques and concepts you need to understand C Including the dangerous ones To demonstrate class design techniques To see examples of “neat” code and good design Stroustrup/Programming vector // a very simplified vector of doubles (as far as we got in chapter 17): class vector { int sz; // the size double* elem; // pointer to elements public: vector(int s) :sz{s}, elem{new double[s]} { } // constructor // new allocates memory ~vector() { delete[ ] elem; } // destructor // delete[] deallocates memory double get(int n) { return elem[n]; } // access: read void set(int n, double v) { elem[n]=v; } // access: write int size() const { return sz; } // the number of elements }; Stroustrup/Programming Initialization: initializer lists We would like simple, general, and flexible initialization So we provide suitable constructors, including class vector { // public: vector(int s); // constructor (s is the element count) vector(std::initializer_list lst); // initializer-list constructor // }; vector v1(20); // 20 elements, each initialized to 0 vector v2 {1,2,3,4,5}; // 5 elements: 1,2,3,4,5 Stroustrup/Programming Initialization: initializer lists We would like simple, .

crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.