1 Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers Adapter Classes Adapter Classes

Download 1 Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers Adapter Classes Adapter Classes

Post on 28-Dec-2015




2 download

Embed Size (px)


  • *

    Container TypesSequence ContainersAssociative ContainersAdapter Classes

    Stack ContainerQueue ContainerPriority Queue ContainerSet ContainerMap Container

    C++ Arrays

    Containers Overviewand Class Vector


    Templated Insertion Sort

    Template ClassesStore Class

  • *STLC++ STL provides 10 container classes4 additional types considered near-containersarraystringbitsetvalarrayExtensions to Standardslistrope huge stringsunordered_set, unordered_map, unordered_multiset, unordered_multimap

  • *Container Categories

    Sequence ContainersAdapter ContainersAssociative ContainersVectorStackSet, MultisetDequeQueueMap, MultimapListPriority Queue

  • *Sequence ContainersA sequence container stores data by position in linear order 1st element, 2nd element, and so forth

  • *Associative ContainersAssociative containers store elements by key Ex: name, SS #, or part numberA program accesses an element in an associative container by its keyMay bear no relationship to the location of the element in the containerAccess by key O(lg N)Implementation?

  • *Adapter ClassesUse a sequence container as underlying storage structureE.g. Stack and Queue use DequeDesign Pattern: AdapterAdapters interface provides a restricted set of operations from underlying storage structureHow is stack restricted? queue?

  • *Stack ContainersA stack allows access at only one end of sequence, called the top.

  • *Queue ContainersA queue is a container that allows access only at front and back

  • *Priority Queue Containerspriority queue -- HPFOPush elements in any order -- pop operation removes the largest (or smallest) value

  • *Set ContainersA set is a collection of unique values, called keys or set members.

  • *Map ContainersA map is a storage structure that implements a key-value relationship.

  • *C++ ArraysFixed-size collection homogenous Stores the n (size) elements in contiguous block

  • *Evaluating an Array as a ContainerSize is fixed at the time of its declarationDoes an array know its size?C++ arrays do not allow the assignment of one array to anotherRequires loop structure with the array size as an upper bound*or* what algorithm?

  • *Vectors

  • *

  • *

  • *

  • *

  • *

  • *

  • *iterator insert (iterator pos, const T& value);Insert value before pos, and return an iterator pointing to the position of the new value in the vector. The operation invalidates any existing iterators beyond pos.Postcondition:The list has a new element.

  • *

  • *Output with Vectorstemplate void writeVector (const vector& v){// capture the size of the vector in nsize_t i, n = v.size ();for (i = 0; i < n; ++i)cout
  • *Declaring Vector Objects// vector of size 5 containing the integer // value 2vector intVector (5, 2);// vector of size 10; each element // contains the empty string vector strVector (10);

  • *Adding and Removing Vector Elements

  • *Resizing a Vectorint arr[5] = {7, 4, 9, 3, 1};vector v (arr, arr + 5);// v initially has 5 integersv.resize (10);// size is doubledv.resize (4);// vector contracted; data // is lost

  • *Insertion SortCan use with vectors or arraysCount comparisonsBest-case run time?T(N) = N - 1 = O(N) Worst-case?

  • *Insertion Sort Algorithm// sort a vector of type T using insertion // sorttemplate void insertionSort (vector& v){// Place v[i] into the sublist v[0] ... // v[i-1], 1
  • *Insertion Sort Algorithmfor (size_t i = 1; i < n; ++i) {// Index j scans down list from v[i] // looking for correct position to // locate target. Assigns it to v[j] size_t j = i; T temp = v[i]; // Locate insertion point by scanning // downward as long as temp < v[j-1] // and we have not encountered the // beginning of the list

  • *Insertion Sort Algorithmwhile (j > 0 && temp < v[j-1]) { // Shift elements up list to make // room for insertion v[j] = v[j-1]; --j; } // Location is found; insert temp v[j] = temp; }}

  • *Class TemplatesCan templatize classes as well as functionsAll container classes in STL are templatesSyntaxtemplate class ClassName { }Concrete Store template example

  • *template class Store {public:// Ctor: init w/item or default TStore (const T& item = T()): m_value (item) { }

    T getValue () const { return m_value;} Class Template Store

  • *void setValue (const T& item) {m_value = item;} private:T m_value;};

    Use:Store s;s.setValue (4.3);Class Template Store (Contd)