CNS 3370.  Sequences  vector,deque,list,(string),forward_list  Container Adapters  queue, stack, priority_queue  Associative Containers  set, unordered_set.

Slides:



Advertisements
Similar presentations
M The University Of Michigan Andrew M. Morgan EECS Lecture 22 Savitch Ch. 16 Intro To Standard Template Library STL Container Classes STL Iterators.
Advertisements

C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
More on the STL vector list stack queue priority_queue.
OOP Etgar 2008 – Recitation 101 Object Oriented Programming Etgar 2008 Recitation 10.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
Standard Template Library (STL) Overview – Part 1 Yngvi Bjornsson.
CMSC 202 Lesson 24 Iterators and STL Containers. Warmup Write the class definition for the templated Bag class – A bag has: Random insertion Random removal.
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
Rossella Lau Lecture 12, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
CSE 332: C++ Algorithms II From Last Time: Search with Generic Iterators Third generalization: separate iterator type parameter We arrive at the find algorithm.
Writing Your Own STL Container Ray Lischner
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Standard Template Library There are 3 key components in the STL –Containers –Iterators –Algorithms Promote reuse More debugged May be more efficient.
STL !!!generic programming!!! Anar Manafov
Data Structures Using C++ 2E
Containers Overview and Class Vector
Containers and Iterators CNS 3370 Copyright 2003, Fresh Sources, Inc.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Overview 18.1 Iterators 18.2 Containers 18.3 Generic Algorithms Slide
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Generic Programming Using the C++ Standard Template Library.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
The C++ Standard Template Library. What is STL A subset of the standard C++ library –The string class is not part of it! 3 major components –Algorithms.
C++ STL CSCI 3110.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
Templates code reuse - inheritance - template classes template classes - a class that is not data-type specific - eg. a class of Array of any type - intArray,
1. The term STL stands for ? a) Simple Template Library b) Static Template Library c) Single Type Based Library d) Standard Template Library Answer : d.
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen.
An Introduction to STL. The C++ Standard Template Libraries  In 1990, Alex Stepanov and Meng Lee of Hewlett Packard Laboratories extended C++ with a.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
CS 403, Class 23Slide #1 CS Programming Languages Class 23 November 16, 2000.
 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Chapter 15 - Class string and String Stream.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
Lecture 7 : Intro. to STL (Standard Template Library)
Computer Science and Software Engineering University of Wisconsin - Platteville 11.Standard Template Library Yan Shi CS/SE 2630 Lecture Notes.
CS 403: Programming Languages Lecture 24 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
A gentle introduction to the standard template library (STL) Some references:
1 STL Containers Copyright Kip Irvine, All rights reserved. Only students enrolled in a class at Florida International University may copy or print.
The Standard Template Library Container Classes Version 1.0.
The List ADT Reading: Sections 3.2, 3.3, 3.5.
C++ Review STL CONTAINERS.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
1 The Standard Template Library The STL is a collection of Container classes These are class templates for containers. A container is an object that stores.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
CHAPTER 11 CS 3370 – C++ Associative Containers. The ordered containers used tree-based storage O(log n) retrieval complexity The unordered containers.
Final Exam Review COP4530.
Introduction to olympic programming
Standard Template Library (STL)
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Chapter 22: Standard Template Library (STL)
Generic Programming Karl Lieberherr 12/1/2018 Generic Programming.
Lists - I The List ADT.
Lists - I The List ADT.
Iterators and STL Containers
STL Библиотека стандартных шаблонов
Standard Template Library
Standard Template Library
Chapter 3 Lists, Stacks, and Queues
The List Container and Iterators
Standard Template Library
Presentation transcript:

CNS 3370

 Sequences  vector,deque,list,(string),forward_list  Container Adapters  queue, stack, priority_queue  Associative Containers  set, unordered_set  map, unordered_map  plus multi_ versions of each of the above

bool empty();// use instead of size() == 0 size_t size(); void resize(size_t, T = T()); T& front(); T& back();// Not forward_list void push_back(const T&);// Not forward_list void pop_back();// Not forward_list

// deque and list/forward_list only: void push_front(const T&); void pop_front(); //deque, vector and string only: T& at(size_type n);// range-checked T& operator[] shrink_to_fit();

 Generalization of a pointer  Overload at least operator!=, operator==, operator*, operator++, operator->  Some overload operator--, operator[], pointer arithmetic

template Iterator find(Iterator start, Iterator past, const T& v) { while (start != past) { if (*start == v) break; ++start; } return start; } All algorithms are implemented in terms of iterators

 Input  Output  Forward  Bi-directional  Random Access

 Modeled after file input  Read-only access to elements  Single-pass, forward traversal  find expects an Input Iterator  it only needs to read through the data once

template InputIterator find(InputIterator start, InputIterator past, const T& v) { while (start != past) { if (*start == v) break; ++start; } return start; }

 Modeled after file output  Write-only access to elements  Single-pass, forward traversal  Example: ostream_iterator: copy(a, a+n, ostream_iterator (cout,“ “));

 Both read and write access  can be used as Input or Output Iterators  Multiple-pass, forward traversal  unique expects a Forward Iterator list ::iterator p = unique(lst.first(), lst.end());  It needs 2 simultaneous, read/write iterators

 Can do everything a Forward Iterator can  Also support backwards traversal  operator--()  operator--(int)  reverse requires a Bi-directional Iterator

