C++ Standard Library.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Skip List & Hashing CSE, POSTECH.
Beginning C++ Through Game Programming, Second Edition
C++ for Engineers and Scientists Third Edition
Rossella Lau Lecture 12, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic.
C++ Introduction. Why Learn C++? Very powerful and respected prog. Lang. Very powerful and respected prog. Lang. Highly respected in Academics Highly.
Object Oriented Data Structures
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Data Structures Using C++ 2E
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Built-in Data Structures in Python An Introduction.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Dynamic Memory. We will follow different order from Course Book We will follow different order from Course Book First we will cover Sect The new.
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.
Standard C++ Library Part II. Last Time b String abstraction b Containers - vector.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Using Sequential Containers Lecture 8 Hartmut Kaiser
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Std Library of C++ Part 2. vector vector is a collection of objects of a single type vector is a collection of objects of a single type Each object in.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Arrays Chapter 7.
Recursion Powerful Tool
CS212: Object Oriented Analysis and Design
“Generic Programming” ECE 297
Note: Some of the slides are repeated from Introduction to C++
CSc 110, Autumn 2016 Lecture 26: Sets and Dictionaries
C++ Exceptions.
Data Structures Using C++ 2E
C++ Programming:. Program Design Including
Systems Analysis and Design
Programming with ANSI C ++
Department of Computer Science,
Slides by Steve Armstrong LeTourneau University Longview, TX
Chapter 7 Part 1 Edited by JJ Shepherd
Standard Template Library (STL)
Starting Out with C++ Early Objects Eighth Edition
CSc 110, Spring 2018 Lecture 32: Sets and Dictionaries
Tuesday, February 20, 2018 Announcements… For Today… 4+ For Next Time…
CSc 110, Autumn 2017 Lecture 30: Sets and Dictionaries
Programming Abstractions
Collections Intro What is the STL? Templates, collections, & iterators
Road Map CS Concepts Data Structures Java Language Java Collections
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Introduction to Linked Lists
Today’s Learning Objective
Associative Structures
Lab 03 - Iterator.
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
14 – Sequential Containers
Chapter 9 One-Dimensional Arrays
Algorithm Discovery and Design
The Standard Template Library
CH 9 : Maps And Dictionary
Standard Template Library (STL)
Data Structures & Algorithms
C++ Introduction - 3.
Standard Template Library
Std Library of C++.
Collections Intro What is the STL? Templates, collections, & iterators
Chapter 11: Indexing and Hashing
Standard C++ Library Part II.
An Introduction to STL.
Chapter 3 Lists, Stacks, and Queues
Vectors the better arrays.
A dictionary lookup mechanism
Presentation transcript:

C++ Standard Library

C++ Programming C++ is about efficient programming with abstractions A C++ class is abstract because we can use the class by knowing only its operations (interface) without knowing its implementation

C++ Standard Library The C++ Standard Library is a good example of efficient programming with abstractions The Library defines: A number of container classes A family of generic algorithms Which allow us to write programs that are: Succinct (brief, to the point) Efficient

… C++ Standard Library The standard library does: Handling the details In particular, memory management Allow our programs to focus on our problem instead of coding standard classes and algorithms

Std Lib has Sequential Containers Associative Containers E.g. vector, string Associative Containers Elements ordered by key instead of sequence E.g. map, set

… Std Lib has Generic Algorithms Typically operate on a range of elements in a container (sequential or associative) Offer efficient implementation of various classical algorithms like searching, sorting, copy etc. Are generic in the sense that they work with different kinds of containers and that the containers can hold elements of most types

Library Design Container types provide common interface. E.g. size() operation of all containers (vector, string, map) returns size. Consistent interface across classes and algorithms of standard library makes it easier to learn the library Often possible to take code that works with one container type and change it to work with another container type with minimal changes

Sequential Containers We have used vector and string More details about them are provided in Chapter 9. We are skipping those details to move onto Associative Containers

Associative Containers Elements in an associative container are stored and retrieved by a key In contrast, elements in a sequential container are stored and accessed sequentially by their position within the container Associative containers support efficient lookup and retrieval by a key

map The two primary associative containers are map and set Elements in a map are key-value pairs The key serves as an index into the map The value represents the data that is stored or retrieved Map is also referred to as an associative array

map A good usage of map would be a dictionary. The word would be the key and the meaning (definition) would be the value

Preliminary class: pair pair is a companion type defined in <utility> header Is used by map pair holds two data members pair is a template type taking in two types pair<string, string> anon; // holds two strings pair<string, int> word_count; pair<string, vector<int> > line;

