Announcement Final exam Standard Template Library II Tuesday, May - - PDF document

announcement
SMART_READER_LITE
LIVE PREVIEW

Announcement Final exam Standard Template Library II Tuesday, May - - PDF document

Announcement Final exam Standard Template Library II Tuesday, May 20 th 2:45 4:45pm Rm 70-1620 Announcement Speaking of Exams For those taking SE1 in the fall There will be no exam this week Now a studio


slide-1
SLIDE 1

Standard Template Library II Announcement

  • Final exam

– Tuesday, May 20th – 2:45 – 4:45pm – Rm 70-1620

Announcement

  • For those taking SE1 in the fall

– Now a studio course – No need to sign up for lecture and lab – Register for one studio section.

Speaking of Exams

  • There will be no exam this week
  • Next exam:

– May 1st – Topic list to come

Projects

Mandlebrot table on Web is now fixed. deliverable:

– File Image: OPEN TILL END OF TODAY – Fractal Image: Due May 2nd

  • Questions?

The Plan

  • Today: STL 1
  • Wednesday: STL 2
  • Thursday: Smart Pointers
slide-2
SLIDE 2

The Standard Template Library

  • A general-purpose C++ library of

algorithms and data structures

  • Based on a concept known as generic

programming

  • Implemented by means of the C++

template mechanism

  • Part of the standard ANSI C++ library
  • util package for C++

STL Components

  • containers

– classes that hold stuff

  • iterators

– Used to iterate through containers – Generalization of C++ pointers

  • generic algorithms

– Templated functions

STL Components

  • function objects (Functors)

– Objects that overload operator(); – Substitute for pointers to functions – Beyond the scope of this course

  • adaptors

– adapt other components to special purposes. – Queues and stacks are adaptors

  • Allocators

– encapsulate a memory model. – decouple the algorithms from assumptions about a particular model.

Plan for Today

  • More complex containers
  • Algorithms
  • Function

Sorted Containers

  • Objects are maintained in sorted order
  • Requires Comparator function
  • Examples

– Set – Collection of unique values – Multiset – Collection of non-unique values

Sorted Containers

set<int, less<int> > s; multiset<int, less<int> > ms; for (int i = 0; i < 10; i++) { s.insert(i); s.insert(i * 2); ms.insert(i); ms.insert(i * 2); } s = 0 1 2 3 4 5 6 7 8 9 10 12 14 16 18 ms = 0 0 1 2 2 3 4 4 5 6 6 7 8 8 9 10 12 14 16 18

slide-3
SLIDE 3

Associative Containers

  • Associates a key object with a value object

(Dictionary)

  • Container holds a pair of objects

– Accessed via predefined value_type

  • Examples

– map – hash_map

Maps

map<string, int, less<string> > mymap; mymap.insert(value_type(string("January"), 31)); mymap.insert(value_type(string("February"), 28)); map<string, int, less<string>>::iterator it = mymap.find (string “January”); map<string, int, less<string>>::value_type V = (*it); cout << V.first(); // prints out key (January) cout << V.second(); //prints out value (31)

typedef

  • Means to define a new typename
  • Makes Template types more manageable

typedef definition typename

Maps

typedef map<string, int, less<string> > monthmap; monthmap mymap; mymap.insert(value_type(string("January"), 31)); mymap.insert(value_type(string("February"), 28)); monthmap::iterator it = mymap.find (string “January”); monthmap:: value_type V = (*it); cout << V.first(); // prints out key (January) cout << V.second(); //prints out value (31)

Bitsets

  • space-efficient support for sets of bits.
  • operator[] overloaded to provide access to

individual bits

  • NOT the same as a vector of bool

Bitsets

bitset<16> b1("1011011110001011"); bitset<16> b2; b2 = ~b1; for (int i = b2.size() - 1; i >= 0; i--) cout << b2[i];

slide-4
SLIDE 4

strings

  • strings are actually the char instantiation
  • f the STL basic_string template

(which is a container class).

template <class charT, class traits = char_traits<charT>, class Allocator allocator<charT> > class basic_string; typedef basic_string <char> string;

basic_string

  • Provides capabilities:

– Compare – Append – Assign – Insert – Remove – Replace – various searches – Iterator access

Other containers

  • There are others:

– See SGI docs for full list

  • Questions?

Algorithms

  • A set of commonly used templated

functions.

  • Many work on container objects

– Iterators passed in to indicate positions within containers

Algorithms

