Standard Template Library Find

Slides:



Advertisements
Similar presentations
Chapter 21 The STL (maps and algorithms) Bjarne Stroustrup
Advertisements

Chapter 18 Vectors and Arrays
Copyright © 2002 Pearson Education, Inc. Slide 1.
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Introduction to C++ Programming. Brief Facts About C++ Evolved from C Designed and implemented by Bjarne Stroustrup at the Bell Labs in the early 1980s.
Chapter 18 Vectors and Arrays John Keyser’s Modification of Slides by Bjarne Stroustrup
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 5: Functions 1 Based on slides created by Bjarne Stroustrup.
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
Standard Template Library C++ introduced both object-oriented ideas, as well as templates to C Templates are ways to write general code around objects.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Lecture 23 Today Standard Template Library Programs in: programs/p19 Bibliography: Textbook p.252,
Dr. Yingwu Zhu STL Vector and Iterators. STL (Standard Template Library) 6:14:43 AM 2 A library of class and function templates Components: 1. Containers:
CSCE 121: Introduction to Program Design and Concepts, Honors Dr. J. Michael Moore Spring 2015 Set 3: Objects, Types, and Values 1 Based on slides.
C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 4: Computation 1 Based on slides created by Bjarne Stroustrup.
1 The Standard Template Library Drozdek Section 3.7.
CSCE Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 6: Miscellaneous 1 Based on slides created by Bjarne Stroustrup.
Generic Programming in C
C++ Templates.
Generic Algorithms (TIC++V2:C6)
Lecture 7-2 : STL Iterators
C++ Standard Library.
Starting Out with C++ Early Objects Eighth Edition
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Lecture 7-3 : STL Algorithms
Operator Overloading CSCE 121 J. Michael Moore
abstracting away the data structure
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Lecture 7-2 : STL Iterators
ADT Implementations: Templates and Standard Containers
Data Representation Methods
Anatomy of a Function Part 2
Today’s Learning Objective
Chapter 20 The STL (containers, iterators, and algorithms)
C++ Functions, Classes, and Templates
Sorting CSCE 121 J. Michael Moore
Introduction to the Standard Template Library
C++ STL Vector Container
Generic Programming CSCE 121 J. Michael Moore.
IO Overview CSCE 121 J. Michael Moore
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Manipulators CSCE 121 J. Michael Moore
Array & Pointers CSCE 121 J. Michael Moore.
Return by Reference CSCE 121 J. Michael Moore.
Functions Overview CSCE 121 J. Michael Moore
Anatomy of Templates CSCE 121 J. Michael Moore
AP Java Warm-up Boolean Array.
Anatomy of a Function Part 3
Introduction to Programming
Standard Template Library Model
Containers and the Standard Template Library (STL)
Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
STL: Traversing a Vector
Iterators Professor Hugh C. Lauer CS-2303, System Programming Concepts
Standard Template Library
Lists - I The List ADT.
Lists - I The List ADT.
Standard Template Library (STL)
Lecture 8-2 : STL Iterators and Algorithms
CSCE 121 – Spring 2016 Professor J. Michael Moore
Guidelines for Writing Functions
STL (Standard Template Library)
Standard Template Library
C++ Programming: chapter 10 – STL
Data Representation Methods
Data Representation Methods
An Introduction to STL.
Chapter 3 Lists, Stacks, and Queues
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
Presentation transcript:

Standard Template Library Find CSCE 121 Slides adapted by J. Michael Moore Based on slides created by Bjarne Stroustrup and Jennifer Welch

STL find() algorithm // find first element that equals a value template<class It, class T> It find(It first, It last, const T& val) { while (first != last && *first != val) ++first; return first; } // sample use of find, for vector void f(vector<int>& v, int x) { vector<int>::iterator p = find(v.begin(), v.end(), x); if (p != v.end()) { /* we found x */ } // ...

More About STL find() Function It is generic in two aspects element type (sequence of any kind of data) container type (is the sequence implemented with an array, a vector, a linked list, …?) The next slide shows how similar the code is between a vector of ints a list of strings a set of doubles

Comparing Different Uses of find() void f(vector<int>& v, int x) { vector<int>::iterator p = find(v.begin(), v.end(), x); if (p != v.end()) { /* ... */ } } void f(list<string>& v, string x) { list<string>::iterator p = find(v.begin(), v.end(), x); void f(set<double>& v, double x) { set<double>::iterator p = find(v.begin, v.end(), x);