Top Banner
Hold data and provide access to it. Random-access containers: - Allow accessing any element by index - arrays, vectors Sequential containers: - Allow accessing elements by sequence (not by index) - queues, stacks, linked lists - Require iterators to locate elements Containers Containers
17

Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.

Mar 30, 2015

Download

Documents

Braeden Felice
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.

Hold data and provide access to it.Random-access containers:- Allow accessing any element by

index- arrays, vectorsSequential containers:- Allow accessing elements by

sequence (not by index)- queues, stacks, linked lists- Require iterators to locate elements

ContainersContainers

Page 2: Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.

In principle one can solve any problem with arrays or vectors.

In practice queues, stacks and linked lists are more convenient and more efficient performance-wise for select applications.

Why Sequential Containers?Why Sequential Containers?

Page 3: Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.

Suppose we are dealing with N points organized in array.

To copy them from one location to another will take an order of N operations – O(N).

To get i-th point – pt[i] – from array would always take the same constant time – O(1).

To sort the array via brute force approach would take order N*N operations – O(N2).

To sort the array via bubble sort would take order N*log2(N) – O(NlogN).

Efficiency: Big-O NotationEfficiency: Big-O Notation

Page 4: Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.

Huge collection of generic classes at your disposal!

string, vector, queue, dequeue, list, etc.

Include files match class names.

Type class name and click F1 for reference.

Include header file then use IntelliSense.

Standard Template Library (STL)Standard Template Library (STL)

Page 5: Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.

#include <string>string s1, s2;cin >> s1 >> s2;cout << (s1 + s2);

size() returns string size;<, >, <=, >=, !=, and == comparison operators;<< and >> stream I/O operators;+ Concatenates two string objects;[ ] index operator for character access.

string class (STL)string class (STL)

Page 6: Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.

#include <vector>vector<int> v;v.push_back(1);

size(), capacity() returns vector size, capacity;insert() inserts element at specified pos;erase() removes element at specified pos;push_back() adds element at the end;pop_back() removes element at the end.begin() returns position of the first element;end() returns position of the last element.[] index operator

Generic vector class (STL)Generic vector class (STL)

Page 7: Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.

vector is cool, but insert() and erase() operations operate in linear time – O(N).

What if we need to insert and remove quickly?

We can use linked list to insert and remove in constant time – O(1)!

Linked List: RationaleLinked List: Rationale

Page 8: Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.

Linked List: IllustrationLinked List: Illustration

Max

Lex

Nika

Vlad

Joe insert

move down to make room

vector

Max

Lex

Nika

Vlad

Joe insert

linked list

Max

Lex

Nika

Vlad

Joe

Page 9: Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.

Each list node holds data and a pointer to the next list node.

In the last node the pointer to the next node is NULL.

Linked List: Node DataLinked List: Node Data

SomeData Value;

Node* Next;

Node

Page 10: Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.

Node* node = listHead;do{ // do something with current node … // next node node = node->Next} while ( node != NULL);

Linked List: TraversingLinked List: Traversing

Page 11: Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.

Double-Linked List: IllustrationDouble-Linked List: Illustration

Max

Lex

Nika

Vlad

Double-linked list

Page 12: Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.

Each list node holds data and a pointer to the next and previous list node.

In the last node the pointer to the next node is NULL.

In the first node the pointer to the previous node is NULL.

Double-Linked List: DataDouble-Linked List: Data

SomeData Value;

Node* Next;

Node

Node* Previous;

Page 13: Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.

Double-linked list#include <list>list<string> myList;

size() returns list size;insert() / erase() inserts / removes element at specified pos;push_front() adds element at the beginning;push_back() adds element at the end;pop_front() removes element at the beginning;pop_back() removes element at the end;begin() / end() returns position of the first / last element;end() returns position of the last element.Iterator - iterator for the list ++ next element in the list -- previous element in the list * current element

Generic list Class (STL)Generic list Class (STL)

Page 14: Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.

#include <list>using namespace std;

list<string> myList;

for ( list<string>::iterator pos = myList.begin(); pos != myList.end(); ++pos ){ // Current element string s = *myList;}

list Traversal Examplelist Traversal Example

Page 15: Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.

// Prefix: ++iter;// Modifies self and returns the NEW valueiterator& operator++();

// Postfix: iter++;// Modifies self, but returns a copy of OLD self created

prior to the modificationiterator operator++(int);

for (; ; ++pos ) // More efficient (no copying)for (; ; pos++ ) // Equivalent to prefix++ in this loop

MyFunc(pos++); // Firtst MyFunc is called then pos++MyFunc(++pos); // First pos++ then MyFunc is called

Postfix / Prefix ++ OperatorPostfix / Prefix ++ Operator

Page 16: Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.

Using string and list- Read 5 strings from cin- store them in the list in

alphabetically sorted order – HOW??

- Print the list (which must be sorted)

TIP: You can use <, >, <=, >= operators for string comparison.

Today’s LabToday’s Lab

Page 17: Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.

Read chapter 4, prepare for quiz next class.

I will randomly question 10 students. Correct answer earns 1%, incorrect earns -1%.

AssignmentAssignment