Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Standard Library.

Similar presentations


Presentation on theme: "C++ Standard Library."— Presentation transcript:

1 C++ Standard Library

2 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

3 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

4 … 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

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

6 … 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

7 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

8 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

9 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

10 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

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

12 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;

13 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

14 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;

15 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

16 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.

17 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

18 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

19 … 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

20 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]

21 … 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.

22 … 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)

23 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

24 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

25 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.

26 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.

27 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

28 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

29 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

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

31 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.


Download ppt "C++ Standard Library."

Similar presentations


Ads by Google