Lecture 8-2 : STL Iterators and Algorithms

Slides:



Advertisements
Similar presentations
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Advertisements

Vectors, lists and queues
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
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.
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
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.
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)
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
Lecture 36 OOP The STL Standard Template Library
Andy Wang Object Oriented Programming in C++ COP 3330
Motivation for Generic Programming in C++
Concepts of Programming Languages
Regarding homework 9 Many low grades
Standard Template Library
C++ Templates.
Pointers & Arrays.
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
Andy Wang Object Oriented Programming in C++ COP 3330
Multi-dimensional Array
solve the following problem...
Generic Programming Techniques in C++
Starting Out with C++ Early Objects Eighth Edition
לולאות קרן כליף.
The C++ Algorithm Libraries
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
Lecture 7-2 : STL Iterators
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Today’s Learning Objective
Vectors the better arrays.
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Data type List Definition:
priority_queue<T>
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
Standard Template Library Find
Elements are always copied when they are put into a container
Object-Oriented Programming (OOP) Lecture No. 40
Lecture 8 : Intro. to STL (Standard Template Library)
CS1201: Programming Language 2
Arrays of Two-Dimensions
Standard Template Library (STL)
C++ Templates L03 - Iterator 10 – Iterator.
C++ Templates L03 - Iterator 10 – Iterator.
CSCE 121 – Spring 2016 Professor J. Michael Moore
Pointers & Arrays.
CMSC 341 C++ and OOP.
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 8-2 : STL Iterators and Algorithms

Iterators For each kind of container in the STL there is an iterator type. list<int>::iterator ip; 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. Operations on iteratorsare just like pointers in arrays: Access element: *p, p-> Go to next or previous element: ++p, –p Compare iterators: ==, !=

example #include <list> #include <iostream> using namespace std; void display(list<int>::iteratorfirst, list<int>::iteratorend) { list<int>::iteratorp; 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;

Example: locate() with an int Iterator Iterators provide a systematic way of looking at elements of sequence container without differentiating between different container classes. The same code works correctly for all sequence container classes // File: “locate_int_iterator.cpp” typedef int* Int_Iterator; Int_Iterator locate( Int_Iterator begin, Int_Iterator end, const int& value ) { while (begin != end && *begin != value) { ++begin; } return begin;

Example: locate() with an int Iterator… #include <iostream> #include “locate_int_iterator.cpp” using namespace std; 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 = locate(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;

Why Are Iterators So Great? Because they allow us to separate algorithms from containers. If we change the locate() function as follows, it still works: The new locate() function contains no information about the implementation of the container, or how to move the iterator from one element to the next. The same locate() function can be used for any container that provides a suitable iterator. Using iterators lets us turn locate() into a re-useable generic function! template <class IteratorT, class T> IteratorT locate( IteratorT begin, IteratorT end, const T& value ) { while (begin != end && *begin != value) { ++begin; } return begin;

Example: locate() with an Iterator #include <iostream> #include <vector> // “vector” class from STL using namespace std; int main() { 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 = locate(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;