320 likes | 460 Views
DCO 20105 Data structures and algorithms. Lecture 2: Vector Array and vector Internal structure of a vector How data is stored in a vector Process on a vector Application considerations -- By Rossella Lau. 0. 1. 2. 3. 4. …. …. Array.
E N D
DCO20105 Data structures and algorithms • Lecture 2: Vector • Array and vector • Internal structure of a vector • How data is stored in a vector • Process on a vector • Application considerations -- By Rossella Lau
0 1 2 3 4 … … Array A traditional container allowing storage of multiple occurrences of data • A truck of memory is assigned and data can be stored into its slots. • The number of slots is the size of the array and once that is defined, its size cannot be changed • An array uses an index to identify (access) its element • Data can be stored in any slot of an array but usually are stored from the first slot and new datum is appended at the end
3 Main Index Contents C++ Arrays (Ford’s slide: 4-12) An array is a fixed-size collection of values of the same data type. An array is a container that stores the n (size) elements in a contiguous block of memory.
Typical operations on an array • An array allows elements to be added/deleted at any arbitrary position • “Shift” operations are required when elements are inserted between elements and deleted at a position before the last element (Text book slides: 1:7)
Arrays in programming language • Usually supported by programming languages without using any library functions • An array can be static or dynamic • The size of a static array is determined at compilation time while the size of a dynamic array can be determined during execution
Examples of C arrays // static arrays int arrayA[20]; int arrayB[]={1,2,3,4}; • An array and a pointer are the same • A static array id is a constant pointer while dynamic array, the pointer, can point to any new value (array) • The storage occupied by a dynamic array should be released before the program is terminated. // dynamic array int *array; …… array = new int[size]; …… delete[] array;
7 Main Index Contents Evaluating an Array as a Container (Ford’s slide: 4-13) • The size of an array is fixed at the time of its declaration and cannot be changed during the runtime. • An array cannot report its size. A separate integer variable is required in order to keep track of its size. • C++ arrays do not allow the assignment of one array to another. • The copying of an array requires the generation of a loop structure with the array size as an upper bound.
Exception handling of array process • A programmer should take care to avoid overflow, when elements needed to be stored are more than the slots of an array • A programmer should take care all the “shift” operations are of an insert or a delete action
Vector • As O-O concept was matured and there were O-O languages, class Vector usually comes with a language’s library for use as an array • A vector encapsulates all array’s related housekeeping processes to save programmers’ some work in taking care of the overflow (while doing an insertion), shift operations, etc.
A typical vector class – Structure • A language supported array • capacity stores the number of slots in the vector (the array size) • size stores the number of slots used (usually the slots are occupied contiguously) class Vector{ private: int *array; size_t size; size_t capacity; public: …… };
A typical vector class – Methods class Vector{ private: …… public: int at(size_t i); void insert(size_t i, int item); void erase(size_t i); void resize(size_t); size_t size(); size_t capacity(); …… }; • at(i) – returns the item at slot i • insert(i,item) – inserts item before slot i and automatically resize the array if the array is “full” • erase(i) – removes the element at slot i and may shift elements from (i+i..n-1) to (i..n-2) • resize(i) – makes the vector to have an array with size i
However … class Vector{ int *array; size_t size; size_t capacity; …… }; • Consider the sample class vector • if the objects stored on the array are not integers, but strings, rational numbers, or student records, we may need to create many classes. class VectorStr{ string *array; size_t size; size_t capacity; …… }; class VectorStd{ Student *array; size_t size; size_t capacity; …… }; Absolutely terrible for maintenance
Templates in C++ class Vector{ int *array; size_t size; size_t capacity; …… }; template <class T> • In C++, type parameter supports a template class / function to be written as it can be of any data type • Rewrite class Vector to a template class • Whenever instantiating a Vector object, a type must be specified T Vector intArray; <int> Vector<string> strArray; Vector<Student> stdArray;
C++ Vector • Vector in C++ is a template class in which it supports • typical array operations: getting space for an array (instantiating a vector object), identifying an element, storing data, removing data; etc • housekeeping together with iterator operations in order to allow some generic algorithms to be applied on
Getting a vector • Syntax: Ford’s slide 4:15-16 • E.g.,// declare a vector with number of default slots vector<int> studentNumber; • Ford’s slide 4:22 // vector of size 5 containing the integer // value 0 vector<int> intVector(5); // vector of size 10; each element // contains the empty string vector<string> strVector(10);
Identifying elements • Syntax: Ford’s slide 4:18 • To identify elements in a vector is the same as the way for an array: Ford’s slide 14
Storing data to a vector • push_back() • provides a quick action to store an item after the occupied slot (the last datum) • can cause the vector to re-size if there is not enough room • insert() • places an item before a specified position • is inefficient but necessary if a particular order is required • []= (e.g., v[i]=23;) • places an item at a specified position • overwrites the value if the position is occupied • may cause error if the specified position is invalid
vector<int> numbers;for( i = 0; i < SIZE; i++ ) numbers.push_back(i); vector<int> numbers(SIZE);for( i = 0; i < SIZE; i++ ) numbers[i] = i; Examples for storing data • Storing data into an empty vector • Storing data into a pre-defined vector:
Remove an item • Remove an item: • pop_back() To remove the last item, shifting is not needed • erase() To remove an item through an iterator, but shifting is required • E.g., OrderItem.h in application bookShop v2.0 (without a delete flag) • Alternatively, each element can include a flag to indicate if an item is deleted to avoid shift operations • E.g., OrderItem.h in application bookShop (v 1.0)
It is efficient to remove an item in the middle – just a mark rather than shifts The trade-off is an additional space is required and subsequently, whenever a slot is visited, the slot must be checked The slot marked “delete” can be used again or let it be “removed” forever Re-use may cause more checking 0 1 2 3 4 … … T T Elements with delete flags
Other vector operations • Accessors: • back() to return the value of the last item • size() to return the number of contiguous used slots • capacity() to return the number of slots in the vector • Housekeeping: • resize() to re-allocate the capacity and size of the vector • Care should be taken when instantiating an object with the constructor Vector(SIZE). The system assumes SIZE of slots have been used. If it is not the case, remember to reset its size to 0 by using resize(0). • ……
…… vector<int> studentID; …… for (int i=0; ; i < studentID.size(); i++) cout << studentID[i] << “ “; …… Vector traversal with index • It is similar to traverse an array; e.g.: Traversal: to “visit” each element in a data structure; typical operations such as: to print all elements from a data structure, to find an item from a data structure
…… vector<int> studentID; vector<int>::iterator it; …… for (it=studentID.begin(); i !=studentID.end(); it++) cout << *it << “ “; …… Vector traversal with Iterator Remember that • Iterator is similar to a pointer referring to an element • each STL container supports an iterator to traverse the container itself • begin() returns the iterator which points to the first element • end() returns the iterator which refers to to pass-the-end, not the last element
Searching an item from a vector with find() find() is a generic algorithm which can be applied to every STL’s container Typical usage: typedef vector<int>::iterator It; // for simpler declaration It it = find(v.begin(), v.end(), x); // returns an iteratorif ( it != v.end() ) // found
int getItem(string publicationCode) const { int i=0; for (; i<items.size() &&((items[i].getPublication().getCode() != publicationCode); i++); return items.size() == 0 ? -1 : i < items.size() ? i : -1; } pair<bool, It> getItem(string const& publicationCode) { It result = find(items.begin(),items.end(), publicationCode); return result == items.end()? pair<bool, It>(false, result) : pair<bool, It>(true, result); } Example of find() • The linear search we used to have: (w/o delete flag) • Use the generic algorithm find() with iterator;
Better searching method find() uses linear searching and the efficiency is not good. When a container can provide random (direct) position access, such as an array or a vector, sort can be applied first and then binary search can take place • sort (v.begin(), v.end());binary_search(v.begin(), v.end(), target); // returns boolean
13 …… 13 25 25 25 28 33 …… range.first= lbIt ubIt =range.second Binary search methods • To return the iterator pointing to the position of the target being searched, the following can be used: • It lbIt = lower_bound(v.begin(), v.end(), target); • It ubIt = upper_bound(v.begin(), v.end(), target); • pair<It, It> range = equal_range(v.begin, v.end(), target); target=25
Operator Overload and generic algorithm • Note that generic algorithm used to require operator overload • In the last example, two operator overload operations are required: • orderItem == publicationCode (in OrderItem.h) • publication == publicationCode (in Publication.h)
Better? Not always! • Storing data to a vector • push_back() provides a quick action to store an item • insert() is inefficient but necessary if a particular order is required (save operations for a sort) • Finding elements from a vector • Sequential search is inefficient but an order is not required; i.e., push_back() can be used for storing data • Binary search is efficient but requiring a sorted order: i.e., insert() must be used for storing data
Application considerations Stable and long data stream required a lot of searches but few insert/erase operations are better to be sorted for binary search • Short data stream with few searches but many insert operations are better to use push_back() and sequential search
Summary • Vector is a sequential storage container which encapsulates an array and its housekeeping process as a class to simplify programmers’ work • It is the simplest storage structure to store data in contiguous slots with a trade off of that inefficient insert/erase operations • In C++, vector, in the STL, is a template class which allows data in a vector to be of any data type • STL’s vector supports a variety of functions for data storage • Together with generic algorithms, iterator, and operator overload, many popular vector traversal functions, such as search, are ready for use • Operations’ usage should depend on a particular application
Reference • Ford: 1.8, 2.4, 3.5, 4 • Lecture 12 of DCO10105 • STL online references • http://www.sgi.com/tech/stl/stl_introduction.html • http://www.sgi.com/tech/stl • http://www.cppreference.com/ • Example programs: OrderItem.h, Order.h, Catalog.h in the application BookShop v2.0 -- END --