pair pair<string, string> author(“James”, “Joyce”); author.first returns string “James” author.second returns string “Joyce” first and second are public data members of pair

map <map> include header map<string, int> word_count; // empty map from string to int word_count is indexed by a string (!!!) and holds an associated int value. word_count[“Anna”] = 1;

map First word_count[“Anna”] expression is evaluated: word_count is searched for element whose key is “Anna”. Element is not found. A new key-value pair is inserted into word_count. The key is the string “Anna” and the value is value initialized, in this case it is initialized to 0 The new key-value pair is inserted into word_count The value associated with newly inserted element is fetched as an lvalue

map Now the full expression is evaluated: lvalue returned is assigned the value 1 So the value of pair with key “Anna” becomes 1 Note that subscripting a map behaves quite differently from subscripting an array Using an index which is not present adds an element with that index to the map and returns the initialized value. But if the index is present it simply returns the value and in that sense behaves similar to array.

map word_count[“Anna”]++; This time the key “Anna” is already present, so the associated value of 1 is returned as an lvalue The increment operator is applied to the lvalue and so the associated value in the map becomes 2 See first part of word_count.cc where words are read from standard input

Iterator Revision To access elements in a vector (or other containers) we can use iterators instead of subscripts Iterator is a type that Lets us examine elements in a container Lets us navigate from one element to another

… Iterator Revision Std. library defines an iterator type for each of the standard containers Iterators are more general than subscripts. All library containers define iterators but only few support subscript. For example, vector can be used with iterator or subscript map can be used only with iterator

vector iterator vector<int>::iterator iter iter = ivec.begin(); Defines variable iter as an iterator type of vector<int> iter = ivec.begin(); iter refers to first element of ivec. In other words iter refers to ivec[0]

… vector iterator iter = ivec.end(); *iter = 0; iter refers to non-existent element iter is positioned “one past the end” of ivec end() returns a sentinel (watchman/guard) indicating we have processed all the elements in the vector *iter = 0; dereference operator (*) accesses the element that the iterator refers to.

… vector iterator ++iter; Increment operator (++) advances the iterator (iter) to refer to next element in the container Two iterator (variables) can be compared using == or != Iterators are equal if they refer to the same element They are unequal otherwise See vec_assign.cpp (Book code from Chapter 3)

map iterator Note that map elements are ordered by key (and not by the sequence in which they are inserted) map<string, int>::iterator map_it = word_count.begin(); // *map_it refers to a pair<const string, int> object. The beginning object in the map

map iterator cout << map_it->first; // prints key (string) for beginning element; cout << map_it->second; // prints value (int) for beginning element; map_it->first = “new key” //Error; key is const ++map_it->second; //Ok. Value can be changed through iterator See remaining part of word_count.cc and word_count1.cc

Danger of map subscript operator We want to check if “Sai” is present in word_count map int occurs = word_count[“Sai”]; If “Sai” is present we get no. of occurrences, no problem But if “Sai” is not present, pair<string(“Sai”), int(0)> is inserted!!! Value 0 is returned for occurs and so we are fine with the result But an unnecessary pair object is added to the map.

Safe way to check occurrence of a key in a map int occurs = 0; map<string, int>::iterator it = word_count.find(“Sai”); If (it != word_count.end()) // if true then “Sai” key exists in map occurs = it->second; // if false, “Sai” does not exist and so we do not change occurs from its initalized value of 0. As we do not use subscript operator in this case “Sai” key does not get added to the map.

set A set contains only a key There can be only one element with a given key in a set And supports efficient queries on whether a given key is present in the set The (unique) words in a document could be stored in a set

set #include <set> set<string> set1; //empty set set1.insert(“the”); // set1 has 1 element now set1.insert(“and”); // set1 has 2 elements now set1.insert(“the”); // set1 continues to have 2 elements. See word_count2.cc

Assignment 10A Write a dictionary program. While you are at liberty to ideate about the program the usual features a dictionary program will have are: Will have a predefined dictionary of words and their meanings (definitions) Will allow user to lookup meanings of any word in the dictionary Will allow user to add new words and their meanings to the dictionary

Assignment 10B !!!!Optional Assignment!!!! Do exercise 10.32 of the book (page 387). Will require you to first understand text-query program given in Chapter 10.

Book Source Code Copyright Note The course book is C++ Primer, 4th Edition by Lippman, Lajoie and Moo. Any references in earlier files to source files, and use of code within those files, are of example code given in and/or along with the book. As these slides are freely accessible on the Internet, not-for-profit, and for educational purposes, based on the permission related statements in the source code, I have considered that permission has been granted to use them in these slides.