Generic Programming Techniques in C++

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.
Thrust & Curand Dr. Bo Yuan
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Vectors, lists and queues
STL Antonio Cisternino. Introduction The C++ Standard Template Library (STL) has become part of C++ standard The main author of STL is Alexander Stephanov.
CSE 332: C++ Algorithms I The C++ Algorithm Libraries A standard collection of generic algorithms –Applicable to various types and containers E.g., sorting.
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.
CSE 332: Combining STL features Combining STL Features STL has containers, iterators, algorithms, and functors –With several to many different varieties.
CSE 332: C++ STL algorithms C++ STL Algorithms Generic algorithms –Apply to a wide range of types E.g., sorting integers ( int ) vs. intervals ( pair )
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.
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.
CSE 332: C++ STL algorithms C++ STL Algorithms Generic algorithms –Apply to a wide range of types E.g., sorting integers (long) or intervals (long, long)‏
Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
C++ Programming Part 2 Michael Griffiths Corporate Information and Computing Services The University of Sheffield
CSE 332: C++ template examples Concepts and Models Templates impose requirements on type parameters –Types that are plugged in must meet those requirements.
Lecture 19 CIS 208 Wednesday, April 06, Welcome to C++ Basic program style and I/O Class Creation Templates.
Lecture 8-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
Overview of C++ Templates
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for.
Lecture 7-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
CSE 332: C++ templates and generic programming II Review: Concepts and Models Templates impose requirements on type parameters –Types that are plugged.
Lecture 36 OOP The STL Standard Template Library
Motivation for Generic Programming in C++
CS212: Object Oriented Analysis and Design
“Generic Programming” ECE 297
Data Structures and Algorithms
Andy Wang Object Oriented Programming in C++ COP 3330
Concepts of Programming Languages
Regarding homework 9 Many low grades
Lecture 7-2 : STL Iterators
CSCE 210 Data Structures and Algorithms
Command Line Arguments
STL – Standard Template Library
Starting Out with C++ Early Objects Eighth Edition
Tuesday, February 20, 2018 Announcements… For Today… 4+ For Next Time…
The C++ Algorithm Libraries
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Lecture 7-3 : STL Algorithms
C++ Templates L03 - Iterator 10 – Iterator.
10 – Iterators C++ Templates 4.6 The Iterator pgs
Lecture 7-2 : STL Iterators
CSE 332 Overview and Structure
Today’s Learning Objective
C++ Functions, Classes, and Templates
Random Number Generation
Lab 03 - Iterator.
Object-Oriented Programming (OOP) Lecture No. 39
priority_queue<T>
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
Andy Wang Object Oriented Programming in C++ COP 3330
Object-Oriented Programming (OOP) Lecture No. 37
Lists - I The List ADT.
Lists - I The List ADT.
Lecture 8 : Intro. to STL (Standard Template Library)
Standard Template Library (STL)
C++ Templates L03 - Iterator 10 – Iterator.
Lecture 8-2 : STL Iterators and Algorithms
C++ Templates L03 - Iterator 10 – Iterator.
Overview of C++ Polymorphism
STL (Standard Template Library)
Standard Template Library
Pointers & Functions.
C++ Programming: chapter 10 – STL
An Introduction to STL.
C++ Templates L04 - Iterator 10 – Iterator.
Presentation transcript:

Generic Programming Techniques in C++ Generic programming allows diverse types to be used together Without requiring inheritance relations among them Lets native, library, and user-defined types work with each other well Generic abstractions rely on interface polymorphism Based on C++ templates, typedefs, and compiler’s type inference rules Allow many combinations to be composed successfully Impose specific rules on types’ interfaces Today we’ll look at some examples for why this is useful To motivate investment of effort towards a deeper understanding To show some early techniques that you can apply right away Subsequent lectures and studios will dig into this in more detail We’ll look first at key generic abstractions the STL provides Containers that can hold diverse types Iterators that define ranges, act like built-in pointer types Algorithms that use iterators to manipulate data in containers Function objects that are used to extend containers and algorithms

STL Containers Can Hold Different Types #include <vector> #include <iostream> using namespace std; int main (int, char *[]) { vector<char *> cstrings; vector<int> numbers; cstrings.push_back(“one”); cstrings.push_back(“two”); numbers.push_back(1); numbers.push_back(2); cout << cstrings[0] << endl << cstrings[1] << endl << numbers[0] << endl << numbers[1] << endl; return 0; // what output is produced, and why? }

STL Iterators Can Act Like Pointers (Somewhat) #include <vector> #include <iostream> using namespace std; int main (int, char *[]) { vector<char *> cstrings; vector<int> numbers; cstrings.push_back(“one”); cstrings.push_back(“two”); numbers.push_back(1); numbers.push_back(2); vector<char *>::iterator cstrings_iter = cstrings.begin(); vector<int>::iterator numbers_iter = numbers.begin(); cout << *cstrings_iter << endl; ++cstrings_iter; cout << *numbers_iter << endl; ++numbers_iter; return 0; // what output is produced, and why? }

STL Algorithms Use Iterators to Manipulate Data #include <vector> #include <iostream> #include <algorithm> #include <iterator> using namespace std; int main (int, char *[]) { vector<char *> cstrings; vector<int> numbers; cstrings.push_back(“one”); cstrings.push_back(“two”); numbers.push_back(1); numbers.push_back(2); copy (cstrings.begin(), cstrings.end(), ostream_iterator<char *>(cout, “\n”)); copy (numbers.begin(), numbers.end(), ostream_iterator<int>(cout, “\n”)); return 0; // what output is produced, and why? }

STL Functors Extend Algorithms (& Containers) #include <vector> #include <iostream> #include <algorithm> #include <iterator> using namespace std; int main (int, char *[]) { vector<char *> cstrings; cstrings.push_back(“one”); cstrings.push_back(“two”); sort (cstrings.begin(), cstrings.end(), greater<char *>()); copy (cstrings.begin(), cstrings.end(), ostream_iterator<char *>(cout, “\n”)); return 0; // what output is produced, and why? }