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

## Presentation on theme: "Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing."— Presentation transcript:

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 elementsContainers

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?

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(N 2 ). To sort the array via bubble sort would take order N*log 2 (N) – O(NlogN). Efficiency: Big-O Notation

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)

#include string s1, s2; cin >> s1 >> s2; cout << (s1 + s2); size() returns string size;, =, !=, and == comparison operators; > stream I/O operators; + Concatenates two string objects; [ ] index operator for character access. string class (STL)

#include vector 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)

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: Rationale

Linked 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

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 Data SomeData Value; Node* Next; Node

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

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: Data SomeData Value; Node* Next; Node Node* Previous;

Double-linked list #include list 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)

#include using namespace std; list myList; for ( list ::iterator pos = myList.begin(); pos != myList.end(); ++pos ) { // Current element string s = *myList; } list Traversal Example

// Prefix: ++iter; // Modifies self and returns the NEW value iterator& operator++(); // Postfix: iter++; // Modifies self, but returns a copy of OLD self created prior to the modification iterator 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 ++ Operator

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. Todays Lab

Read chapter 4, prepare for quiz next class. I will randomly question 10 students. Correct answer earns 1%, incorrect earns -1%.Assignment

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

Similar presentations