Lab4 problems More about templates Some STL

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Advertisements

CMSC 202 Lesson 24 Iterators and STL Containers. Warmup Write the class definition for the templated Bag class – A bag has: Random insertion Random removal.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Stacks, Queues, and Deques
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
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.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Lecture 7 : Intro. to STL (Standard Template Library)
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
Template Lecture 11 Course Name: High Level Programming Language Year : 2010.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
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.
Operator Overloading Introduction
Programming with ANSI C ++
Pointers and Linked Lists
Static data members Constructors and Destructors
How to be generic Lecture 10
CS 215 Final Review Ismail abumuhfouz Fall 2014.
C++ Templates.
Templates.
Motivation and Overview
Chapter 14 Templates C++ How to Program, 8/e
Introduction to Custom Templates
Standard Template Library (STL)
C++ Plus Data Structures
Cinda Heeren / Geoffrey Tien
STACKS AND QUEUES UNIT 2 DS THROUGH C++.
CS212: Object Oriented Analysis and Design
Templates.
Tuesday, February 20, 2018 Announcements… For Today… 4+ For Next Time…
Stacks, Queues, and Deques
ADT Implementations: Templates and Standard Containers
Templates and Standard Containers
Generic Positional Containers and Double-Ended Queues
Stacks, Queues, and Deques
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Abstraction: Generic Programming, pt. 2
Doubly Linked List Implementation
Tonga Institute of Higher Education
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Lists - I The List ADT.
Lists - I The List ADT.
Iterators and STL Containers
Containers: Queue and List
Overview of C++ Polymorphism
Lists.
Java Programming Language
C++ STL Stack, Queue, and Deque
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Templates Generic Programming.
Stacks, Queues, and Deques
Function Templates Class Templates
Doubly Linked List Implementation
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 3 Lists, Stacks, and Queues
Pointers, Dynamic Data, and Reference Types
Abstract Data Types Stacks CSCI 240
SPL – PS1 Introduction to C++.
Presentation transcript:

Lab4 problems More about templates Some STL Lecture 10 Lab4 problems More about templates Some STL

Lab4 Template (size, type) vs. Input file Friend Function, increment, decrement Operator Overloading http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B (see the rules there) Ostream operator << friend ostream& operator<<(ostream &os,const matrix &s); (why friend here? )

Dependent vs. Non-dependent Dependent names are names whose definitions are considered to depend upon the template parameters and for which there is no declaration within the template definition. They are resolved only when the template is instantiated. Those that are intended to refer to types or templates may require disambiguation. Non-dependent names are those names that are considered not to depend upon the template parameters, plus the name of the template itself and names declared within it (members, friends and local variables). They are resolved when the template is defined, in the normal way, and do not require disambiguation. Dependent names are names whose definitions are considered to depend upon the template parameters and for which there is no declaration within the template definition. They are resolved only when the template is instantiated.

Indicating Typename In a template, the name of a member of another class that depends on its template parameter(s) (first<T>::pointer in this example, dependent on the T parameter) is a dependent name that is not looked-up immediately. To tell the compiler that it is meant to refer to a type and not some other sort of member, you must add the keyword typename before it. template<typename T> struct first { typedef T * pointer; }; class second { first<T>::pointer p; // syntax error Dependent names are names whose definitions are considered to depend upon the template parameters and for which there is no declaration within the template definition. They are resolved only when the template is instantiated.

Indicating Typename A name used in a template declaration or definition and that is dependent on a template-parameter is assumed not to name a type unless the applicable name lookup finds a type name or the name is qualified by the keyword typename. In short, if the compiler can't tell if a dependent name is a value or a type, then it will assume that it is a value. template <typename T> void foo(const T& t) { // declare a pointer to an object of type T::bar T::bar * p; } struct S { typedef int bar; }; int main() { S x; foo(x);

Indicating Base Classes Base_func is inherited. However, the standard says that unqualified names in a template are generally non-dependent and must be looked up when the template is defined. Since the definition of a dependent base class is not known at that time (there may be specialisations of the base class template that have not yet been seen), unqualified names are never resolved to members of the dependent base class. template<typename T> class base { public: void base_func(); }; class derived : public base<T> { public: void derived_func() { base_func(); // error: base_func not defined } Where names in the template are supposed to refer to base class members or to indirect base classes, they can either be made dependent by qualifying them or brought into the template's scope with a using-declaration. In the example, this could be achieved by replacing the call to base_func() with this->base_func() or base<T>::base_func(), or by adding the declaration using base<T>::base_func;.

Specialisation Member Function According to the standard, you can declare a full specialisation of a member function of a class template. It’s possible to specialise a member function of a class template without specialising the whole template. template<typename T> class my_class { public: bool func(); // other functions }; template<> bool my_class<int>::func(); Unfortunately not all compilers support this.

STL – Pair in Map int min( int a, int b ) { return a < b ? a : b; the STL defines a class pair that simply stores 2 things. pair has 2 member variables (or fields): first is of type A and second is of type B. To create a pair, you need to manually specify what A and B are, like this. pair< string, double > p( "pi", 3.14 ); //make_pair(“pi”, 3.14); p.second = 3.14159; The first line calls the constructor. To use pair, you need to #include <map>. (map: like Java‘s TreeMap - basically, a set of key-value pairs, sorted by the key.) int min( int a, int b ) { return a < b ? a : b; } (templates, chars, doubles, strings, and anything else that has the '<' operator defined on it. ) template< class C > C min( C a, C b ) { template< class A, class B > class pair { public: A first; B second; pair( A a, B b ) { first = a; second = b; } };

STL - Stacks, Queues, and Deques Stacks, queues, and deques (for double-ended queues) are simple containers that allows O(1) insertion and deletion at the beginning and/or end. You cannot modify any elements inside the container, or insert/remove elements other than at the ends. However, because they are templatized, they can hold just about any data types, and are tremendously useful for many purposes. A queue is a First-In-First-Out (FIFO) container, whereas a stack is Last-In-First-Out (LIFO). A deque is both a stack and a queue. A stack has pretty much all the methods of a queue, including push(), pop(), size(), empty(). However, instead of front(), a stack accesses the top of the stack with top(). And of course, pop() will retrieve the element at the top (or end) of the stack, not the front. Finally, a deque has both the features of a stack and a queue. It has the member methods size() and empty(), but instead of the push(), pop() combination, it now provides 4 different methods, all pretty much self-explanatory: push_front(), push_back(), pop_front(), pop_back().

STL - Stacks, Queues, and Deques #include <stack> #include <queue> // either one will provide deque queue< int > Q; // Construct an empty queue for( int i = 0; i < 3; i++ ) { Q.push( i ); // Pushes i to the end of the queue // Q is now { “0”, “1”, “2” } } int sz = Q.size(); // Size of queue is 3 while( !Q.empty() ) { // Print until Q is empty int element = Q.front(); // Retrieve the front of the queue Q.pop(); // REMEMBER to remove the element! cout << element << endl; // Prints queue line by line