The C++ Standard Template Library
|
|
- Edith Phelps
- 5 years ago
- Views:
Transcription
1 The C++ Standard Template Library CS 342: Object-Oriented Software Development Lab The C++ Standard Template Library David L. Levine Christopher D. Gill Department of Computer Science Washington University, St. Louis What is the STL? Generic programming: why use the STL? STL overview: helper class and function templates, containers, iterators, generic algorithms, function objects, adaptors STL examples Conclusions: writing less, doing more References for more information on the STL Copyright c Dept. of Computer Science, Washington University 1 What is the STL? What is the STL (cont d)? The Standard Template Library provides a set of well structured generic C++ components that work together in a seamless way. Alexander Stepanov & Meng Lee, The Standard Template Library A collection of composable class and function templates Helper class and function templates: operators, pair Container and iterator class templates Generic algorithms that operate over iterators Function objects Adaptors Enables generic programming in C++ Each generic algorithm can operate over any iterator for which the necessary operations are provided Extensibile: can support new algorithms, containers, iterators Copyright c Dept. of Computer Science, Washington University 2 Copyright c Dept. of Computer Science, Washington University 3
2 Generic Programming: why use the STL? STL Overview: helper operators Reuse: write less, do more The STL hides complex, tedious and error prone details The programmer can then focus on the problem at hand Type-safe plug compatibility between STL components Flexibility Iterators decouple algorithms from containers Unanticipated combinations easily supported Efficiency Templates avoid virtual function overhead Strict attention to time complexity of algorithms operator!= (const T& t, const U& u) return!(t == u); operator > (const T& t, const U& u) return u < t; Copyright c Dept. of Computer Science, Washington University 4 Copyright c Dept. of Computer Science, Washington University 5 STL Overview: helper operators (cont d) operator <= (const T& t, const U& u) return!(u < t); operator >= (const T& t, const U& u) return!(t < u); STL Overview: helper operators (cont d) Question: why require that parameterized types support operator == as well as operator é? Operators é and é= and é= are implemented only in terms of operator é on u and t (and! on boolean results) Could implement operator == as!(t < u) &&!(u < t) so classes T and U only had to provide operator é and did not have to provide operator == Answer: efficiency (two operator é calls are needed to implement operator == implicitly) Copyright c Dept. of Computer Science, Washington University 6 Copyright c Dept. of Computer Science, Washington University 7
3 STL Overview: operators example STL Overview: pair helper class class String public: String (const char *s) : s_ (s) String (const String &s) : s_ (s.s_) bool operator < (const String &s) const return (strcmp (this->s_, s.s_) < 0)? true : false; bool operator == (const String &s) const return (strcmp (this->s_, s.s_) == 0)? true : false; const char * s_; ; #include <iostream.h> int main (int, char *[]) const char * wp = "world"; const char * hp = "hello"; String w_str (wp); String h_str (hp); cout << false << endl; // 0 cout << true << endl; // 1 cout << (h_str < w_str) << endl; cout << (h_str == w_str) << endl; cout << (hp < wp) << endl; cout << (hp == wp) << endl; struct pair // Data members T first; U second; // Default constructor pair () // Constructor from values pair (const T& t, const U& u) : first (t), second (u) ; Copyright c Dept. of Computer Science, Washington University 8 Copyright c Dept. of Computer Science, Washington University 9 STL Overview: pair helper class (cont d) STL Overview: pair helper class (cont d) // Pair equivalence comparison operator operator == (const pair<t, U>& lhs, const pair<t, U>& rhs) return lhs.first == rhs.first && lhs.second == rhs.second; // Pair less than comparison operator operator < (const pair<t, U>& lhs, const pair<t, U>& rhs) return lhs.first < rhs.first (!(rhs.first < lhs.first) && lhs.second < rhs.second); Copyright c Dept. of Computer Science, Washington University 10 Copyright c Dept. of Computer Science, Washington University 11
4 STL Overview: pair example STL Overview: containers, iterators, algorithms class String public: String (const char *s) : s_ (s) String (const String &s) : s_ (s.s_) bool operator < (const String &s) const return (strcmp (this->s_, s.s_) < 0)? true : false; bool operator == (const String &s) const return (strcmp (this->s_, s.s_) == 0)? true : false; const char * s_; ; #include <iostream.h> #include <pair.h> int main (int, char *[]) pair<int, String> pair1 (3, String("hello")); pair<int, String> pair2 (2, String("world")); cout << (pair1 == pair2) << endl; cout << (pair1 < pair2) << endl; Containers: Sequence: vector, deque, list Associative: set, multiset, map, multimap Iterators: Input, output, forward, bidirectional, random access Each container declares a trait for the type of iterator it provides Generic Algorithms: Sequence (mutating and non-mutating), sorting, numeric Copyright c Dept. of Computer Science, Washington University 12 Copyright c Dept. of Computer Science, Washington University 13 STL Overview: containers STL Overview: sequence containers STL containers are Abstract Data Types (ADTs) All containers are parameterized by the type(s) they contain Sequence containers are ordered Associative containers are unordered Each container declares an iterator typedef (trait) Each container provides special factory methods for iterators A vector is similar to our Array and Stack class templates provides reallocation, indexed storage, push back, pop back A deque (pronounced deck ) is a double ended queue adds efficient insertion and removal at the beginning as well as at the end of the sequence A list has constant time insertion and deletion at any point in the sequence (not just at the beginning and end) performance trade-off: does not offer a random access iterator Copyright c Dept. of Computer Science, Washington University 14 Copyright c Dept. of Computer Science, Washington University 15
5 STL Overview: associative containers STL Overview: container example A set is an unordered collection of unique keys e.g., a set of student id numbers A map associates a value with each unique key e.g., a student s first name A multiset or a multimap can support multiple equivalent (nonunique) keys e.g., student last names Uniqueness is determined by an equivalence relation e.g., strncmp might treat last names that are distinguishable by strcmp as being the same #include <iostream.h> #include <vector.h> #include "String.h" int main (int argc, char *argv[]) int i; vector <String> projects; // Names of the projects for (i = 1; i < argc; ++i) // Start with 1st arg projects.push_back (String (argv [i])); cout << projects [i-1].s_ << endl; Copyright c Dept. of Computer Science, Washington University 16 Copyright c Dept. of Computer Science, Washington University 17 STL Overview: iterators STL Overview: iterators (cont d) Iterator categories depend on type parameterization rather than on inheritance: allows algorithms to operate seamlessly on both native (i.e., pointers) and user-defined iterator types Iterator categories are hierarchical, with more refined categories adding constraints to more general ones Forward iterators are both input and output iterators, but not all input or output iterators are forward iterators Bidirectional iterators are all forward iterators, but not all forward iterators are bidirectional iterators All random access iterators are bidirectional iterators, but not all bidirectional iterators are random access iterators Input iterators are used to read values from a sequence. An input iterator must allow the following operations Copy ctor and assignment operator for that same iterator type Operators == and!= for comparison with iterators of that type Operators * (can be const) and ++ (both prefix and postfix) Note that native types that meet the requirements (i.e., pointers) can be used as iterators of various kinds Copyright c Dept. of Computer Science, Washington University 18 Copyright c Dept. of Computer Science, Washington University 19
6 STL Overview: iterators (cont d) STL Overview: iterators (cont d) Output iterators differ from input operators as follows: Operators = and == and!= need not be defined (but could be) Must support non-const operator * (e.g., *iter = 3) Forward iterators must implement (roughly) the union of requirements for input and output iterators, plus a default ctor Bidirectional iterators must implement the requirements for forward iterators, plus decrement operators (prefix and postfix) Random access iterators must implement the requirements for bidirectional iterators, plus: Arithmetic assignment operators += and -= Operators + and - (must handle symmetry of arguments) Ordering operators é and é and é= and é= Subscript operator [ ] Copyright c Dept. of Computer Science, Washington University 20 Copyright c Dept. of Computer Science, Washington University 21 STL Overview: iterator example STL Overview: generic algorithms #include <iostream.h> #include <vector.h> #include "String.h" int main (int argc, char *argv[]) vector <String> projects; // Names of the projects for (int i = 1; i < argc; ++i) projects.push_back (String (argv [i])); for (vector<string>::iterator j = projects.begin (); j!= projects.end (); ++j) cout << (*j).s_ << endl; Algorithms operate over iterators rather than containers Each container declares an iterator as a trait vector and deque declare random access iterators list, map, set, multimap, and multiset declare bidirectional iterators Each container declares factory methods for its iterator type: begin ( ), end ( ), rbegin ( ), rend ( ) Composing an algorithm with a container is done simply by invoking the algorithm with iterators for that container Templates provide compile-time type safety for combinations of containers, iterators, and algorithms Copyright c Dept. of Computer Science, Washington University 22 Copyright c Dept. of Computer Science, Washington University 23
7 STL Overview: generic algorithms (cont d) Some examples of STL generic algorithms: find: returns a forward iterator positioned at the first element in the given sequence range that matches a passed value mismatch: returns a pair of iterators positioned respectively at the first elements that do not match in two given sequence ranges copy: copies elements from a sequence range into an output iterator replace: replaces all instances of a given existing value with a given new value, within a given sequence range random shuffle: shuffles the elements in the given sequence range STL Overview: generic algorithm example #include <vector.h> #include <algo.h> #include <assert.h> #include "String.h" int main (int argc, char *argv[]) vector <String> projects; for (int i = 1; i < argc; ++i) projects.push_back (String (argv [i])); vector<string>::iterator j = find (projects.begin (), projects.end (), String ("Lab8")); if (j == projects.end ()) return 1; assert ((*j) == String ("Lab8")); Copyright c Dept. of Computer Science, Washington University 24 Copyright c Dept. of Computer Science, Washington University 25 STL Overview: function objects STL Overview: function objects example Function objects (aka functors) declare and define operator ( ) STL provides helper base class templates unary function and binary function to facilitate writing user-defined function objects STL provides a number of common-use function object class templates: arithmetic: plus, minus, times, divides, molulus, negate comparison: equal to, not equal to, greater, less, greater equal, less equal logical: logical and, logical or, logical not A number of STL generic algorithms can take STL-provided or userdefined function object arguments to extend algorithm behavior #include <vector.h> #include <algo.h> #include <function.h> #include "String.h" int main (int argc, char *argv[]) vector <String> projects; for (int i = 0; i < argc; ++i) projects.push_back (String (argv [i])); // Sort in descending order: note explicit ctor for greater sort (projects.begin (), projects.end (), greater<string> ()); Copyright c Dept. of Computer Science, Washington University 26 Copyright c Dept. of Computer Science, Washington University 27
8 STL Overview: adaptors STL Example: course schedule STL adaptors implement the Adapter design pattern i.e., they convert one interface into another interface clients expect Container adaptors include Stack, Queue, Priority Queue Iterator adaptors include reverse and insert iterators Function adaptors include negators and binders STL adaptors can be used to narrow interfaces (e.g., a Stack adaptor for vector) Goals: Read in a list of course names, along with the corresponding day(s) of the week and time(s) each course meets Days of the week are read in as characters M,T,W,R,F,S,U Times are read as unsigned decimal integers in 24 hour HHMM format, with no leading zeroes (e.g., 11:59pm should be read in as 2359, and midnight should be read in as 0) Sort the list according to day of the week and then time of day Detect any times of overlap between courses and print them out Print out an ordered schedule for the week STL provides most of the code for the above Copyright c Dept. of Computer Science, Washington University 28 Copyright c Dept. of Computer Science, Washington University 29 // Meeting.h #include <iostream.h> struct Meeting enum Day_Of_Week MO, TU, WE, TH, FR, SA, SU; static Day_Of_Week day_of_week (char c); Meeting (const char * title, Day_Of_Week day, unsigned int start_time, unsigned int finish_time); Meeting (const Meeting & m); Meeting & operator = (const Meeting & m); bool operator < (const Meeting & m) const; bool operator == (const Meeting & m) const; // Meeting.h, continued... const char * title_; // Title of the meeting Day_Of_Week day_; // Week day of meeting unsigned int start_time_; // Meeting start time in HHMM format unsigned int finish_time_; // Meeting finish time in HHMM format ; // Helper operator for output ostream & operator << (ostream &os, const Meeting & m); // Meeting.cc #include <assert.h> #include "Meeting.h" Meeting::Day_Of_Week Meeting::day_of_week (char c) switch (c) case M : return Meeting::MO; case T : return Meeting::TU; case W : return Meeting::WE; case R : return Meeting::TH; case F : return Meeting::FR; case S : return Meeting::SA; case U : return Meeting::SU; default: assert (!"not a week day"); return Meeting::MO; // Meeting.cc, continued... Meeting::Meeting (const char * title, Day_Of_Week day, unsigned int start_time, unsigned int finish_time) : title_ (title), day_ (day), start_time_ (start_time), finish_time_ (finish_time) Meeting::Meeting (const Meeting & m) : title_ (m.title_), day_ (m.day_), start_time_ (m.start_time_), finish_time_ (m.finish_time_) Copyright c Dept. of Computer Science, Washington University 30 Copyright c Dept. of Computer Science, Washington University 31
9 // Meeting.cc, continued... Meeting & Meeting::operator = (const Meeting & m) this->title_ = m.title_; this->day_ = m.day_; this->start_time_ = m.start_time_; this->finish_time_ = m.finish_time_; return *this; bool Meeting::operator == (const Meeting & m) const return (this->day_ == m.day_ && ((this->start_time_ < m.start_time_ && m.start_time_ < this->finish_time_) (m.start_time_ < this->start_time_ && this->start_time_ < m.finish_time_)))? true : false; // Meeting.cc, continued... bool Meeting::operator < (const Meeting & m) const return (day_ < m.day_ (day_ == m.day_ && start_time_ < m.start_time_) (day_ == m.day_ && start_time_ == m.start_time_ && finish_time_ < m.finish_time_))? true : false; // Meeting.cc, continued... ostream & operator << (ostream &os, const Meeting & m) const char * dow = " "; switch (m.day_) case Meeting::MO: dow="m "; break; case Meeting::TU: dow="t "; break; case Meeting::WE: dow="w "; break; case Meeting::TH: dow="r "; break; case Meeting::FR: dow="f "; break; case Meeting::SA: dow="s "; break; case Meeting::SU: dow="u "; break; return os << m.title_ << " " << dow << m.start_time_ << " " << m.finish_time_ << endl; #include <stdlib.h> // Main.cc #include <vector.h> #include <assert.h> #include <algo.h> #include <iterator.h> #include "Meeting.h" int parse_args (int argc, char * argv[], vector<meeting>& schedule) for (int i = 1; i < argc; i+=4) schedule.push_back (Meeting (argv [i], Meeting::day_of_week (*argv [i+1]), static_cast<unsigned int> (atoi (argv [i+2])), static_cast<unsigned int> (atoi (argv [i+3])))); Copyright c Dept. of Computer Science, Washington University 32 Copyright c Dept. of Computer Science, Washington University 33 // Main.cc, continued... int print_schedule (vector<meeting> &schedule) // Find and print out any conflicts for (vector<meeting>::iterator j = schedule.begin (); j!= schedule.end (); ++j) j = adjacent_find (j, schedule.end ()); if (j == schedule.end ()) break; // Main.cc, continued... // Print out schedule, using // STL output stream iterator ostream_iterator<meeting> out_iter (cout, "\n"); copy (schedule.begin (), schedule.end (), out_iter); // Main.cc, continued... int main (int argc, char *argv[]) vector<meeting> schedule; if (parse_args (argc, argv, schedule) < 0) return -1; sort (schedule.begin (), schedule.end ()); cout << "CONFLICT:" << endl << " " << *j << endl << " " << *(j+1) << endl << endl; if (print_schedule (schedule) < 0) return -1; Copyright c Dept. of Computer Science, Washington University 34 Copyright c Dept. of Computer Science, Washington University 35
10 Conclusions STL> cat infile CS313A W CS342S T CS342S T CS342S R CS422S T CS422S R CS423S M CS423S W CS431S T CS431S R STL> cat infile xargs main CONFLICT: CS342S T CS422S T CS423S M CS342S T CS342S T CS422S T CS431S T CS423S W CS313A W CS342S R CS422S R CS431S R STL promotes software reuse: writing less, doing more Effort in schedule example focused on the Meeting class STL provided sorting, copying, containers, iterators STL is flexible, according to open/closed principle Used copy algorithm with output iterator to print schedule Can sort in ascending (default) or descending (via function object) order. STL is efficient STL inlines methods wherever possible, uses templates extensively Optimized both for performance and for programming model complexity (e.g., é requiring == and and no others) Copyright c Dept. of Computer Science, Washington University 36 Copyright c Dept. of Computer Science, Washington University 37 References: for more information on the STL David Musser s STL page musser/stl.html Stepanov and Lee, The Standard Template Library musser/doc.ps SGI STL Programmer s Guide Musser and Saini, STL Tutorial and Reference Guide ISBN Copyright c Dept. of Computer Science, Washington University 38
Chapter 5. The Standard Template Library.
Object-oriented programming B, Lecture 11e. 1 Chapter 5. The Standard Template Library. 5.1. Overview of main STL components. The Standard Template Library (STL) has been developed by Alexander Stepanov,
More informationCS11 Advanced C++ Fall Lecture 1
CS11 Advanced C++ Fall 2006-2007 Lecture 1 Welcome! ~8 lectures Slides are posted on CS11 website http://www.cs.caltech.edu/courses/cs11 ~6 lab assignments More involved labs 2-3 week project at end CS
More informationProgramming in C++ using STL. Rex Jaeschke
Programming in C++ using STL Rex Jaeschke Programming in C++ using STL 1997, 1999, 2002, 2007, 2009 Rex Jaeschke. All rights reserved. Edition: 2.0 All rights reserved. No part of this publication may
More informationCOEN244: Class & function templates
COEN244: Class & function templates Aishy Amer Electrical & Computer Engineering Templates Function Templates Class Templates Outline Templates and inheritance Introduction to C++ Standard Template Library
More informationShort Notes of CS201
#includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system
More informationThe Ada Standard Generic Library (SGL)
The Ada Standard Generic Library (SGL) Alexander V. Konstantinou Computer Science Graduate Seminar 4/24/96 1 Presentation Overview Introduction (S{T G}L) The C++ Standard Template Library (STL) Ada 95
More informationCS201 - Introduction to Programming Glossary By
CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with
More informationUnit 4 Basic Collections
Unit 4 Basic Collections General Concepts Templates Exceptions Iterators Collection (or Container) Classes Vectors (or Arrays) Sets Lists Maps or Tables C++ Standard Template Library (STL Overview A program
More informationC++ Basics. Brian A. Malloy. References Data Expressions Control Structures Functions. Slide 1 of 24. Go Back. Full Screen. Quit.
C++ Basics January 19, 2012 Brian A. Malloy Slide 1 of 24 1. Many find Deitel quintessentially readable; most find Stroustrup inscrutable and overbearing: Slide 2 of 24 1.1. Meyers Texts Two excellent
More informationOBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe
OBJECT ORIENTED PROGRAMMING USING C++ CSCI 5448- Object Oriented Analysis and Design By Manali Torpe Fundamentals of OOP Class Object Encapsulation Abstraction Inheritance Polymorphism Reusability C++
More informationChapter 15 - C++ As A "Better C"
Chapter 15 - C++ As A "Better C" Outline 15.1 Introduction 15.2 C++ 15.3 A Simple Program: Adding Two Integers 15.4 C++ Standard Library 15.5 Header Files 15.6 Inline Functions 15.7 References and Reference
More informationThe Standard Template Library. An introduction
1 The Standard Template Library An introduction 2 Standard Template Library (STL) Objective: Reuse common code Common constructs: Generic containers and algorithms STL Part of the C++ Standard Library
More information7 TEMPLATES AND STL. 7.1 Function Templates
7 templates and STL:: Function Templates 7 TEMPLATES AND STL 7.1 Function Templates Support generic programming functions have parameterized types (can have other parameters as well) functions are instantiated
More information2 ADT Programming User-defined abstract data types
Preview 2 ADT Programming User-defined abstract data types user-defined data types in C++: classes constructors and destructors const accessor functions, and inline functions special initialization construct
More informationby Pearson Education, Inc. All Rights Reserved. 2
An important part of every container is the type of iterator it supports. This determines which algorithms can be applied to the container. A vector supports random-access iterators i.e., all iterator
More informationToday. andyoucanalsoconsultchapters6amd7inthetextbook. cis15-fall2007-parsons-lectvii.1 2
TEMPLATES Today This lecture looks at techniques for generic programming: Generic pointers Templates The standard template library Thebestreferenceis: http://www.cppreference.com/index.html andyoucanalsoconsultchapters6amd7inthetextbook.
More informationCS3157: Advanced Programming. Outline
CS3157: Advanced Programming Lecture #12 Apr 3 Shlomo Hershkop shlomo@cs.columbia.edu 1 Outline Intro CPP Boring stuff: Language basics: identifiers, data types, operators, type conversions, branching
More informationC++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay
C++ Basics Data Processing Course, I. Hrivnacova, IPN Orsay The First Program Comments Function main() Input and Output Namespaces Variables Fundamental Types Operators Control constructs 1 C++ Programming
More information1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \
BSc IT C Programming (2013-2017) Unit I Q1. What do you understand by type conversion? (2013) Q2. Why we need different data types? (2013) Q3 What is the output of the following (2013) main() Printf( %d,
More informationStandard Template Library
Standard Template Library Wednesday, October 10, 2007 10:09 AM 9.3 "Quick Peek" STL history 1990s Alex Stepanov & Meng Lee of HP Labs 1994 ANSI/IS0 standard Components Container class templates Iterators
More informationIII. Classes (Chap. 3)
III. Classes III-1 III. Classes (Chap. 3) As we have seen, C++ data types can be classified as: Fundamental (or simple or scalar): A data object of one of these types is a single object. int, double, char,
More informationCS2141 Software Development using C/C++ C++ Basics
CS2141 Software Development using C/C++ C++ Basics Integers Basic Types Can be short, long, or just plain int C++ does not define the size of them other than short
More informationIntroduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.
Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p. 9 Self-Test Exercises p. 11 History Note p. 12 Programming and
More informationIntroduction to C++ Systems Programming
Introduction to C++ Systems Programming Introduction to C++ Syntax differences between C and C++ A Simple C++ Example C++ Input/Output C++ Libraries C++ Header Files Another Simple C++ Example Inline Functions
More informationIV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard
IV. Stacks 1 A. Introduction 1. Consider the problems on pp. 170-1 (1) Model the discard pile in a card game (2) Model a railroad switching yard (3) Parentheses checker () Calculate and display base-two
More informationCS11 Introduction to C++ Spring Lecture 8
CS11 Introduction to C++ Spring 2013-2014 Lecture 8 Local Variables string getusername() { string user; } cout user; return user;! What happens to user when function
More informationLecture 21 Standard Template Library. A simple, but very limited, view of STL is the generality that using template functions provides.
Lecture 21 Standard Template Library STL: At a C++ standards meeting in 1994, the committee voted to adopt a proposal by Alex Stepanov of Hewlett-Packard Laboratories to include, as part of the standard
More informationLecture-5. STL Containers & Iterators
Lecture-5 STL Containers & Iterators Containers as a form of Aggregation Fixed aggregation An object is composed of a fixed set of component objects Variable aggregation An object is composed of a variable
More informationList, Stack, and Queues
List, Stack, and Queues R. J. Renka Department of Computer Science & Engineering University of North Texas 02/24/2010 3.1 Abstract Data Type An Abstract Data Type (ADT) is a set of objects with a set of
More informationCPSC 427: Object-Oriented Programming
CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 22 November 28, 2016 CPSC 427, Lecture 22 1/43 Exceptions (continued) Code Reuse Linear Containers Ordered Containers Multiple Inheritance
More informationOBJECT ORIENTED PROGRAMMING USING C++
OBJECT ORIENTED PROGRAMMING USING C++ Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both operators and functions can be overloaded
More informationCPSC 427a: Object-Oriented Programming
CPSC 427a: Object-Oriented Programming Michael J. Fischer Lecture 17 November 1, 2011 CPSC 427a, Lecture 17 1/21 CPSC 427a, Lecture 17 2/21 CPSC 427a, Lecture 17 3/21 A bit of history C++ standardization.
More informationSTL: C++ Standard Library
STL: C++ Standard Library Encapsulates complex data structures and algorithms CSC 330 OO Software Design 1 We ve emphasized the importance of software reuse. Recognizing that many data structures and algorithms
More informationOperator overloading
1 Introduction 2 The copy constructor 3 Operator Overloading 4 Eg 1: Adding two vectors 5 The -> operator 6 The this pointer 7 Overloading = 8 Unary operators 9 Overloading for the matrix class 10 The
More informationContainer Notes. Di erent Kinds of Containers. Types Defined by Containers. C++11 Container Notes C++11
Di erent Kinds of Containers Container Notes A container is an object that stores other objects and has methods for accessing the elements. There are two fundamentally di erent kinds of containers: Sequences
More informationPurpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures
C++ Review 1 Purpose of Review Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures 2 Class The Class defines the data structure
More informationThis chapter serves mainly to gather and organize information about iterators. Some new concepts are also introduced for completeness.
Iterators Overview We have introduced, used, built, and studied iterators in several contexts, including List, TDeque, and TVector. We have seen that ordinary pointers also can be thought of as iterators
More informationCS11 Advanced C++ Spring 2018 Lecture 2
CS11 Advanced C++ Spring 2018 Lecture 2 Lab 2: Completing the Vector Last week, got the basic functionality of our Vector template working It is still missing some critical functionality Iterators are
More informationCpt S 122 Data Structures. Templates
Cpt S 122 Data Structures Templates Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Introduction Function Template Function-template and function-template
More informationVALLIAMMAI ENGINEERING COLLEGE
VALLIAMMAI ENGINEERING COLLEGE SRM Nagar, Kattankulathur 603 203 DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING QUESTION BANK B.E. - Electrical and Electronics Engineering IV SEMESTER CS6456 - OBJECT ORIENTED
More informationCOMP322 - Introduction to C++ Lecture 01 - Introduction
COMP322 - Introduction to C++ Lecture 01 - Introduction Robert D. Vincent School of Computer Science 6 January 2010 What this course is Crash course in C++ Only 14 lectures Single-credit course What this
More informationG52CPP C++ Programming Lecture 18. Dr Jason Atkin
G52CPP C++ Programming Lecture 18 Dr Jason Atkin 1 Last lecture Operator Overloading Strings and streams 2 Operator overloading - what to know Know that you can change the meaning of operators Know that
More informationuse static size for this buffer
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
More informationCSS 342 Data Structures, Algorithms, and Discrete Mathematics I. Standard Template Library
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I Standard Template Library 1 Standard Template Library Need to understand basic data types, so we build them ourselves Once understood, we
More informationFor Teacher's Use Only Q No Total Q No Q No
Student Info Student ID: Center: Exam Date: FINALTERM EXAMINATION Spring 2010 CS201- Introduction to Programming Time: 90 min Marks: 58 For Teacher's Use Only Q No. 1 2 3 4 5 6 7 8 Total Marks Q No. 9
More informationPreface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS
Contents Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS 1.1. INTRODUCTION TO COMPUTERS... 1 1.2. HISTORY OF C & C++... 3 1.3. DESIGN, DEVELOPMENT AND EXECUTION OF A PROGRAM... 3 1.4 TESTING OF PROGRAMS...
More informationMore Advanced Class Concepts
More Advanced Class Concepts Operator overloading Inheritance Templates PH (RH) (Roger.Henriksson@cs.lth.se) C++ Programming 2016/17 146 / 281 Operator Overloading In most programming languages some operators
More informationWriting an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and
Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and #include The Use of printf() and scanf() The Use of printf()
More informationPreface to the Second Edition Preface to the First Edition Brief Contents Introduction to C++ p. 1 A Review of Structures p.
Preface to the Second Edition p. iii Preface to the First Edition p. vi Brief Contents p. ix Introduction to C++ p. 1 A Review of Structures p. 1 The Need for Structures p. 1 Creating a New Data Type Using
More informationA linear structure is an ordered (e.g., sequenced) arrangement of elements.
Lists, Stacks, and Queues 1 A linear structure is an ordered (e.g., sequenced) arrangement of elements. There are three common types of linear structures: list stack queue random insertion and deletion
More informationCMSC 341 Lecture 6 STL, Stacks, & Queues. Based on slides by Lupoli, Dixon & Gibson at UMBC
CMSC 341 Lecture 6 STL, Stacks, & Queues Based on slides by Lupoli, Dixon & Gibson at UMBC Templates 2 Common Uses for Templates Some common algorithms that easily lend themselves to templates: Swap what
More information377 Student Guide to C++
377 Student Guide to C++ c Mark Corner January 21, 2004 1 Introduction In this course you will be using the C++ language to complete several programming assignments. Up to this point we have only provided
More informationAN OVERVIEW OF C++ 1
AN OVERVIEW OF C++ 1 OBJECTIVES Introduction What is object-oriented programming? Two versions of C++ C++ console I/O C++ comments Classes: A first look Some differences between C and C++ Introducing function
More informationPointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation The new operator The delete operator Dynamic Memory Allocation for Arrays 1 C++ Data Types simple
More informationC++ Programming Chapter 7 Pointers
C++ Programming Chapter 7 Pointers Yih-Peng Chiou Room 617, BL Building (02) 3366-3603 ypchiou@cc.ee.ntu.edu.tw Photonic Modeling and Design Lab. Graduate Institute of Photonics and Optoelectronics & Department
More informationUNIT- 3 Introduction to C++
UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage
More informationProgramming. C++ Basics
Programming C++ Basics Introduction to C++ C is a programming language developed in the 1970s with the UNIX operating system C programs are efficient and portable across different hardware platforms C++
More informationCome and join us at WebLyceum
Come and join us at WebLyceum For Past Papers, Quiz, Assignments, GDBs, Video Lectures etc Go to http://www.weblyceum.com and click Register In Case of any Problem Contact Administrators Rana Muhammad
More informationCOMP 2355 Introduction to Systems Programming
COMP 2355 Introduction to Systems Programming Christian Grothoff christian@grothoff.org http://grothoff.org/christian/ 1 Functions Similar to (static) methods in Java without the class: int f(int a, int
More informationTopics. bool and string types input/output library functions comments memory allocation templates classes
C++ Primer C++ is a major extension of c. It is similar to Java. The lectures in this course use pseudo-code (not C++). The textbook contains C++. The labs involve C++ programming. This lecture covers
More informationTemplates and Vectors
Templates and Vectors 1 Generic Programming function templates class templates 2 the STL vector class a vector of strings enumerating elements with an iterator inserting and erasing 3 Writing our own vector
More informationAryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9
Fundamental of C Programming Unit I: Q1. What will be the value of the following expression? (2017) A + 9 Q2. Write down the C statement to calculate percentage where three subjects English, hindi, maths
More informationCS201 Some Important Definitions
CS201 Some Important Definitions For Viva Preparation 1. What is a program? A program is a precise sequence of steps to solve a particular problem. 2. What is a class? We write a C++ program using data
More information6.096 Introduction to C++ January (IAP) 2009
MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. Welcome to 6.096 Lecture
More information7.1 Optional Parameters
Chapter 7: C++ Bells and Whistles A number of C++ features are introduced in this chapter: default parameters, const class members, and operator extensions. 7.1 Optional Parameters Purpose and Rules. Default
More informationChapter 2. Procedural Programming
Chapter 2 Procedural Programming 2: Preview Basic concepts that are similar in both Java and C++, including: standard data types control structures I/O functions Dynamic memory management, and some basic
More informationCHAPTER 3 Expressions, Functions, Output
CHAPTER 3 Expressions, Functions, Output More Data Types: Integral Number Types short, long, int (all represent integer values with no fractional part). Computer Representation of integer numbers - Number
More informationCharacter Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Uppercase Alphabets Lowercase Alphabets Character Set A, B, C, Y, Z a, b, c, y, z Digits
More informationADTs & Classes. An introduction
ADTs & Classes An introduction Quick review of OOP Object: combination of: data structures (describe object attributes) functions (describe object behaviors) Class: C++ mechanism used to represent an object
More informationCE221 Programming in C++ Part 1 Introduction
CE221 Programming in C++ Part 1 Introduction 06/10/2017 CE221 Part 1 1 Module Schedule There are two lectures (Monday 13.00-13.50 and Tuesday 11.00-11.50) each week in the autumn term, and a 2-hour lab
More informationSeptember 19,
September 19, 2013 1 Problems with previous examples Changes to the implementation will require recompilation & relinking of clients Extensions will require access to the source code Solutions Combine
More informationCpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University
Cpt S 122 Data Structures Course Review FINAL Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Final When: Wednesday (12/12) 1:00 pm -3:00 pm Where: In Class
More informationSTL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms
STL: C++ Standard Library Standard Template Library (STL) Encapsulates complex data structures and algorithms is a library of generic container classes which are both efficient and functional C++ STL developed
More informationCS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010
CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011 Lectures 1-22 Moaaz Siddiq Asad Ali Latest Mcqs MIDTERM EXAMINATION Spring 2010 Question No: 1 ( Marks: 1 ) - Please
More informationG52CPP C++ Programming Lecture 18
G52CPP C++ Programming Lecture 18 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Welcome Back 2 Last lecture Operator Overloading Strings and streams 3 Operator overloading - what to know
More informationBorland 105, 278, 361, 1135 Bounded array Branch instruction 7 break statement 170 BTree 873 Building a project 117 Built in data types 126
INDEX = (assignment operator) 130, 816 = 0 (as function definition) 827 == (equality test operator) 146! (logical NOT operator) 159!= (inequality test operator) 146 #define 140, 158 #include 100, 112,
More informationInstantiation of Template class
Class Templates Templates are like advanced macros. They are useful for building new classes that depend on already existing user defined classes or built-in types. Example: stack of int or stack of double
More informationIndex. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309
A Arithmetic operation floating-point arithmetic, 11 12 integer numbers, 9 11 Arrays, 97 copying, 59 60 creation, 48 elements, 48 empty arrays and vectors, 57 58 executable program, 49 expressions, 48
More informationCS 610: Intermediate Programming: C/C++ Making Programs General An Introduction to Linked Lists
... 1/17 CS 610: Intermediate Programming: C/C++ Making Programs General An Introduction to Linked Lists Alice E. Fischer Spring 2016 ... 2/17 Outline Generic Functions Command Line Arguments Review for
More informationRecap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam
Multimedia Programming 2004 Lecture 2 Erwin M. Bakker Joachim Rijsdam Recap Learning C++ by example No groups: everybody should experience developing and programming in C++! Assignments will determine
More informationProblem Solving with C++
GLOBAL EDITION Problem Solving with C++ NINTH EDITION Walter Savitch Kendrick Mock Ninth Edition PROBLEM SOLVING with C++ Problem Solving with C++, Global Edition Cover Title Copyright Contents Chapter
More informationReview: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements
Review: Exam 1 9/20/06 CS150 Introduction to Computer Science 1 1 Your First C++ Program 1 //*********************************************************** 2 // File name: hello.cpp 3 // Author: Shereen Khoja
More informationCHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object
CHAPTER 1 Introduction to Computers and Programming 1 1.1 Why Program? 1 1.2 Computer Systems: Hardware and Software 2 1.3 Programs and Programming Languages 8 1.4 What is a Program Made of? 14 1.5 Input,
More informationTABLE OF CONTENTS...2 INTRODUCTION...3
C++ Advanced Features Trenton Computer Festival May 4 tth & 5 tt h, 2002 Michael P. Redlich Systems Analyst ExxonMobil Global Services Company michael.p.redlich@exxonmobil.com Table of Contents TABLE OF
More informationCGS 2405 Advanced Programming with C++ Course Justification
Course Justification This course is the second C++ computer programming course in the Computer Science Associate in Arts degree program. This course is required for an Associate in Arts Computer Science
More informationWhen we program, we have to deal with errors. Our most basic aim is correctness, but we must
Chapter 5 Errors When we program, we have to deal with errors. Our most basic aim is correctness, but we must deal with incomplete problem specifications, incomplete programs, and our own errors. When
More informationKOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 1 OPERATOR OVERLOADING KOM3191 Object-Oriented Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 2 Dynamic Memory Management
More informationl Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)
Final Exam Exercises Chapters 1-7 + 11 Write C++ code to: l Determine if a number is odd or even CS 2308 Fall 2016 Jill Seaman l Determine if a number/character is in a range - 1 to 10 (inclusive) - between
More informationIntroduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms
Introduction Problem Solving on Computer Data Structures (collection of data and relationships) Algorithms 1 Objective of Data Structures Two Goals: 1) Identify and develop useful high-level data types
More informationCSE 333 Lecture 9 - intro to C++
CSE 333 Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington Administrivia & Agenda Main topic: Intro to C++ But first: Some hints on HW2 Labs: The
More information2
STL Design CSC 330 2 3 4 5 Discussions Associative Containers 6 7 Basic Associative Containers 8 set class 9 map class 10 Example multiset (1) 11 Example multiset (2) 12 Example multiset (3) 13 Example
More informationThe Standard Template Library Classes
The Standard Template Library Classes Lecture 33 Sections 9.7, 9.8 Robb T. Koether Hampden-Sydney College Wed, Apr 23, 2014 Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes
More informationCS197c: Programming in C++
CS197c: Programming in C++ Lecture 2 Marc Cartright http://ciir.cs.umass.edu/~irmarc/cs197c/index.html Administration HW1 will be up this afternoon Written assignment Due in class next week See website
More informationQuestion Paper Code : 97044
Reg. No. : Question Paper Code : 97044 B.E./B.Tech. DEGREE EXAMINATION NOVEMBER/DECEMBER 2014 Third Semester Computer Science and Engineering CS 6301 PROGRAMMING AND DATA STRUCTURES-II (Regulation 2013)
More informationAgenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures
The main body and cout Agenda 1 Fundamental data types Declarations and definitions Control structures References, pass-by-value vs pass-by-references The main body and cout 2 C++ IS AN OO EXTENSION OF
More informationAbsolute C++ Walter Savitch
Absolute C++ sixth edition Walter Savitch Global edition This page intentionally left blank Absolute C++, Global Edition Cover Title Page Copyright Page Preface Acknowledgments Brief Contents Contents
More informationIntroduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview
Introduction to Visual Basic and Visual C++ Introduction to Java Lesson 13 Overview I154-1-A A @ Peter Lo 2010 1 I154-1-A A @ Peter Lo 2010 2 Overview JDK Editions Before you can write and run the simple
More informationG Programming Languages Spring 2010 Lecture 11. Robert Soulé, New York University
G22.2110-001 Programming Languages Spring 2010 Lecture 11 Robert Soulé, New York University 1 Review Last week Constructors, Destructors, and Assignment Operators Classes and Functions: Design and Declaration
More informationReview of Important Topics in CS1600. Functions Arrays C-strings
Review of Important Topics in CS1600 Functions Arrays C-strings Array Basics Arrays An array is used to process a collection of data of the same type Examples: A list of names A list of temperatures Why
More informationIntroduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts
Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by Walter
More information