slide1 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Main Index PowerPoint Presentation
Download Presentation
Main Index

Loading in 2 Seconds...

play fullscreen
1 / 42

Main Index - PowerPoint PPT Presentation


  • 158 Views
  • Uploaded on

Chapter 4 – The Vector Container. 1. Main Index. Contents. Container Types Sequence Containers Associative Containers Adapter Classes The List Container Stack Containers Queue Containers Priority Queue Containers Set Containers Map Containers C++ Arrays - Evaluating an Array as a

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Main Index' - cicada


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
slide1

Chapter 4

– The Vector Container

1

Main Index

Contents

  • Container Types
  • Sequence Containers
  • Associative Containers
  • Adapter Classes
  • The List Container
  • Stack Containers
  • Queue Containers
  • Priority Queue Containers
  • Set Containers
  • Map Containers
  • C++ Arrays
  • -Evaluating an Array as a
  • Container
  • The Generic Class
  • Vectors
  • -Vector Class Constructor –API
  • (2 slides)
  • -Vector Class Operations –API
  • (4 slides)
  • -Output with Vectors
  • -Declaring Vector Objects
  • -Adding and Removing Vector
  • Elements
  • -Resizing a Vector
  • The Insertion Sort
  • -Insertion Sort Algorithm
  • (3 slides)
  • Review - Template Class (2 slides)
  • Store Class(2 slides)
  • Summary Slides (6 slides)
container types

2

Main Index

Contents

Container Types
sequence containers

3

Main Index

Contents

Sequence Containers

A sequence container stores data by position in linear order 1st element, 2nd element, and so forth.

associative containers

4

Main Index

Contents

Associative Containers
  • Associative containers store elements by key.
    • Ex: name, social security number, or part number.
  • A program accesses an element in an associative container by its key, which may bear no relationship to the location of the element in the container.
adapter classes
Adapter Classes
  • An adapter contains a sequence container as its underlying storage structure.
  • The programmer interface for an adapter provides only a restricted set of operations from the underlying storage structure.
stack containers

7

Main Index

Contents

Stack Containers

A stack allows access at only one end of the sequence, called the top.

queue containers

8

Main Index

Contents

Queue Containers

A queue is a container that allows access only at the front and rear of the sequence.

priority queue containers

9

Main Index

Contents

Priority Queue Containers

A priority queue is a storage structure that has restricted access operations similar to a stack or queue.

Elements can enter the priority queue in any order. Once in the container, a delete operation removes the largest (or smallest) value.

set containers

10

Main Index

Contents

Set Containers

A set is a collection of unique values, called keys or set members.

map containers

11

Main Index

Contents

Map Containers

A map is a storage structure that implements a key-value relationship.

c arrays

12

Main Index

Contents

C++ Arrays

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.

evaluating an array as a container

13

Main Index

Contents

Evaluating an Array as a Container
  • 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.
vectors

14

Main Index

Contents

Vectors
generic classes structure
Generic Classes Structure

template <typename T>

class templateClass

{

public:

templateClass(const T& item); // constructor

T f() const;

// member function that return value of type T

void g(const T& item);

// member function that has an argument of type T

...

private:

// data stored by the object

T data // member function that return value of type T alue;

...

};

the store template class
The store Template Class

template <typename T>

class store

{

public:

store(const T& item = T()); // constructor

// access and update functions

T getValue() const; // return value

void setValue( const T& item); // update value

// overloaded operator << as a friend

friend ostream& operator<< (ostream& ostr, const store<T>& obj)

{//display output

ostr << "Value = " << obj.value;

return ostr;

}

private:

T value; // data stored by the object

};

slide17

CLASS vector

<vector>

Constructors

vector();

Create an empty vector. This is the default constructor.

vector(int n, const T& value = T());

Create a vector with n elements, each having a specified value. If the value argument is omitted, the elements are filled with the default value for type T. Type T must have a default constructor, and the default value of type T is specified by the notation T().

17

Main Index

Contents

slide18

CLASS vector

<vector>

Constructors

vector(T *first, T *last);

Initialize the vector using the address range [first, last). The notation *first and *last is an example of pointer notation that we cover in Chapter 5.

18

Main Index

Contents

slide19

CLASS vector

<vector>

Operations

T& back();

Return the value of the item at the rear of the vector.

Precondition: The vector must contain at least one element.

const T& back() const;

Constant version of back().

bool empty() const;

Return true if the vector is empty and false otherwise.

19

Main Index

Contents

slide20

CLASS vector

<vector>

Operations

T& operator[] (int i);

Allow the vector element at index i to be retrieved or modified.

Precondition: The index, i, must be in the range 0  i < n, where n is the number of elements in the vector.

Postcondition: If the operator appears on the left side of an assignment statement, the expression on the right side modifies the element referenced by the index.

const T& operator[] (int i) const;

Constant version of the index operator.

20

Main Index

Contents

slide21

CLASS vector

<vector>

Operations

void push_back(const T& value);

Add a value at the rear of the vector.

Postcondition: The vector has a new element at the rear and its size increases by 1.

void pop_back();

Remove the item at the rear of the vector.

Precondition: The vector is not empty.

Postcondition: The vector has a new element at the rear or is empty.

21

