STL in Action: Helper Algorithms

Similar documents
STL in Action: A Universal Streaming Service

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

CS11 Advanced C++ Spring 2018 Lecture 2

use static size for this buffer

STL: C++ Standard Library

Topics. Modularity and Object-Oriented Programming. Dijkstra s Example (1969) Stepwise Refinement. Modular program development

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

The Inefficiency of C++ Fact or Fiction? Anders Lundgren IAR Systems

QUIZ Friends class Y;

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

CS32 Summer Object-Oriented Programming in C++ Templates and STL. Victor Amelkin September 12, 2013

Exercise 6.2 A generic container class

6.096 Introduction to C++

Topics. bool and string types input/output library functions comments memory allocation templates classes

COP4530 Data Structures, Algorithms and Generic Programming Recitation 4 Date: September 14/18-, 2008

OpenCL C++ kernel language

The Inefficiency of C++ Fact or Fiction?

EL2310 Scientific Programming

CS197c: Programming in C++

Client Code - the code that uses the classes under discussion. Coupling - code in one module depends on code in another module

Chapter 7 Constructors and Other Tools. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

CS11 Advanced C++ Fall Lecture 1

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Lecture Notes on Programming Languages

Modularity. Modular program development. Language support for modularity. Step-wise refinement Interface, specification, and implementation

Contract Programming For C++0x

Part VII. Object-Oriented Programming. Philip Blakely (LSC) C++ Introduction 194 / 370

The Ada Standard Generic Library (SGL)

Item 4: Extensible Templates: Via Inheritance or Traits?

Object-Oriented Programming

class Polynomial { public: Polynomial(const string& N = "no name", const vector<int>& C = vector<int>());... };

Chapter 5. The Standard Template Library.

Object Oriented Programming with c++ Question Bank


QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

C++ Important Questions with Answers

Lecture 15: Even more pointer stuff Virtual function table

Today s lecture. CS 314 fall 01 C++ 1, page 1

How the Adapters and Binders Work

List, Stack, and Queues

Compaq Interview Questions And Answers

What are the characteristics of Object Oriented programming language?

CS11 Advanced C++ Fall Lecture 7

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

Project. C++: Inheritance III. Plan. Project. Before we begin. The final exam. Advanced Topics. Project. This week in the home stretch

Chapter 7. Constructors and Other Tools. Copyright 2016 Pearson, Inc. All rights reserved.

D Programming Language

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

COEN244: Class & function templates

RAJIV GANDHI COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF INFORMATION TECHNOLOGY OBJECT ORIENTED PROGRAMMING QUESTION BANK UNIT I 2 MARKS

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

September 10,

Testing Techniques for Ada 95

These new operators are intended to remove some of the holes in the C type system introduced by the old C-style casts.

Recap: Pointers. int* int& *p &i &*&* ** * * * * IFMP 18, M. Schwerhoff

Implementing Interfaces. Marwan Burelle. July 20, 2012

Oops known as object-oriented programming language system is the main feature of C# which further support the major features of oops including:

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

C++ Alexander Baltatzis

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

Evaluation Issues in Generic Programming with Inheritance and Templates in C++

Question Paper Code : 97044

Polymorphism. Zimmer CSCI 330

G52CPP C++ Programming Lecture 13

CSC 210, Exam Two Section February 1999

SFU CMPT Topic: Function Objects

Generic Programming and First-Class Libraries

Advanced Programming in C++ Container Design I

Chapter 10 :: Data Abstraction and Object Orientation

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

by Pearson Education, Inc. All Rights Reserved. 2

Item 3: Predicates, Part 2: Matters of State

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

Lecturer: William W.Y. Hsu. Programming Languages

CMSC 202 Final May 19, Name: UserID: (Circle your section) Section: 101 Tuesday 11: Thursday 11:30

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

STL Quick Reference for CS 241

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

2 ADT Programming User-defined abstract data types

Object-Oriented Programming

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

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

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

CS93SI Handout 04 Spring 2006 Apr Review Answers

