• 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!
8
0
0

Pełen tekst

(1)

Programming and Data Structures Programming and Data Structures

Lecture 12 Lecture 12

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)

Iterators Iterators

iterators are generalized pointers to the items of containers

iterators (like pointers) can be initialised (begin, rbegin), incremented (++), decremented (--), dereferenced (*, ->), compared (==, !=) or tested whether they are empty (end, rend)

iterators are similar for all the containers, thus any change of the container used is transparent for existing code

vector<int> v = {1, 2, 3, 4, 5};

vector<int>::iterator i; //iterator

for(i = v.begin(); i != v.end(); i++) //explicit use cout << (*i) << endl;

for(i = v.rbegin(); i != v.rend(); i--) //explicit reverse use cout << (*i) << endl;

for(auto a: v) //implicit use cout << a << endl;

(4)

Iterators Iterators

some methods of sequence containers require iterators, e.g. insert and erase operations for vectors, deques and lists

some methods of associative containers return iterators, e.g. find operation for sets and maps

list<int> l = {1, 2, 3, 4, 5};

l.insert(l.begin() + 1, 7); //1 7 2 3 4 5 l.erase(l.end() - 1); //1 7 2 3 4

map<int, string> m = {{1,"aaa"}, {2,"bbb"}, {3,"ccc"}};

map<int, string>::iterator i = m.find(2);

if (i == m.end())

cout << "not found";

else cout << i->first << ":" << i->second; //2:bbb

(5)

Generalized sorting Generalized sorting

#include <algorithm>

sort function sorts in the ascending order the contents of a container (between the starting and the ending iterator)

container items must support operator<

list<int> l;

for(int i = 0; i < 10; i++)

l.push_back(rand() % 100); //random values sort(l.begin(), l.end()) //in-place sorting

(6)

Generalized copying Generalized copying

#include <algorithm>

copy function copies a cantainer data to another container using the starting and ending iterators of the source container, and the destination iterator

the destination container should be preallocated

vector<int> v1, v2(10);

for(int i = 0; i < 10; i++)

v1.push_back(rand() % 100); //random values

copy(v1.begin(), v1.end(), v2.begin()); //v1 → v2

(7)

Insertion iterators Insertion iterators

#include <iterator>

template iterators to fill cantainers (front_insert_iterator, back_insert_iterator) without preallocation

template functions generating the iterators: front_inserter, back_inserter

vector<int> v1, v2;

for(int i = 0; i < 10; i++)

v1.push_back(rand() % 100); //random values

back_insert_iterator< vector<int> > i(v2); //iterator copy(v1.begin(), v1.end(), i); //v1 → v2

//alterative form:

copy(v1.begin(), v1.end(), back_inserter(v2)); //v1 → v2

(8)

Stream iterators Stream iterators

#include <iterator>

template iterators to copy data to/from streams (istream_iterator, ostream_iterator)

vector<int> v;

for(int i = 0; i < 10; i++)

v.push_back(rand() % 100); //random values

ostream_iterator<int> out(cout, ","); //stream iterator copy(v.begin(), v.end(), out); //v → cout

Cytaty

Powiązane dokumenty

Das formale Design, das sich hier vornehmlich in der diskursiv fortge- setzten Konstanz der topographischen Ordnung von Text und Bild und im typo- graphischen Schriftbild (der

What this means is that these practices create mi- cro-spheres which are immersed in the global economic system and have a chance and opportunity to make new forms

● automatically created if (and only if) there are no explicit constructors, but without any initialisation of the fields (risky when there are any dynamically allocated fields in

● the public method changing the state of an object (modifier) is to check the compliance of such a change with the rules described by invariants. ● if the rules are exceeded,

● 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