use static size for this buffer

Similar documents
Writing Generic Functions. Lecture 20 Hartmut Kaiser hkaiser/fall_2013/csc1254.html

The Standard Template Library. An introduction

STL: C++ Standard Library

Outline. Variables Automatic type inference. Generic programming. Generic programming. Templates Template compilation

STL Quick Reference for CS 241

Function Templates. Consider the following function:

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms

Tutorial 6. Y. Bernat. Object Oriented Programming 2, Spring Y. Bernat Tutorial 6

CS11 Advanced C++ Spring 2018 Lecture 2

Chapter 19 Vector, templates, and exceptions

Intermediate C++ 1/83

CS197c: Programming in C++

COEN244: Class & function templates

TDDD38 - Advanced programming in C++

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

When we program, we have to deal with errors. Our most basic aim is correctness, but we must

Templates and Vectors

This chapter serves mainly to gather and organize information about iterators. Some new concepts are also introduced for completeness.

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

19. Dynamic Data Structures II

Computer Science II Lecture 2 Strings, Vectors and Recursion

Short Notes of CS201

PHY4321 Summary Notes

Outline. Function calls and results Returning objects by value. return value optimization (RVO) Call by reference or by value?

G52CPP C++ Programming Lecture 18

CS201 - Introduction to Programming Glossary By

Chapter 5. The Standard Template Library.

by Pearson Education, Inc. All Rights Reserved. 2

COMP6771 Advanced C++ Programming

Working with Batches of Data

List, Stack, and Queues

G Programming Languages Spring 2010 Lecture 11. Robert Soulé, New York University

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

Container Notes. Di erent Kinds of Containers. Types Defined by Containers. C++11 Container Notes C++11

CS

Overload Resolution. Ansel Sermersheim & Barbara Geller ACCU / C++ June 2018

C++ 11 and the Standard Library: Containers, Iterators, Algorithms

COMP6771 Advanced C++ Programming

Modern and Lucid C++ for Professional Programmers. Part 7 Standard Containers & Iterators. Department I - C Plus Plus

Cours de C++ Introduction

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

Fast Introduction to Object Oriented Programming and C++

AN OVERVIEW OF C++ 1

Dynamic Data Structures

Introduction to C++ Introduction to C++ 1

Objects Managing a Resource

Generic Programming. Akim D le Étienne Renault Roland Levillain

Overload Resolution. Ansel Sermersheim & Barbara Geller Amsterdam C++ Group March 2019

General Advise: Don t reinvent the wheel

COMP6771 Advanced C++ Programming

September 19,

CSCI-1200 Data Structures Fall 2018 Lecture 22 Hash Tables, part 2 & Priority Queues, part 1

Tutorial 7. Y. Bernat. Object Oriented Programming 2, Spring Y. Bernat Tutorial 7

Simulating Partial Specialization

STL. Zoltán Porkoláb: C++11/14 1

Chapter 15 - C++ As A "Better C"

7 TEMPLATES AND STL. 7.1 Function Templates

Database Systems on Modern CPU Architectures

Polymorphism. Programming in C++ A problem of reuse. Swapping arguments. Session 4 - Genericity, Containers. Code that works for many types.

STL in Action: Helper Algorithms

Introduction to C++ Systems Programming

Object-Oriented Programming for Scientific Computing

Lesson 13 - Vectors Dynamic Data Storage

Lecture 2 Polymorphism, Traits, Policies, and Inheritance

C++11 and Compiler Update

CS11 Advanced C++ Fall Lecture 1

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

Auto - a necessary evil?

Instantiation of Template class

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

CS93SI Handout 04 Spring 2006 Apr Review Answers

CSCI-1200 Data Structures Fall 2009 Lecture 20 Hash Tables, Part II

Templates Templates are functions or classes that are parameterized. We have already seen a few in STL:

Absolute C++ Walter Savitch

Vector and Free Store (Vectors and Arrays)

COMP6771 Advanced C++ Programming

15. Pointers, Algorithms, Iterators and Containers II

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Programmazione. Prof. Marco Bertini

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

The Standard Template Library. EECS 211 Winter 2018

Cpt S 122 Data Structures. Templates

Scientific programming (in C++)

Part XI. Algorithms and Templates. Philip Blakely (LSC) C++ Introduction 314 / 370

Standard Library Reference

Introduction. Programming in C++ Pointers and arrays. Pointers in C and C++ Session 5 - Pointers and Arrays Iterators

