STL headers Defined in std namespace Headers are of form - - PDF document

stl headers
SMART_READER_LITE
LIVE PREVIEW

STL headers Defined in std namespace Headers are of form - - PDF document

16 . Standard Template Library (STL) Standard Template Library Promotes Reuse Saves time and effort Better quality part of ANSI/ISO C++ standard STL headers Defined in std namespace Headers are of form


slide-1
SLIDE 1
  • 16. Standard Template Library (STL)

Standard Template Library

  • Promotes Reuse
  • Saves time and effort
  • Better quality
  • part of ANSI/ISO C++ standard
slide-2
SLIDE 2

STL headers

  • Defined in std namespace
  • Headers are of form <headername> without the .h
  • Containers:

– <vector>, <list>, <deque>, <queue>, <stack>, <map> (also multimap), <set> (also multiset), <bitset>

  • Iterators:

– <iterator>

  • Algorithms:

– <algorithm>

  • Strings:

– <string>

STL Containers

  • Sequence Containers

– vector insert/delete at back, access any elem – deque insert/delete front/back, access any elem

  • Double ended queue (deck)

– list doubly linked list, insert/delete any where

  • Associative Containers

– set lookup, no duplicates – multiset lookup, duplicates OK – map

  • ne-to-one mapping, no duplicates, key-based lookup

– multimap

  • ne-to-many, duplicates OK, key-based lookup
  • Container Adapters

– stack Last-in-first-out – queue First-in-first-out – priority_queue highest priority element is first out

slide-3
SLIDE 3

Container Types

  • First-class Containers:

– Sequence containers and Associative containers

  • Near Containers (Almost containers)

– arrays, string, bitset, valarray

Which one to choose?

  • Standard Containers are interchangeable
  • Each container is efficient for a certain usage

– Key based look up: map – List operations: list – Add/remove elements at end: deque, stack, queue – Most common usage: vector

slide-4
SLIDE 4

Common Operations and Members

  • Common Member Types

– value_type, allocator_type, size_type, difference_type, iterator, const_iterator, reverse_iterator, const_reverse_iterator, reference, const_reference, [for associative only: key_type, mapped_type, key_compare]

  • Common Operations

– Iterators

  • begin(), end(), rbegin(), rend()

– Elements access

  • front(), back(), [not for list: [] (unchecked

subscripting), at()]

– Stack and Queue operations

  • push_back(), pop_back(), [list and deque only:

push_front(), pop_front()]

– List Operations

  • insert(), erase(), clear()

Containers and Efficiency

[] List Ops Front Ops Back Ops Iterator Type vector O(1) O(n)+ O(1)+ Random list O(1) O(1) O(1) Bidirectional deque O(1) O(n) O(1) O(1) Random stack O(n) O(1) queue O(n) O(1) O(1) priority_queue O(log n) O(log n) map O(log n) O(log n)+ Bidirectional multimap O(log n)+ Bidirectional set O(log n) O(log n)+ Bidirectional multiset O(log n)+ Bidirectional string O(1) O(n)+ O(n)+ O(1)+ Random array O(1) Random valarray O(1) Random bitset O(1) Source: C++ Programming Language, 3rd. Ed., Bjarne Stroupstrup. "+: May incurr extra cost occasionally" Sequence Adaptor Associative Near

slide-5
SLIDE 5

Iterators

  • Iterators used to point to an element of a container
  • Container’s

– begin() returns an iterator pointing to the first element – end() returns an iterator pointing to the end (next to last)

  • Use instance of iterator to point to element you want

to modify

  • Use instance of const_iterator to point to element

you do not want to modify

  • *iter refers to the pointed element
  • ++iter advances the iterator to point to the next

element

Category of Iterators and Operations

input

  • utput

forward bidirectional random access

++itr itr++ *itr (rvalue) itr1 = itr2 itr1 == itr2 itr1 != itr2 ++itr itr++ *itr(lvalue)

  • -itr

itr-- itr += i itr -= i itr [i] itr1 <= itr2 itr1 >= itr2

not all operations listed

slide-6
SLIDE 6

Iterators Aid Generic Programming

Iterator Container Algorithm

Iterators decouple Algorithms from Containers

Algorithms in STL

  • Algorithms are used to manipulate containers
  • Loosely coupled to the containers
  • Works on Iterators returned by Containers
  • Returns Iterator to element/result
  • Examples: sort, search, delete, find, insert, etc.
slide-7
SLIDE 7

STL Algorithms

  • Mutating-sequence algorithms:

– copy, copy_backward, fill, fill_n, generate, generate_n, iter_swap, partition, random_shuffle, remove, remove_copy, remove_copy_if, remove_if, replace, replace_copy, replace_copy_if, replace_if, reverse, reverse_copy, rotate, rotate_copy, stable_partition, swap, swap_ranges, transform, unique, unique_copy

  • Non-mutating-sequence algorithms:

– adjacent_find, count, count_if, equal, find, for_each, mismatch, search, search_n

  • Numerical algorithms

– accumulate, inner_product, partial_sum, adjacent_difference

Lab Work: Details provided on-line.