Lecture 7-2 : STL Iterators

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Vectors, lists and queues
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
STL. What is STL? Standard Templates Library Templates are best used for –Defining containers for storing data –Some kinds of algorithms that work the.
More on the STL vector list stack queue priority_queue.
Templates and the STL.
STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley.
Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Section 3.5, class notes Iterators Generic algorithms.
Lecture 8-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for.
Lecture 7-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
CSE 332: C++ templates and generic programming II Review: Concepts and Models Templates impose requirements on type parameters –Types that are plugged.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
Lecture 36 OOP The STL Standard Template Library
Motivation for Generic Programming in C++
Concepts of Programming Languages
Regarding homework 9 Many low grades
Standard Template Library
C++ Templates.
Lecture 7-2 : STL Iterators
Recursion Chapter 12.
CSCE 210 Data Structures and Algorithms
CS3340 – OOP and C++ L. Grewe.
C++ Standard Library.
STL – Standard Template Library
solve the following problem...
Generic Programming Techniques in C++
Starting Out with C++ Early Objects Eighth Edition
לולאות קרן כליף.
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Lecture 7-3 : STL Algorithms
C++ Templates L03 - Iterator 10 – Iterator.
10 – Iterators C++ Templates 4.6 The Iterator pgs
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Today’s Learning Objective
Random Number Generation
Vectors the better arrays.
Chapter 5 Function Basics
Object-Oriented Programming (OOP) Lecture No. 39
priority_queue<T>
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
Standard Template Library Find
Object-Oriented Programming (OOP) Lecture No. 40
Lists - I The List ADT.
Lists - I The List ADT.
Lecture 8 : Intro. to STL (Standard Template Library)
CS1201: Programming Language 2
Standard Template Library (STL)
C++ Templates L03 - Iterator 10 – Iterator.
Lecture 8-2 : STL Iterators and Algorithms
C++ Templates L03 - Iterator 10 – Iterator.
CSCE 121 – Spring 2016 Professor J. Michael Moore
STL (Standard Template Library)
STL and Example.
Pointers & Functions.
Lesson 25 Miscellaneous Topics
Programming Strings.
CMSC 341 C++ and OOP.
An Introduction to STL.
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
Vectors the better arrays.
Lab 04 - Iterator.
C++ Templates L04 - Iterator 10 – Iterator.
Presentation transcript:

Lecture 7-2 : STL Iterators

Iterator Represent certain position in a container (like pointer) For each kind of container in the STL there is an iterator type. list<int>::iterator p; vector<string>::iterator vp; deque<double>::iterator dp; Iterators are a generalization of pointers, and are used much like pointers: They can be used to indicate elements in the sequence. A pair of iterators can be used to indicate a subsequence.

Behaviors of Iterators Ex) vector<string>::iterator vp; Behave just like pointers Access element: *p, p-> Go to next or previous element: ++p, -–p Compare iterators: ==, != Assignment : =

begin() , end() All container classes provide begin(), end() to navigate over their elements begin() Points to beginning of elements in the container end() Points to end of elements in the container The end is the position behind the last element “Past-the-end iterator” begin() end()

example #include <list> #include <iostream> using namespace std; void display(list<int>::iterator first, list<int>::iterator end) { list<int>::iterator p; for (p = first; p != end; ++p) { cout<< *p << “ “ ; } main() list<int> my_list, small, large; list<int>::iterator ip; for (int i = 1; i < 13; ++i) {// initialize values in the list my_list.push_back(i*i % 13); for (ip= my_list.begin(); ip< = my_list.end(); ++ip) { if (*ip< 7) { small.push_back(*ip); } else { large.push_back(*ip); cout<< “my_list: “; display(my_list.begin(), my_list.end()); cout<< endl; cout<< “small: “; display(small.begin(), small.end()); cout<< endl; cout<< “large: “; display(large.begin(), large.end()); cout<< endl;

Why Are Iterators So Great? Because Can access elements of sequence container without differentiating between different container classes they allow us to separate algorithms from containers. The Find() function contains no information about the implementation of the container, or how to move the iterator from one element to the next. The Find() function can be used for any container that provides a suitable iterator. Reusability of Find() : GOOD! template <class IteratorT, class T> IteratorT Find( IteratorT begin, IteratorT end, const T& value ) { for ( ; begin != end ; begin++) if (*begin == value) break; return begin; }

Example: Find() with an int Iterator… #include <iostream> using namespace std; typedef int* Int_Iterator; template <class IteratorT, class T> IteratorT Find( IteratorT begin, IteratorT end, const T& value ) ; int main() { const int SIZE = 100; int x[SIZE]; int num; Int_Iterator begin = x; Int_Iterator end = &x[SIZE]; for (int i = 0; i < SIZE; ++i) { x[i] = 2 * i; } while (true) { cout<< “Enter number: “; cin>> num; Int_Iterator position = Find(begin, end, num); if (position != end) { ++position; if (position != end) cout << “Found before “ << *position << endl; else cout<< “Found as last element” << endl; } else cout<< “Not found” << endl;

Example: Find() with an vector Iterator #include <iostream> #include <vector> // “vector” class from STL using namespace std; template <class IteratorT, class T> IteratorT Find( IteratorT begin, IteratorT end, const T& value ) ; int main() { const int SIZE = 100; vector<int> x(SIZE); int num; for (int i = 0; i < SIZE; ++i) { x[i] = 2 * i; } while (true) { cout<< “Enter number to locate: “; cin >> num; vector<int>::iterator position = Find(x.begin(), x.end(), num); if (position != x.end()) { ++position; if (position != x.end()) cout << “Found before “ << *position << endl; else cout << “Found as last element.” << endl; } else { cout<< “Not found” << endl;

example #include <list> #include <vector> #include <string> #include <iostream> using namespace std; template<class IteratorT> void display(IteratorT start, IteratorT end) // now display() becomes our own generic algorithm! { for( IteratorT p = start; p != end; ++p ) { cout<< *p << “ ” ; } main() list<int> li; vector<string> vs; for (int i = 1; i < 13; ++i) { li.push_back(i*i % 13); vs.push_back(“Now”); vs.push_back(“Is”); vs.push_back(“The”); vs.push_back(“Time”); vs.push_back(“For”); vs.push_back(“All”); cout<< “li: “; display(li.begin(), li.end()); cout<< endl; cout<< “vs: “; display(vs.begin(), vs.end()); cout<< endl;