Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming in C++ Michal Brabec Petr Malý. Standard Template Library Containers - vector, map, set, deque, list Algorithms - copy, replace, sort, find.

Similar presentations


Presentation on theme: "Programming in C++ Michal Brabec Petr Malý. Standard Template Library Containers - vector, map, set, deque, list Algorithms - copy, replace, sort, find."— Presentation transcript:

1 Programming in C++ Michal Brabec Petr Malý

2 Standard Template Library Containers - vector, map, set, deque, list Algorithms - copy, replace, sort, find Iterators - Input, Output, Forward, Bidirectional, Random-access Function objects - equal_to, greater, less, logical_and

3 Containers vector, map, set, deque, list,... parametrized by another type(s) each container has specific member functions push_back, pop, front, back, insert,... common interface – iterators

4 Algorithms #include copy, replace, sort, find,... generic functions which manipulates with the container’s elements algorithms use iterators

5 Iterators Iterators are similar to pointers * dereference operator pointer arithmetic ContainerType::iterator, ContainerType::const_iterator myContainer1.begin(), begin(myContainer1); points to the first element of the container or end iterator myContainer1.end(), end(myContainer1); refers to the the past-the-end element should not be dereferenced

6 Loops std::vector myVec1 = { 10, 20, 30, 40, 50, 60, 70 }; for (int i = 0; i < myVec1.size(); ++i) std::cout << myVec1[i] << " "; // the type of i is std::vector ::const_iterator for (auto i = myVec1.begin(); i != myVec1.end(); ++i) std::cout << *i << " "; for (auto i : myVec1) std::cout << i << " "; std::for_each(myVec1.begin(), myVec1.end(), [](int i){ std::cout << i << " "; );

7 Iterator Categories categorypropertiesvalid expressions all categories copy-constructible, copy-assignable and destructible X b(a); b = a; Can be incremented ++a a++ Random Access Bidirectional Forward Input Supports equality/inequality comparisons a == b a != b Can be dereferenced as an rvalue *a a->m Output Can be dereferenced as an lvalue (only for mutable iterator types) *a = t *a++ = t default-constructible X a; X() Multi-pass: neither dereferencing nor incrementing affects dereferenceability { b=a; *a++; *b; } Can be decremented --a a-- *a-- Supports arithmetic operators + and - a + n n + a a - n a - b Supports inequality comparisons (, =) between iterators a b a = b Supports compound assignment operations += and -= a += n a -= n Supports offset dereference operator ([])a[n]

8 Task Create a global function remove_same Function gets two parameters: Container1 cnt1 – any STL container Container2 cnt2 – any STL container Function removes all elements which are contained in both objects

9 Task Use code from previous practicals Create a program which loads the complex numbers from the file The program stores the number ordered by their absolute value in other file Use STL functions / containers


Download ppt "Programming in C++ Michal Brabec Petr Malý. Standard Template Library Containers - vector, map, set, deque, list Algorithms - copy, replace, sort, find."

Similar presentations


Ads by Google