• Nie Znaleziono Wyników

Programming and Data Structures Programming and Data Structures

N/A
N/A
Protected

Academic year: 2021

Share "Programming and Data Structures Programming and Data Structures"

Copied!
10
0
0

Pełen tekst

(1)

Programming and Data Structures Programming and Data Structures

Lecture 11 Lecture 11

Dr Piotr Cybula <piotr.cybula@wmii.uni.lodz.pl>

Dr Piotr Cybula <piotr.cybula@wmii.uni.lodz.pl>

(2)

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

input/output streams: iostream, fstream, sstream, iomanip

string management: string

standard exceptions: stdexcept

containers:

sequence containers: vector, deque, list

container adapters: stack, queue, priority_queue

associative containers: set, map

iterators: iterator

advanced numerics: complex, numeric, limit

generalized algorithms: algorithm, functional

(3)

vector vector

#include <vector>

sequence container – dynamic array using contiguous storage

fast access by indexing operator []

fast addition/removal at the end of the sequence: push_back, pop_back

slow insertion/removal at positions other than the end: insert, erase (memory reallocation)

vector<int> v;

v.push_back(1);

v.push_back(3);

cout << v.size(); //2 cout << v[1]; //3 v[0] = 5;

cout << v[0]; //5 v.pop_back();

cout << v[1]; //out_of_range exception v.clear();

(4)

deque deque

#include <deque>

sequence container – double-ended queue, dynamic array using non- contiguous storage, a list of arrays

fast addition/removal at the beginning and at the end of the sequence:

push_front, pop_front, push_back, pop_back

a little slower than for the vector access by indexing operator []

a little faster than for the vector insertion/removal at positions other than the beginning or the end: insert, erase

deque<int> d;

d.push_back(1);

d.push_back(3);

d.push_front(2);

cout << d.size(); //3 cout << d[1]; //1 d.pop_front();

(5)

list list

#include <list>

sequence container – double-linked list using non-contiguous storage

fast addition/insertion/removal at any position of the sequence: push_front, pop_front, push_back, push_front, insert, erase, remove

ordering methods: reverse, sort

no indexing operator []

list<int> l;

l.push_back(1);

l.push_back(3);

l.push_front(4);

l.push_front(2); //2 4 1 3 l.reverse(); //3 1 4 2 cout << l.size(); //4

l.sort(); //1 2 3 4

l.remove(3); //1 2 4

(6)

stack stack

#include <stack>

container adapter – adapts the deque container to LIFO structure

changing: push, pop

reading: top, empty, size

stack<int> s;

s.push(1);

s.push(2);

s.push(3);

cout << s.top(); //3 cout << s.size(); //3 s.pop();

cout << s.top(); //2 cout << s.size(); //2 if( !s.empty() )

{ … }

(7)

queue queue

#include <queue>

container adapter – adapts the deque container to FIFO structure

changing: push, pop

reading: front, back, empty, size

queue<int> q;

q.push(1);

q.push(2);

q.push(3);

cout << q.front(); //1 cout << q.back(); //3 cout << q.size(); //3 q.pop();

cout << q.front(); //2 cout << q.size(); //2 if( !q.empty() )

{ … }

(8)

priority_queue priority_queue

#include <queue>

container adapter – adapts the vector container to a heap structure ensuring the ordering of its items from the greatest to the lowest (requires operator<)

changing: push, pop

reading: top, empty, size

priority_queue<int> q;

q.push(1);

q.push(3);

q.push(2);

cout << q.top(); //3 cout << q.size(); //3 q.pop();

cout << q.top(); //2 cout << q.size(); //2 if( !q.empty() )

{ …

(9)

set set

#include <set>

associative container – stores unique elements in the structure of the binary search tree (unordered_set is a hash table)

changing: insert, erase, clear

reading: count, empty, size

set<int> s;

s.insert(1);

s.insert(2);

s.insert(1);

cout << s.size(); //2 s.erase(2);

cout << s.size(); //1 cout << s.count(1); //1 cout << s.count(2); //0 if( !s.empty() )

{ … }

(10)

map map

#include <map>

associative container – stores unique keys with mapped values in the structure of the binary search tree (unordered_map is a hash table)

changing: [], erase, clear

reading: [], count, empty, size

map<int, string> m;

m[1] = "first";

m[2] = "second";

m[3] = "third";

cout << m.size(); //3

cout << m[3]; //third m.erase(2);

cout << m.size(); //2 cout << m.count(1); //1 if( !m.empty() )

{ …

Cytaty

Powiązane dokumenty

● destructor, copy constructor and assignment operator (as for bounded) insertion/removal

(3) set a dedicated pointer (killer) to the last node using the address in the next pointer of the node pointed to by tmp (or the head pointer if tmp is empty, or the tail pointer

● during instantiation of a template with a specific type, the compiler replaces each instance of a parametric type with the specified type and compiles the data structure

(1) set the pred pointer on the node preceding the deleted node, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the

(1) create two auxiliary pointers: set pred to an empty address, and succ on to the address of the first node (head) and pass the auxiliary pointers towards the end of the list

(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking

(1) invoke two auxiliary pointers set on the adjacent nodes pred and succ (assume that the pointers next in the preceding nodes pointed to by succ are already inverted, succ is

● input/output streams: iostream, fstream, sstream, iomanip. ● string