Programming, numerics and optimization

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

6.S096 Lecture 4 Style and Structure

What about Object-Oriented Languages?

void setup(){ void loop() { The above setup works, however the function is limited in the fact it can not be reused easily. To make the code more gene


Lecture on pointers, references, and arrays and vectors

C++ (Non for C Programmer) (BT307) 40 Hours

Overview of C++ Support in TI Compiler Tools July 2008

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

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

Object-Oriented Principles and Practice / C++

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

Transcription:

STL in Action: Helper Algorithms By Graham Glass Abstract This article introduces a new column called STL in Action. Each installment will describe a way to either utilize or extend the C++ Standard Template Library (STL). This article considers several approaches for making the STL algorithms easier to use and less prone to programmer error. Background on STL Most programmers who write C++ programs have to write or purchase a set of collection classes such as vectors, lists, and sets. Until recently, commercial collection classes have had one or more of the following drawbacks. 1. Lack of portability Since there was no standard set of C++ collection classes, versions available from different sources were incompatible. Therefore, it was very hard to decide how to return a collection of results from an object since one user of an object might be using collections from one class library, whereas another user might be using collections from another library. 2. Lack of efficiency Many early C++ collection classes were influenced by Smalltalk. Thus, they made heavy use of inheritance virtual functions. Although these features support very flexible software, they tend to reduce performance somewhat. Although this is not always a problem, the culture of C and C++ is highly driven by performance. Therefore, many programmers will not use libraries unless they perform within a few percentage points of hand-coded C equivalents. 3. Lack of functionality Dealing with the hidden vptr in classes with virtual functions makes it difficult to portably instantiate objects in shared memory, store them persistently, and transfer them across a network. These capabilities are increasingly important for many applications. 4. Lack of extensibility Traditional C++ collection classes place the code for algorithms that worked on the collections within the collections themselves. For example, code for sorting often ended up in a collection classed SortedCollection, and code for applying a function to every element in a collection often ended up in an abstract base class called Collection. This approach made it hard to extend existing classes to add new algorithms without editing and recompiling the vendor's source code, which for maintenance reasons is usually best to avoid. STL in Action: Helper Algorithms 1

5. Lack of type-safety Many traditional collection classes were not type-safe. Use of these collections required heavy use of casting. This goes against one of the important goals of C++: strong type-checking supported by the compiler. Although RTTI is now becoming available to support strong type-checking at run-time, there is a non-trivial performance overhead associated with using RTTI. 6. Lack of flexible memory management Most traditional C++ collection classes had their memory allocation policies woven deeply into the code of the containers. This made it hard (if not impossible) for programmers to allocate space for a collection from shared memory instead of from the local heap. In response to this situation, the ANSI/ISO C++ standards committee decided to search for a standard set of collection classes to overcome these limitations. Alex Stepanov and Meng Lee proposed STL as the standard. STL was based on their successful work on the topic of "generic programming." In July 1994, STL was selected to become an important part of the ANSI/ISO C++ standard library. A number of articles on the design and structure of STL and the draft C++ standard library have appeared in C++ Report. My columns will focus on ways to utilize and extend the Standard Template Library (STL). In this month's column I consider the use of "Helper Algorithms," which make the STL algorithms easier to use and less prone to programmer error. Helper Algorithms Feedback from the C++ community regarding the ease of use of the STL has been very encouraging. After a brief period of adjustment to the new philosophy, users seem to enjoy their new-found power. However, some programmers are concerned that STL algorithms can be accidentally misused. The following describes some problems with using STL and evaluates several solutions. Potential Errors with Using STL Consider the STL count() algorithm: template< class InputIterator, class T, class Size > void count ( InputIterator first, InputIterator last, const T& value, Size& n ); This algorithm counts the number of elements in the range (first, last) that match value using operator== and add this count to n. The count() algorithm makes several assumptions about how it is used. For example, it expects iterators that reference the same container. It also expects that n is not automatically initialized to zero prior to the counting procedure. However, there is nothing to stop a programmer from messing up like this: vector<int> v1 (10); vector<int> v2 (10); //... fill v1 and v2 with values. int n; // Oops, n should be initialized to zero! count (v1.begin (), v2.end (), 42, n); // Oops! STL in Action: Helper Algorithms 2

The Class Wrapper Solution One possible way to help programmers to avoid this is to add a new derived class to each STL container that contains user-friendly versions of the most common algorithms. For example: template < class T > class nonstandard_vector : public vector<t> public: int count (const T& value) const // Call standard. count( begin(), end(), value, n ); //... rest of easier interface goes here... ; This approach would allow you to write: nonstandard_vector<int> v1 (10); // NONSTANDARD VECTOR! int n = v1.count (42); // Easier. An alternative approach is to encapsulate every STL container in a nonstandard and easier-to-use version: template< class T > class nonstandard_vector public: int count (const T& value) const // Call standard. count( v.begin(), v.end(), value, n ); //... rest of easier interface goes here... vector<t>& standard() return v; // Return encapsulated vector. private: vector<t> v; // Encapsulated standard version. ; This approach would also allow you to write: nonstandard_vector<int> v1 (10); // NONSTANDARD VECTOR! int n = v1.count (42); // Easier. Evaluating the Inheritance Solution Although these approaches seem reasonable at first glance, there are several problems: They double the number of classes. They require the user-friendly version of the algorithm to be declared in every extra nonstandard class. They require programs to be littered with nonstandard types, like nonstandard_vector, which in some ways defeats the purpose of having a standard in the first place. STL in Action: Helper Algorithms 3

The first approach reduces efficiency by introducing unnecessary inheritance and virtual destructors. Both approaches only provide user-friendly versions of the algorithm for the nonstandard derived classes, and cannot be extended to provide user-friendly algorithms that work with other vendors' containers. In other words, the class-wrapper approach is contrary to the original aims of the STL design. The Helper Algorithm Solution An alternative helper algorithm solution does not have any of these problems and is in perfect harmony with the STL philosophy. A helper algorithm is a templatized nonmember function that does some of the error-prone work that a programmer would otherwise have to do manually, but is just as efficient as the original. For example, here s the source code of a helper function called ez_count() that makes the standard count() algorithm easier to use: template< class Container, class T > inline int // Inline for efficiency. ez_count (const Container& c, const T& value) // Call standard algorithm. count (c.begin (), c.end (), value, n); Here is an example that uses this helper algorithm: vector<int> v1 (10); int n = ez_count (v1, 42); // Standard vector! // Easiest! Evaluating the Helper Algorithm Solution The significance of helper algorithms becomes more apparent when you consider an operation such as removing items from a container. Without the helper algorithms, the following code is required to remove all of the 42s from the vector v: v.erase( remove( v.begin (), v.end (), 42 ), v.end () ); The ez_erase() helper algorithm allows you to write the following code instead: ez_erase (v, 42); Here is an implementation of the ez_erase() helper algorithm: template< class Container, class T > inline void ez_erase (Container& c, const T& t) // Call standard algorithm. c.erase (remove (c.begin (), c.end (), t), c.end ()); STL in Action: Helper Algorithms 4

Another candidate helper function is ez_release(), which assumes that every element of its container argument is a pointer to some heap and deletes each item: template< class Container > inline void ez_release (Container& c) Container::iterator i; for (i = c.begin (); i!= c.end (); i++) delete *i; Helper versions can be written for many of the STL algorithms. For additional technical information on Recursion Software products and programs or for information on how to order and evaluate Recursion Software technology, contact us today! Copyright 1996-2004 Recursion Software, Inc. All rights reserved. C++ Toolkits is a trademark of Recursion Software, Inc. All other trademarks or service marks are the property of their respective holders. Recursion Software, Inc. 2591 North Dallas Parkway, Suite 200 Frisco, Texas 75034 1.800.727.8674 or 972.731.8800 STL in Action: Helper Algorithms 5