list ::iterator p = lst.end(); while (p != lst.begin()) { --p;// “advances” backwards // process *p }

list ::reverse_iterator p = lst.rbegin(); while (p != lst.rend()) { // process *p, then: ++p;// “advances” backwards }

 Modeled after pointers  support Pointer Arithmetic in constant time  operator+, +=, -, -=, [],, >=  sort expects a Random Access Iterator

“Functional” inheritance via duck typing (not via classes)

 Doesn’t provide a Random Access Iterator  Generic sort will fail on a list  Answer:  Provides its own sort member function  Also merge, remove, and unique

vector v1; … // fill v1, then: vector v2; copy(v1.begin(), v1.end(), v2.begin());

 Iterators work in overwrite mode by default  Need an insert mode for cases like above  that calls the appropriate insert operation provided by the container

 Replace output calls ( operator*, operator=, etc.) with appropriate insert function  back_insert_iterator  calls push_back  front_insert_iterator  calls push_front  insert_iterator  calls insert

vector v1; … // fill v1, then: vector v2; copy(v1.begin(), v1.end(), back_inserter(v2)); (But there is still a better way)

 back_inserter  creates a back_insert_iterator  front_inserter  creates a front_insert_iterator  inserter  creates an insert_iterator

 ostream_iterator  an Output Iterator copy(v1.begin(), v1.end(), ostream_iterator (cout, “ “));  istream_iterator  an Input Iterator copy(istream_iterator (cin), istream_iterator (), back_inserter(v1));

 insert, assign, erase, and constructors  Usually more efficient than the generic algorithm counterparts  Prefer over using copy  See range-based.cpp

 For erasing selected elements of a sequence  applies to vector, deque, string  for lists, use remove/remove_if  The idiom:  The remove algorithm reorders the sequence, moving the deleted elements to the end ▪ returning an iterator to the first deleted element  c.erase(remove(beg, end, x), end);  c.erase(remove_if(beg, end, pred), end);

Higher-level Containers that use Sequences

 High-level abstract data types  queue, stack, priority_queue  They “adapt” sequences for specific uses  i.e., they use a sequence for implementation  stack & queue use deque by default  priority_queue uses a vector  can change the underlying sequence: stack > myStack;  No iterators are provided  they offer a more restricted interface

 queue:  pushes at end, pops from front  front, back, push, pop  stack:  pushes and pops at front  top, push, pop  priority_queue: (See pq.cpp, pq2.cpp)  retrieves elements in priority order  you provide a strict weak ordering

 priority_queue and ordered associative containers (set, map, multi_set, multi_map) require strict weak ordering comparators  behave like less ( ) ( (which calls operator<( ))  never use <= or anything like it!!!  Definition: f(x,y) is a SWO if:  f(x,x) = false(irreflexive)  f(x,y) = !f(y,x)(anti-symmetric)  f(x,y) && f(y,z) => f(x,z)(transitive)

 Two “flavors”  Ordered  tree-based storage  O(log n) retrieval  set, multi_set, map, multi_map  Unordered  hashed-based storage  O(1) retrieval  unordered_set, unordered_map

#include using namespace std; int main() { // Populate a set: set s; s.insert("Alabama"); s.insert("Georgia"); s.insert("Tennessee");

// Print it out: auto p = s.begin(); while (p != s.end()) cout << *p++ << endl; cout << endl; // Do some searches: string key = "Alabama"; p = s.find(key); cout << (p != s.end() ? "found " : "didn't find ") << key << endl; key = "Michigan"; p = s.find(key); cout << (p != s.end() ? "found " : "didn't find ") << key << endl; }

// Output: Alabama Georgia Tennessee found Alabama didn't find Michigan

#include using namespace std; int main() { // Insert some elements (two ways): map > m; m.insert(make_pair(string("Alabama"), string("Montgomery"))); m["Georgia"] = "Atlanta"; m["Tennessee"] = "Knoxville"; m["Tennessee"] = "Nashville"; // overwrites

// Print the map: auto p = m.begin(); while (p != m.end()) { auto elem = *p++; cout << '{' << elem.first << ',’ << elem.second << "}\n"; } cout << endl;

// Retrieve via a key: cout << '"' << m["Georgia"] << '"' << endl; cout << m.size() << endl; cout << '"' << m["Texas"] << '"' << endl; cout << m.size() << endl; } // Output: {Tennessee,Nashville} {Georgia,Atlanta} {Alabama,Montgomery} "Atlanta" 3 "" 4

 Shows the conciseness of map’s design  Count the number of each word in a text file  wordcount.cpp  output in wordcount-gettysburg.out

 Necessary to maintain proper order in the underlying tree data structure  They test for equivalence, not equality, to maintain uniqueness  x and y are equivalent iff !cmp(x,y) && !cmp(y,x)  i.e., neither precedes the other  Example:  swo.cpp  ignores non-alpha characters in strings