Standard Template Library C++ introduced both object-oriented ideas, as well as templates to C Templates are ways to write general code around objects.

Slides:



Advertisements
Similar presentations
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Advertisements

Vectors, lists and queues
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
Copyright © 2012 Pearson Education, Inc. Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)
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.
STL. What is STL? Standard Templates Library Templates are best used for –Defining containers for storing data –Some kinds of algorithms that work the.
Computer programming 1 Multidimensional Arrays, and STL containers: vectors and maps.
Concept= a set of abstractions (e.g., types) (Generic Programming) Programming with Concepts defined by a set of requirements {vector, deque, list, set,
Templates & STL Instructor: 小黑. Templates  Template serves as a class outline, from which specific classes are generated at compile time.  One template.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
Data Structures Using C++1 Chapter 13 Standard Template Library (STL) II.
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.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Templates and the STL.
CSE 332: Combining STL features Combining STL Features STL has containers, iterators, algorithms, and functors –With several to many different varieties.
Lecture 23 Today Standard Template Library Programs in: programs/p19 Bibliography: Textbook p.252,
CSE 332: C++ Algorithms II From Last Time: Search with Generic Iterators Third generalization: separate iterator type parameter We arrive at the find algorithm.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
Templates and Polymorphism Generic functions and classes
High-Level Programming Languages: C++
Dr. Yingwu Zhu STL Vector and Iterators. STL (Standard Template Library) 6:14:43 AM 2 A library of class and function templates Components: 1. Containers:
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
STL !!!generic programming!!! Anar Manafov
Data Structures Using C++ 2E
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits A General Look at Type Programming in C++ Associated types (the idea) –Let you associate.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Generic Programming Using the C++ Standard Template Library.
Data Structures Using C++ 2E Chapter 13 Standard Template Library (STL) II.
Software Design 1.1 Tapestry classes -> STL l What’s the difference between tvector and vector  Safety and the kitchen sink What happens with t[21] on.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
CS 403, Class 23Slide #1 CS Programming Languages Class 23 November 16, 2000.
Lecture 8-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
Templates “Generic Programming” ECE Templates A way to write code once that works for many different types of variables –float, int, char, string,
CS 403: Programming Languages Lecture 24 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Intro to the C++ STL Timmie Smith September 6, 2001.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
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.
CSE 332: C++ STL iterators What is an Iterator? An iterator must be able to do 2 main things –Point to the start of a range of elements (in a container)
Algorithms CNS 3370 Copyright 2003, Fresh Sources, Inc.
1 ENERGY 211 / CME 211 Lecture 7 October 6, 2008.
CPS Searching, Maps, Tables (hashing) l Searching is a fundamentally important operation  We want to search quickly, very very quickly  Consider.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Introduction to Templates and Standard Template Library 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Templates 16.2.
CPS Searching, Maps, Tables l Searching is a fundamentally important operation ä We want to do these operations quickly ä Consider searching using.
Object-Oriented Programming (OOP) Lecture No. 42.
Unit VI.  C++ templates are a powerful mechanism for code reuse, as they enable the programmer to write code (classes as well as functions) that behaves.
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.
Bubble Sort Selection Sort Insertion Sort Merge Sort Quick Sort
Regarding homework 9 Many low grades
Exceptions, Templates, and the Standard Template Library (STL)
Standard Template Library (STL)
Starting Out with C++ Early Objects Eighth Edition
Standard Version of Starting Out with C++, 4th Edition
Exceptions, Templates, and the Standard Template Library (STL)
Standard Template Library
Standard Template Library
An Introduction to STL.
Presentation transcript:

Standard Template Library C++ introduced both object-oriented ideas, as well as templates to C Templates are ways to write general code around objects or types to be declared by the programmer sort, find, sets, looping

Why Templates void swap(int& a, int& b) { int temp = a; a = b; b = temp; }

Templates allow common logic with different data types template void swap(C& a, C& b) { C temp = a; a = b; b = temp; }

STL Example vector v(3); // Declare a vector of 3 elements. v[0] = 7; v[1] = v[0] + 3; v[2] = v[0] + v[1]; reverse(v.begin(), v.end());

Advantages of using templated containers Common code can be written just once with basic data structures Algorithms on basic structures can be implemented generally (and efficiently) Standard data processing tasks can be reduced to calls to the standard template library allows programmer to focus on value-add parts of code

Comparison to Java In Java, general classes like Vector and List can be programmed Not typed: any object can be put in a vector leads to possible errors in the code No optimization possible based on type knowing both the container and data type at compile time, the best-suited algorithm can be chosen

Data structures/containers Vectors set lists dequeue

Hashes (map in STL) Less known than other containers Work like instant telephone book lookups, or a generic array Input and Output can be of any type e.g. string -> phone number Access is very fast: log(n) on average

Map example Counting number of occurrences of each string

#include using namespace std; int main() { map string_count; string word; // input buffer for words. //--- Read words/tokens from input stream cout -D to end." << endl; while (cin >> word) { string_count[word]++; } //--- Write the count and the word. cout << " --- Word counts ---" << endl; map ::const_iterator iter; for (iter=string_count.begin(); iter != string_count.end(); ++iter) { cout second first << endl; } return 0; }//end main

Functions on standard objects Data structures aren't much good without functions that work on them Generic algorithms can often take any flavour of container sorting works on vectors, lists, queues particular algorithm will take advantage of appropriate aspects of container sorting vectors is faster than sorting linked lists

Types of STL Algorithms Non-Mutating for_each find find_if adjacent_find find_first_of count count_if mismatch equal Mutating sort copy transform replace fill generate reverse random_shuffle partition

Memory Management Typing allows templated functions to be efficient (almost always better than hand-written functions) Surprisingly easy to manage memory

Vector Memory Optins void myfunction () { vector v1(100); // set size to 100 vector v2(100); v2.resize(200); // now storage and size are 200 vector v3; v3.reserve(100); // make room for 100, but size=0 for (i = 1; i < 150; i++) { v3.push_back(i); } In all cases, memory in vector is freed at end of function

Learning the STL The standard template library is not the easiest thing to understand Simple examples cut+paste is definitely the way to start Best done after some C++ programming experience, reading template compiler errors are among the most cryptic I've ever seen Need to be able to "think like a compiler" to interpret them