Exercise 6.2 A generic container class

We look back... #include <iostream> #include <vector>

Lecture-5. STL Containers & Iterators

2 ADT Programming User-defined abstract data types

Lecture 21 Standard Template Library. A simple, but very limited, view of STL is the generality that using template functions provides.

More on Templates. Shahram Rahatlou. Corso di Programmazione++

Advanced Programming in C++ Container Design I

Object Oriented Paradigm

19.1 The Standard Template Library

Improve your C++! John Richardson. Lab Study Meeting 2013

Sequential Containers. Ali Malik

Rvalue References & Move Semantics

int main(){ int main(){ We want to understand this in depth! std::vector<int> v(10,0); // Vector of length 10

Today. andyoucanalsoconsultchapters6amd7inthetextbook. cis15-fall2007-parsons-lectvii.1 2

Transcription:

Software Design (C++) 4. Templates and standard library (STL) Juha Vihavainen University of Helsinki Overview Introduction to templates (generics) std::vector again templates: specialization by code generation pros and cons of templates STL: containers: basic data structures iterators: to access elements algorithms: processing element sequences 1.12.2014 Juha Vihavainen / University of Helsinki 2 1

Templates But we don t just want a vector of double We want vectors with any element types we specify std::vector <double> std::vector <int > std::vector <Month> // enum class type std::vector <std::vector <Record>> // vector of vectors.. std::vector <char char> //? why not use string.. std::vector <Record* > //? vector of pointers std::vector <std::shared_ptr<record>> // smart pointers We can design our own parameterized types, called templates make the element type a parameter to a template A template is able to take both built-in in types and user-defined types as element types (of course) 1.12.2014 Juha Vihavainen / University of Helsinki 3 Templates for generic programming Code is written in terms of yet unknown types that are to be later specified Also called parametric polymorphism parameterization of types and functions by types - and by integer values, in C++ Reduces duplication of source code Provides flexibility and performance providing good performance is essential: real time and numeric calculations providing flexibility is essential e.g., C++ standard containers 4 2

Templates for generic (cont.) Template definitions and specializations (instantiations) template <typename T, int N> class Buffer {... Buffer () { buff [0] = T ();... } // assumes N >= 1 }; T buff [N]; use static size for this buffer template <typename T, int N> void fill (Buffer <T, N>& buffer) { /*... */ }... // for a class template, specify the template arguments: Buffer <char, 1024> buf; // for buf, T is char and N is 1024 // for a function template, the compiler ( usually) deduces arguments: fill (buf); // here also, T is char and N is 1024: that s what buf has // the same as: fill <char, 1024> (buf); 1.12.2014 Juha Vihavainen / University of Helsinki 5 Parameterize with element type // an almost real vector of T s : template <typename T> // or ( originally) ) "class T " class vector {... produce separate push_back for each void push_back (T const&); instantiation: uses T::T (T const&) to }; place the new item at the end std::vector <double> vd ; // T is double std::vector <int> vi; // T is int std::vector <std::vector <int>> vvi; // T is vector < int> std::vector <char> vc ; // T is char std::vector <double double* > vpd; // T is double* std::vector <std::shared_ptr <E>> spe; // T is shared_ptr <E> 1.12.2014 Juha Vihavainen / University of Helsinki 6 3

Essentially, std::vector is something like: template <typename T > // read for all types T class vector { public: explicit vector (int s) : sz (s), elem (new T [s]), space (s) {... } T & operator [] (int n) { return elem [n]; } // access: return reference int size () const { return sz; } //... etc. vector () : sz (0), elem (nullptr), space (0); // zero-arg. constructor vector (vector const&); // copy ctor vector& operator = (vector const&); // copy assignment ~ vector () { delete [] elem; } // destructor private: int sz ; // the size T * elem; // a pointer to the elements int space; // size + free space }; This original template is analyzed only partially. The use of T is type checked when the template is actually instantiated (and compiled). Essentially, std::vector <double> is something like // a new class is instantiated ( generated) from the template and compiled: class _vector { // the compiler generates from: "vector <double>" public: // uses some internal name for vector<double> explicit _vector (int s) : sz (s), elem (new double [s]), space (s) {... } double& operator [] (int n) {... } // access element int size () const { return sz; } //... etc. _ vector () : sz (0), elem (nullptr), space (0) {} // zero-arg. ctor _ vector (_vector const&); // copy ctor _ vector& & operator = (_vector const&); // copy assignment ~ _vector () { delete [] elem; } // destructor private: int sz ; // the size double * elem; // a pointer to the elements int space; // size + free space }; Member functions are instantiated only if called 1.12.2014 Juha Vihavainen / University of Helsinki 8 4

Templates: no free lunch Template instantiation generates custom (type-specialized) code => efficient but may involve memory overhead (replicated binary) however, only those templates used/called are actually instantiated Sometimes poor diagnostics (obscure messages) -- at least historically Delayed error messages: only when "source" actually gets generated.. Used templates must be fully defined in each separate translation unit need the template source code to be specialized by the instantiation so (usually) must place template definitions in header files the new extern template (C++11) feature suppresses multiple implicit extra instantiations of templates: a way of avoiding significant redundant work by the compiler and linker Usually: no problems using available template-based libraries such as the C++ standard library: e.g.,., std::vector, std::sort() initially, should probably only write simple templates yourself.. 1.12.2014 Juha Vihavainen / University of Helsinki 9 STL background the STL was developed by Alex Stepanov, originally implemented for Ada (80's - 90's) in 1997, STL was accepted by the C++ Standards Committee as part of the standard C++ adopting STL strongly affected various language features of C++, especially those features offered by templates supports basic data types such as vectors, lists,, associative maps, sets, and algorithms such as sorting efficient and compatible with C computation model not object-oriented: oriented: uses value-copy semantics (copy ctor, assign) many operations (called "algorithms") are defined as stand-alonealone functions uses templates for reusability provides exception safety for all operations (on some level) 10 5

STL examples std::vector <std::string> v;.. // some code to initialize v v.push_back ("123");");.. // can grow dynamically if (! v.empty ()) std::cout << v.size () << std::endl d ; std::vector <std::string> v1 = v; // make a new copy of v ( copy ctor) std::list <std::string> list (v.begin (), v.end ()); // makes a list copy of v using iterators std::list <std::string> list1;.. std::swap (list, list1); // swap two lists ( efficiently).. // actually calls: " list.swap (list1) typedef std::shared_ptr <std:: ::vector <int>> VectPtr; VectPtr f (std::vector<int> v) { // copy constructs local variable!.. v [7] = 11;.. return VectPtr (new std::vector<int> (v)); } 11 Basic principles of STL STL containers are type-parameterized parameterized templates, rather than classes with inheritance and dynamic binding e.g., no common base class for all of the containers no virtual functions and late binding used however, containers implement a (somewhat) uniform service interface with similarly named operations (insert, erase, size..) the standard std::string was defined first but later extended to cover STL-like services (e.g.,., to provide iterators) STL collections do not directly support I/O operations istream_iterator <T> and ostream_iterator <T> can represent IO streams as STL compatible iterators so IO can be achieved using STL algorithms (std::copy,, etc.) 12 6

Components of STL 1. Containers,, for holding (homogeneous) collections of values: a container itself manages (owns) its elements and their memory 2. Iterators are syntactically and semantically similar to C-like pointers; different containers provide different iterators but with a similar pointer-like interface 3. Algorithms are functions that operate on containers via iterators; iterators are given as (generic) parameters; the algorithm and the container must support compatible iterators (using implicit generic constraints) In addition, STL provides, for example functors: : objects to be "called" as if they were functions ("(...)") various adapters,, for adapting components to provide a different public interface (std::stack, std::queue) 13 #include <iostream> #include <vector> #include <algorithm> // std:: ::cin, std::cout, std::cerr // std::vector // std::reverse, std::sort.. sort.. int main () { std::vector <double> v; // buffer for input data double d; while (std::cin >> d) v.push_back (d); // read elements until EOF if (! std::cin.eof ()) { // check how input failed std::cerr << "Input error\n"; return 1; } std:: ::reverse (v.begin (), v.end ()); std::cout << "elements in reverse order:\n"; for (const auto x : v) std::cout << x << '\n'; } loop local that cannot be modified 14 7

STL algorithms STL algorithms are implemented for efficiency, having an associated time complexity (constant, linear, logarithmic) They are defined as function templates, parameterized by iterators to access the containers they operate on: the same template std::vector <int> v;... // initialize v std::sort (v.begin (), v.end ()); // instantiates sort std::deque <double> d; // double-endedended queue... // initialize d std::sort (d.begin (), d.end ()); // instantiate, again If a general algorithm, such as sorting, is not available for a specific container (since iterators may not be compatible), it is provided as a member operation (e.g., for std::list) 15 Introduction to STL containers A container holds a homogeneous collection of values Container <T> c;... // initially empty c.push_back (value); // can grow dynamically When you insert an element into a container, you always insert a value copy of a given object the element type T must provide copying of values Heterogeneous (polymorphic) collections are represented as containers storing pointers to a base class brings out all memory management problems (C pointers) can use std::shared_ptr (with reference counting) can use std::unique_ptr (with its single-owner semantics) Containers support constant-timetime swaps (usually) 16 8

Intr. to STL containers (cont.) in sequence containers,, each element is placed in a certain relative position: as first, second, etc.: std::vector <T> std::deque <T> std::list <T> std::forward_list <T> vectors, sequences of varying length deques (with operations at either end) doubly-linked linked lists singly-linked linked lists associative containers are used to represent sorted collections std::map <KeyType, ValueType> (ordered search tree) std::unordered_map <KeyType, ValueType> (hash map) for a map,, provide operator < for the key type for a hash map, provide std::hash<key> for the key type also sets s and multi-key/value versions 17 Intr. to STL containers (cont.) Standard containers are somewhat interchangeable - in principle, you can choose the one that is the most efficient for your needs however, interfaces and services are not exactly identical changing a container may well involve changes to the client source code (that calls the services of a container) Different kinds of algorithms require different kinds of iterators once you choose a container, you can apply only those algorithms that accept a compatible iterator Container adapters are used to adapt containers for the use of specific interfaces (e.g., push (... ), pop (), etc.) for example, std::stack and std::queue are adapters of sequences; the actual container (deque)) is a protected member 18 9

Iterators (again) an iterator provides access to elements in a container; every iterator it has to support (at least) * it it- > ++it it == it1 it!= it1 to access the current element or its member to move to the next element "pointer" equality "pointer" inequality container classes provide iterators in a uniform way as standardized typedef names within the class definition std::vector<std::string>::iterator // is a typedef std::vector<std::string>::const_iterator begin () points to the first element (if any) end () points beyond the last (end marker) const_iterators s are required for const - qualified containers 19 Iterators (cont.) C::iterator first = c.begin (), last = c.end (); a container holds a set of values, of type C::value_type (typedef) an iterator points to an element of this container, or just beyond the last proper element (a special past-the-end end value) it can be dereferenced by using the operator * (e.g., "* it"), and the operator -> (e.g., "it-> op ()") 20 10

Iterators (cont.) Iterators are syntactically compatible with C pointers Container c;... Container::iterator it; // a shorter form: for (it = c.begin (); it!= c.end (); ++it) { // for (auto& x : c)..... it-> op ();... std::cout <<* it;... } to allow modifications to the container Could use a range-for statement, or an algorithm: for_each, copy Non-const iterators support overwrite semantics: can modify or overwrite the elements already stored in the container Generic algorithms are not written for a particular container class in STL but use iterators instead There are iterator adapters that support insertion semantics (i.e., while writing through an iterator, inserts a new element at that point) 21 Using iterators within function templates template <typename It, typename T> // a sample function template bool contains (It first, It beyond, T const& value) { while (first!= beyond &&* first!= value) ++first first; // note implicit constraints on It first and T value return first!= beyond; } Why not < comparison? // can operate on any primitive array: int a [100];... // initialize elements of a bool b = contains (a, a+100, 42); // can operate on any STL sequence: std::vector <std::string> v;... the same source code // initialize v b = contains (v.begin (), v.end (), "42"); 22 11

Iterators: summary Validity of iterators is not guaranteed (as usual in C/C++) especially, modifying the organization of a container may invalidate any existing iterators and references (this depends on the kind of container and modification) For array-like structures, iterators are (usually) native C-style pointers to elements of the array (e.g., std::vector) efficient: uses direct addresses and pointer arithmetics may have the same security problems as other native pointers some libraries may provide optional special checked iterators For other containers (e.g., std::list), iterators are provided as abstractions defined as classes with properly overloaded operators ++, *, - >, etc. but traverse links between nodes instead of address calculations 23 Summary C++ containers are based on generic templates Templates provide compile-time polymorphism via type parametrization STL templates don't use such object-orientedoriented features as inheritance or late binding of methods Templates are instantiated, and these instantiations are then compiled (in a selected manner) STL provides containers, iterators,, and algorithms 24 12