Document Details

DivineZebra9695

Uploaded by DivineZebra9695

Red Rocks Community College

Tags

vectors computer science data structures programming

Summary

This document provides notes and activities on vectors, encompassing topics like pre-challenge vectors, vector data structures, static and dynamic arrays, memory management and object destructors, and operations including splitting strings. It demonstrates the Python way to describe elements and structure, potentially including visualisations and examples of C++ programming techniques for working with "vector-like" data structures.

Full Transcript

CSC 1061 VECTORS OBJECTIVES AGENDA: WEEK 09 Implement container classes 1. Pre-Challenge Vector so that the elements are 2. Vector Defined and stored in a dynamic array with Overview a capacity that is adjusted by...

CSC 1061 VECTORS OBJECTIVES AGENDA: WEEK 09 Implement container classes 1. Pre-Challenge Vector so that the elements are 2. Vector Defined and stored in a dynamic array with Overview a capacity that is adjusted by 3. Static and Dynamic Arrays the class's member functions as needed. 4. Data Structure: Vector 5. Record::split PRE-CHALLENGE VECTORS Complete the 1.10.2. Vectors tutorial Required: Activity: 1.10.2.1.1 Drag-N-Drop Optional: Activity: 1.10.2.1.2 Using a vector in C++ Optional: Activity: 1.10.2.1.4 With use of ``reserve`` Optional: Activity: 1.10.2.1.5 Vectors out of bounds Required: Activity: 1.10.2.1.7 Multiple Choice Required: Activity: 1.10.2.1.8 Multiple Choice VISUALIZE VECTORS USING PYTHON LIST Visualize the program that converts a randomly generated binary number to the decimal number equivalent for a single byte of memory. 0 1 2 3 4 5 6 7 Index 27 26 25 24 23 22 21 20 128 64 32 16 8 4 2 1 VECTOR DATA STRUCTURE DEFINED Vectors are linear sequence containers representing arrays that can change in size. Just like arrays vectors use contiguous storage locations for their elements vector elements can be accessed using offsets on regular pointers to its elements vectors are just as efficient as arrays But unlike arrays vector size can change dynamically STATIC ARRAY DYNAMIC ARRAY a. stack memory b. linear structure c. heap memory d. compiler managed memory e. same data types for all elements in collection f. memory is fixed size g. memory can grow h. developer managed memory i. fixed size memory j. element access [index] k. indexes: 0 based l. memory leaks can occur m. contiguous memory n. allocation of memory: new o. deallocation of memory: delete p. pointer VECTOR Vector Definition: 0 1 2 3 Vectors are A B C D sequence containers representing arrays 0 1 2 3 4 5 that can change in A B C D E F size. data 0 1 2 3 4 5 6 7 A B C D E F G In CSC 1061 it is NOT allowed to use the std::vector class! The objective is to learn the algorithms and coding involved to create a vector like structure from scratch! VECTOR – MEMORY (CPLUSPLUS) Internally, vectors use a dynamically allocated array to store their elements – pointer to heap memory! To grow-in-size, the array will need to reallocate Memory allocation is expensive task in terms of processing time: Generally, vectors should NOT reallocate each time an element is added to the container and will allocate extra storage for growth. Generally, vectors only grow memory NOT shrink memory The max of the vector will only equal the used of the vector when the container is completely full or empty. ~ DESTRUCTOR When the object goes out of scope - All dynamically allocated memory must be destroyed by deleting it In a class that uses dynamic memory, the destructor function takes on this role, performing the following algorithm: If the pointer is not nullptr then Destroy all dynamically allocated memory: delete [] ptr; Reset the pointer back to nullptr to indicate the Memory has been destroyed. RECORD::SPLIT(CHAR) Record is a std::string. Record objects will be able to be split based upon a char delimiter. The split member function will grow memory one-by-one to split the Record object into separate fields The Record::split member function will create an internal Vector inside the method to perform the split The Vector must be manually coded – NO std::vector allowed! Our Record::split will work similarly to Python's String split CLASS RECORD class Record : public std::string { Record* fields = nullptr; int used = 0; Record will add a new member int max = 0; function, split(char) public: //... Record will need to add private ~Record(); Record* split(char); data members to handle the }; dynamically allocated pointer to Record::~Record() { if(fields != nullptr) { array, delete [] fields; fields = nullptr; Record will need to implement } the destructor to ensure all } Record* Record::split(char delimiter) { memory is destroyed when the // TODO algorithm object goes out of scope. return fields; } GROWING DYNAMIC MEMORY VISUALIZATION Notice that the memory is being allocated one-by-one, just like you need to do inside of Record::split(char) While all the code for this visualization is occurring inside of main, when done in a class, which member functions handle the following? Creating the pointer Initializing the pointer to nullptr Allocating memory one-by-one and populating the memory Deallocating all memory and re-setting back to nullptr

Use Quizgecko on...
Browser
Browser