template <class Iterator, class T> Iterator find (Iterator first, Iterator last, const T & value) { for (Iterator i = first; i != last && *i != value; ++i); return i; }

Algorithms

list<int> nums; list<int>::iterator nums_iter; nums.push_back (3); nums.push_back (7); nums.push_front (10); // Search the list nums_iter = find(nums.begin(), nums.end(), 3); if (nums_iter != nums.end()) { /* found */ } else { /* not found */ }

slide-5
SLIDE 5

Abridged Catalogue of algorithms

  • Filling & generating

– Fills or a range with a particular value (constant or generated) – fill, fill_n, generate, generate_n

  • Counting

– count, count_if (counts elements w/a given value)

  • Manipulating sequences

– copy, reverse, swap, random_shuffle

Abridged Catalogue of algorithms

  • Searching & replacing

– find, find_if, find_first_of, replace, replace_if

  • Takes predicate as argument! FUNCTOR

– max_element, min_element – search (range searches)

  • Comparing ranges

– equal, mismatch

  • Removing elements

– remove, unique (removes duplicates)

Abridged Catalogue of algorithms

  • Sorting

– sort, partial_sort, nth_element – binary_search, lower_bound, upper_bound – merge – set_union, set_difference, set_intersection

  • Applying an operation to each element

– for_each, transform

  • Takes operation as argument! FUNCTOR!
  • Numeric Algorithms

– Accumulate, partial_sum

Functors

  • Function Objects (or Functors)

– Objects that represent functions – Overrides operator() – Functors are Templated – But why bother when you have functions?

  • Remember maps

map<string, int, less<string> > mymap;

Functors

template <class T> struct less : public binary_function<T, T, bool> { bool operator() (const T &x, const T &y) const { return x < y; } } Note: will only work if < is defined for T.

Functors

  • STL Predicates (returns bool)

!arg logical_not arg1 || arg2 logical_or arg1 && arg2 logical_and arg1 <= arg2 less_equal arg1 >= arg2 greater_equal arg1 < arg2 less arg1 > arg2 greater arg1 != arg2 not_equal_to arg1 == arg2 equal_to

slide-6
SLIDE 6

Functors

  • STL Arithmetic functors
  • arg1

negate arg1 % arg2 modulus arg1 / arg2 divides arg1 * arg2 multiplies arg1 - arg2 minus arg1 + arg2 plus

Functors

// Set up a vector vector<int> v; // Setup a function object

  • ut_times_x<int> f2(2);

// Apply function for_each(v.begin(),v.end(),f2); // Will apply f2 to each element in v // f2.operator() (int) best be defined

Functors and Algorithms

  • Note that generic algorithms do not care if

the operation argument is

– A function – A pointer to a function – Functor

Questions

  • But why use STL?

Top 5 Reasons to use STL

  • 5. Source, 2K / Executable 1.5M
  • 4. Who needs understandable compiler errors?

Top 5 Reasons to use STL

"/opt/SUNWspro/SC5.0/include/CC/./algorithm.cc", line 1015: Error: The operation "std::list<int, std::allocator<int>>::iterator - std::list<int, std::allocator<int>>::iterator" is illegal. "/opt/SUNWspro/SC5.0/include/CC/./algorithm", line 776: Where: While instantiating "std::__final_insertion_sort<std::list<int,std::alloc ator<int>>::iterator>(std::list<int, std::allocator<int>>::iterator, std::list<int, std::allocator<int>>::iterator)". "/opt/SUNWspro/SC5.0/include/CC/./algorithm", line 776: Where: Instantiated from non-template code.

slide-7
SLIDE 7

Top 5 Reasons to use STL

  • 5. Source, 2K / Executable 1.5M
  • 4. Who needs understandable compiler errors?
  • 3. Who needs understandable linker errors?
  • 2. Why make your program look overly complicated

when STL can do it for you?

  • 1. Now there’s a standard way to access elements

beyond the bounds of an array!

But seriously…

  • Lots of functionality
  • Efficiency

– very good performance at low run-time space cost – Generalized algorithms are only provided when their efficiency is good. – The implementation of the containers and algorithms of the STL is not specified in the standard, but the efficiency of each algorithm is.

Reading the docs

  • There’s no standard STL docs

– No man for STL – Except, perhaps the ANSI spec – Docs on webpage

  • From STL vendors

– Roguewave – SGI – categorizes templates

Summary

  • Advanced STL Containers
  • Algorithms
  • Functors
  • Questions?
  • Tomorrow: Smart Pointers