rossella lau lecture 2, dco20105, semester a,2005-6 dco 20105 data structures and algorithms ...

32
Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 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

Post on 21-Dec-2015

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-6

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

Page 2: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-6

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

Array

…0 1 2 3 4 …

Page 3: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-63 Main IndexMain Index ContentsContents

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.

arr[0 ] arr[1 ] arr[2 ]

0 1 2 n -1

. . . arr[n -1 ]

Page 4: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-6

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)

3 0 3 51 5 2 0 3 0 3 5

1 5 2 0 3 0 3 51 5 2 0 3 0 3 5 4 0

4 02 5In sert 2 5 a t

P o s it io n 2

V ecto r 4 0

1 5 4 0

2 0

E rase 2 0 a tP o s it io n 1

V ecto r

S h ift righ t S h ift le ft

Page 5: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-6

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

Page 6: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-6

// static arraysint arrayA[20];int arrayB[]={1,2,3,4};

Examples of C arraysAn 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 arrayint *array;……array = new int[size];……delete[] array;

Page 7: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-67 Main IndexMain Index ContentsContents

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.

Page 8: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-6

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

Page 9: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-6

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.

Page 10: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-6

class Vector{ private: int *array; size_t size; size_t capacity; public:……};

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)

Page 11: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-6

A typical vector class – Methods

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

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(); ……};

Page 12: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-6

However …

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 Vector{ int *array; size_t size; size_t capacity; ……};

class VectorStr{ string *array; size_t size; size_t capacity; ……};

class VectorStd{ Student *array; size_t size; size_t capacity; ……}; Absolutely terrible for maintenance

Page 13: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-6

Vector intArray;

Templates in C++

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

class Vector{ int *array; size_t size; size_t capacity; ……};

template <class T>

T

<int>Vector<string> strArray;Vector<Student> stdArray;

Page 14: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-6

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

Page 15: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-6

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);

Page 16: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-6

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

v [ 0 ] v [ 1 ] v [ 2 ] . . . v [ n-1 ] ro o m to gro w

0 1 2 n-1

Page 17: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-6

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

Page 18: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-6

Examples for storing data

Storing data into an empty vector

Storing data into a pre-defined vector:

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;

Page 19: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-6

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)

Page 20: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-6

Elements with delete flags

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

Page 21: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-6

Other vector operationsAccessors:

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).

……

Page 22: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-6

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; …… for (int i=0; ; i < studentID.size(); i++) cout << studentID[i] << “ “; ……

Page 23: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-6

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

…… vector<int> studentID; vector<int>::iterator it; …… for (it=studentID.begin(); i !=studentID.end(); it++) cout << *it << “ “; ……

Page 24: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-6

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

Page 25: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-6

Example of find()

The linear search we used to have: (w/o delete flag)

Use the generic algorithm find() with iterator;

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);}

Page 26: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-6

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

Page 27: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-6

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

13 13 25 25 25 28 33…… ……

lbIt ubItrange.first= =range.second

Page 28: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-6

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)

Page 29: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-6

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

Page 30: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-6

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

Page 31: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-6

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

Page 32: Rossella Lau Lecture 2, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure

Rossella Lau Lecture 2, DCO20105, Semester A,2005-6

Reference

Ford: 1.8, 2.4, 3.5, 4Lecture 12 of DCO10105STL 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 --