Main Index

Contents

slide22

CLASS vector

<vector>

Operations

void resize((int n, const T& fill = T());

Modify the size of the vector. If the size is increased, the value fill is added to the elements on the tail of the vector. If the size is decreased, the original values at the front are retained.

Postcondition: The vector has size n.

int size() const;

Return the number of elements in the vector.

22

Main Index

Contents

output with vectors
Output with Vectors

// number of elements in list is v.size()

template <typename T>

void writeVector(const vector<T>& v)

{

// capture the size of the vector in n

int i, n = v.size();

for(i = 0; i < n; i++)

cout << v[i] << " ";

cout << endl;

}

declaring vector objects
Declaring Vector Objects

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

declaring a vector
Declaring a Vector
  • int arr[5] = {7, 4, 9, 3, 1};
  • vector<int> v(arr,arr+5); // v initially has 5 integers
    • //Other option: good – when arr changes, vector declaration does not change
    • int arr[ ] = {7, 4, 9, 3, 1, 3, 2};
    • int arrsize = sizeof(arr)/sizeof(int)
    • /*sizeof returns the number of bytes of memory required for the type*/
    • vector<int> v(arr,arr+5);
resizing a vector
Resizing a Vector

v.resize(10);// list size is doubled

v.resize(4); // list is contracted. data // is lost

slide28

28

Main Index

Contents

the insertion sort

29

Main Index

Contents

The Insertion Sort
insertion sort algorithm

30

Main Index

Contents

Insertion Sort Algorithm

insertionSort():

// sort a vector of type T using insertion // sort

template <typename T>

void insertionSort(vector<T>& v)

{

int i, j, n = v.size();

T temp;

// place v[i] into the sublist v[0] ... // v[i-1], 1 <= i < n, so it is in the // correct position

insertion sort algorithm1

31

Main Index

Contents

Insertion Sort Algorithm

for (i = 1; i < n; i++)

{ // index j scans down list from v[i] // looking for correct position to // locate target. assigns it to v[j]

j = i;

temp = v[i];

// locate insertion point by scanning // downward as long as temp < v[j-1] // and we have not encountered the // beginning of the list

insertion sort algorithm2

32

Main Index

Contents

Insertion Sort Algorithm

while (j > 0 && temp < v[j-1])

{

// shift elements up list to make // room for insertion

v[j] = v[j-1];

j--;

}

// the location is found; insert temp

v[j] = temp;

}

}

review template class

33

Main Index

Contents

Review - Template Class

<template typename T>

class templateClass

{

public:

// constructor with argument of // constant reference type T

templateClass (const T& item);

// member function returns a value of // type T

T f();

review template class1

34

Main Index

Contents

Review - Template Class

// member function has an argument of // type T

void g(const T& item);

...

private:

T dataValue;

...

};

slide35

template <typename T>

class store

{

public:

store(const T& item = T());

// initialize value with item or the default // object of type T

getValue() const;

// retrieve and return data member value

CLASS store

Declaration

“d_store.h”

35

Main Index

Contents

slide36

void setValue(const T& item);

// update the data member value to item

friend ostream& operator<< (ostream& ostr, const store<T>& obj);

// display output in the from "Value = " // value

private:

T value; // data stored by the object

};

CLASS store

Declaration

“d_store.h”

36

Main Index

Contents

summary slide 1

37

Main Index

Contents

Summary Slide 1
  • §- The Standard Template Library (STL) provides 10 container classes for solving a wide range of problems.
  • §- Containers fall into one of three classifications:
    • sequence containers
    • adapters
    • associative containers.
summary slide 2

38

Main Index

Contents

Summary Slide 2

§- The array data structure is a sequence container.

- It defines a block of consecutive data values of the same type.

- Arrays are direct access containers. §- An index may be used to select any item in the list without referencing any of the other items

summary slide 3

39

Main Index

Contents

Summary Slide 3

§- The vector sequence container provides direct access through an index and grows dynamically at the rear as needed.

- Insertion and deletion at the rear of the sequence is very efficient §-these operations inside a vector are not efficient.

summary slide 4

40

Main Index

Contents

Summary Slide 4

§- The list sequence container stores elements by position.

- List containers do not permit direct access §-must start at the first position (front) and move from element to element until you locate the data value.

- The power of a list container is its ability to efficiently add and remove items at any position in the sequence.

summary slide 5

41

Main Index

Contents

Summary Slide 5

§- stacks and a queues are adapter containers that restrict how elements enter and leave a sequence.

- A stack allows access at only one end of the sequence, called the top.

- A queue is a container that allows access only at the front and rear of the sequence. §-Items enter at the rear and exit from the front.

§- Similar to a stack or queue, the priority queue adapter container restricts access operations. §-Elements can enter the priority queue in any order. §- Once in the container, only the largest element may be accessed.

summary slide 6

42

Main Index

Contents

Summary Slide 6

§- A set is a collection of unique values, called keys or set members.

- The set container has a series of operations that allow a programmer to determine if an item is a member of the set and to very efficiently insert and delete items.

§- A map is a storage structure that allows a programmer to use a key as an index to the data.

- Maps do not store data by position and instead use key-access to data allowing a programmer to treat them as though they were a vector or array.