Hold data and provide access to it. Random-access containers: -Allow accessing any element by index...
-
Upload
braeden-felice -
Category
Documents
-
view
219 -
download
0
Transcript of Hold data and provide access to it. Random-access containers: -Allow accessing any element by index...
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
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?
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
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)
#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)
#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)
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
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
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
Node* node = listHead;do{ // do something with current node … // next node node = node->Next} while ( node != NULL);
Linked List: TraversingLinked List: Traversing
Double-Linked List: IllustrationDouble-Linked List: Illustration
Max
Lex
Nika
Vlad
Double-linked list
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;
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)
#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
// 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
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
Read chapter 4, prepare for quiz next class.
I will randomly question 10 students. Correct answer earns 1%, incorrect earns -1%.
AssignmentAssignment