C++ Templates L03 - Iterator 10 – Iterator.

Slides:



Advertisements
Similar presentations
CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.
Advertisements

ITEC 320 C++ Examples.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Operator Overloading.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
C++ Programming Part 2 Michael Griffiths Corporate Information and Computing Services The University of Sheffield
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
Operator Overloading. Binary operators Unary operators Conversion Operators –Proxy Classes bitset example Special operators –Indexing –Pre-post increment/decrement.
W 5 L 1 sh 1 LessonSubjectBook Week 4 lesson 1Class, copy-constructorH ; p197 – 226 Week 4 lesson 2Operators 1H ; p237 – 254 Week 5 lesson.
Why Use Namespaces? Classes encapsulate behavior (methods) and state (member data) behind an interface Structs are similar, but with state accessible Classes.
CSE 332: C++ data types, input, and output Built-In (a.k.a. Native) Types in C++ int, long, short, char (signed, integer division) –unsigned versions too.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
1 CSC241: Object Oriented Programming Lecture No 08.
Object-Oriented Programming (OOP) and C++
 Binary operators  Unary operators  Conversion Operators  Proxy Classes (simulating a reference) ▪ bitset example  Special operators  Indexing 
Chapter 15 - C++ As A "Better C"
Chapter 18 - C++ Operator Overloading
CSE1002 – Problem Solving with Object Oriented Programming
Operator Overloading CS 3370 – C++ Chapter 14.
Operator Overloading Introduction
Motivation for Generic Programming in C++
Examples (D. Schmidt et al)
Programming with ANSI C ++
C++ Templates.
CS Computer Science IA: Procedural Programming
Lecture 7-2 : STL Iterators
Introduction to C++ Systems Programming.
Motivation and Overview
Object-Oriented Programming (OOP) Lecture No. 21
Command Line Arguments
Pointers and Pointer-Based Strings
P.4 Primitive Data Types and Class Types
Starting Out with C++ Early Objects Eighth Edition
The dirty secrets of objects
Tuesday, February 20, 2018 Announcements… For Today… 4+ For Next Time…
Templates in C++.
(5 - 1) Object-Oriented Programming (OOP) and C++
C++ Templates L03 - Iterator 10 – Iterator.
10 – Iterators C++ Templates 4.6 The Iterator pgs
2.5 Reasoning about Programs: Assertions and Loop Invariants
Lecture 7-2 : STL Iterators
ADT Implementations: Templates and Standard Containers
3.3 Abstract Classes, Assignment, and Casting in a Hierarchy
Lab 03 - Iterator.
15 – Sequential Containers
Operator Overloading; String and Array Objects
Built-In (a.k.a. Native) Types in C++
Arrays Kingdom of Saudi Arabia
Abstraction: Generic Programming, pt. 2
Why Use Namespaces? Classes encapsulate behavior (methods) and state (member data) behind an interface Structs are similar, but with state accessible Classes.
Operator Overloading, Friends, and References
Operator Overloading.
(5 - 1) Object-Oriented Programming (OOP) and C++
CISC/CMPE320 - Prof. McLeod
COMS 261 Computer Science I
C++ Templates L03 - Iterator 10 – Iterator.
COP 3330 Object-oriented Programming in C++
Lecture 8-2 : STL Iterators and Algorithms
Engineering Problem Solving with C++ An Object Based Approach
Parasol Lab, Texas A&M University
Pointers and Pointer-Based Strings
Arrays Arrays A few types Structures of related data items
Operator Overloading; String and Array Objects
Lab4 problems More about templates Some STL
An Introduction to STL.
16 – Sequential Containers
4.1 Introduction Arrays A few types Structures of related data items
Lab 04 - Iterator.
C++ Templates L04 - Iterator 10 – Iterator.
Presentation transcript:

C++ Templates L03 - Iterator 10 – Iterator

Attendance Quiz #12 Iterators

Tip #13: '\n' Confusion Program Correctness Internally all applications use '\n' to indicate line termination. But the line termination sequence is platform specific. Windows (DOS) uses CR and LF. Unix (Linux) uses LF. Mac (OSX) CR. 2 additional formats: Unicode Line Separator (LS) and Unicode Paragraph Separator (PS).

Tip #13: Newline Confusion Iterators The problem with platform specific line termination is The '\n' character is transformed into a platform specific sequence of character(s) when you write it to a file. The platform specific sequence is converted back to '\n' when the file is read. The problem becomes a problem when you write files on one platform and read them on another. For example, after running "filename >> amount;", the newline is still in the buffer. This normally isn't a problem when using only ">>" as it ignores newlines. But when you mix getline and ">>" you need to ignore the newlines that ">>" leaves behind. Bad getline(cin, line, '\n'); cout << "hello\n"; filename >> amount; getline(filename, line); Good getline(cin, line); cout << "hello" << endl; filename >> amount >> std::ws;

Iterators typedef

typedef Iterators typedef is a C/C++ keyword used to create an alias name for another data type. typedef is helpful for giving a short, unambiguous alias to a portable data structure or a complicated function pointer type: typedef unsigned int size_t; typedef vector<int> MyObj; typedef MyObj::iterator Iterator; MyObj numbers; Iterator iter = numbers.begin(); Typedefs provide a level of abstraction away from the actual types being used, allowing you to focus more on the concept of just what a variable should mean. This makes it easier to write clean code, as well as make it far easier to modify your code.

typedef vs Using With C++11, you can use using to create a type alias. Iterators With C++11, you can use using to create a type alias. The identifier following the using keyword becomes a typedef- name. using has the same semantics as if it were introduced by the typedef specifier. For example: typedef vector<string>::iterator StringVectorIterator; using StringVectorIterator = vector<string>::iterator; is equivalent to: In general, if you're using a C++11 compiler, you should consider using the 'using' syntax instead of typedefs.

Prefix / Postfix Unary Operators Iterators Prefix / Postfix Unary Operators

Overload Prefix Operator Iterators The prefix and postfix versions of the unary increment and decrement operators can all be overloaded. To allow both unary prefix and postfix increment usage, each overloaded operator function must have a distinct signature (so that the compiler can determine which version of "++" is intended.) When the compiler sees the pre-increment expression on a Data data type: Data myData; ++myData; the compiler generates the member function call: myData.operator++() The prototype for this operator function would be: Data& operator++();

Overload Postfix Operator Iterators Overloading the postfix increment operator presents a challenge because the compiler must be able to distinguish between the signatures of the overloaded prefix and postfix increment operator functions. The convention that has been adopted in C++ is that when the compiler sees the post-incrementing expression object++, it generates the member function call: myData.operator++(0) The prototype for this operator function would be: Data operator++(int); The argument 0 is strictly a "dummy value" that enables the compiler to distinguish between the prefix and postfix increment operator functions.

Overload Postfix Operator Iterators Postfix operators are expressions that yield the original value and then modify the object. Operator overloads are functions and thus all side effects must take place before the function completes. The only way of attaining the required semantics is by copying the initial state of the object before applying the change. The original state must be returned by value (if a reference was returned, the evaluation of the expression would yield the state of the object after the function completes, and thus would have the semantics of prefix operators, not postfix ones). A typical implementation of postfix involves Creating a temporary object local to the function, Incrementing the originally passed object using the prefix operator, And then returning the temporary object by value, not by reference since the local object is guaranteed to be alive only within the scope of the function and any access to it beyond this scope will result in Undefined Behavior.

Prefix and Postfix Operators Iterators // prefix (return by reference) MyClass& operator++() { // implement increment logic, return reference to it. return *this; } // postfix (return by value) MyClass operator++(int) // the dummy int disambiguates { MyClass tmp(*this); operator++(); // prefix-increment this instance ++*this; // prefix-increment this instance return tmp; // return value before increment } **Note that this means the return value of the overloaded postfix operator must be a non-reference, because we can’t return a reference to a local variable that will be destroyed when the function exits. **Also note that this means the postfix operators are typically less efficient than the prefix operators because of the added overhead of instantiating a temporary variable and returning by value instead of reference.

Doggy Example… Prefix incrementer Postfix incrementer Output? Iterators #include <iostream> using namespace std; class Dog { private: int value; public: Dog() : value(0) {} Dog& operator++() { ++value; return *this; } Dog operator++(int) { Dog temp(*this); ++value; return temp; } friend ostream& operator<<(ostream& os, Dog& d) { return os << d.value; } }; int main() Dog dog; cout << dog++ << endl; cout << ++dog << endl; return 0; } Prefix incrementer Postfix incrementer Output?

Lab 03 - Iterator

Iterator Lab Iterators The concept of an iterator is fundamental to understanding the C++ Standard Template Library (STL). Iterators provide access to data stored in container classes (e.g. vector, map, list, etc.) Your Iterator lab implements a C++ array container class that uses an iterator to access the array elements. Your container supplies begin() and end() functions. Your iterator object overloads the not equal ("!="), dereference ("*"), and pre-increment ("++") operators. Use an iterator to sequentially access elements of the associated array class. Read integer values from a file into the array class. Instantiate an iterator using the begin() function and then use the iterator to iterate thru the array values until the iterator equals the iterator returned by the end() function.

Lab 03 Iterator iter "points" to first element in myArray. Iterators #include <iostream> #include "MyArray.h" using namespace std; int main() { MyArray<int> myArray; myArray.push_back(1); myArray.push_back(2); myArray.push_back(3); myArray.push_back(4); MyArray<int>::iterator iter = myArray.begin(); while (iter != myArray.end()) cout << *iter << " "; ++iter; } return 0; #include <iostream> #include <list> using namespace std; int main() { list<int> myArray; myArray.push_back(1); myArray.push_back(2); myArray.push_back(3); myArray.push_back(4); list<int>::iterator iter = myArray.begin(); while (iter != myArray.end()) cout << *iter << " "; ++iter; } return 0; #include <iostream> #include <vector> using namespace std; int main() { vector<int> myArray; myArray.push_back(1); myArray.push_back(2); myArray.push_back(3); myArray.push_back(4); vector<int>::iterator iter = myArray.begin(); while (iter != myArray.end()) cout << *iter << " "; ++iter; } return 0; iter "points" to first element in myArray. myArray.end() "points" to something NOT in myArray. Dereference iter to access myArray elements.

Pre-increment operator Nested Iterator Class Iterators template<typename T> class MyArray { private: // MyArray data public: MyArray(const size_t maxSize) { ... } void push_back(T item) { ... } std::string toString() const { ... } friend ostream& operator<< (ostream& os, const MyArray<T>& myArray) { ... } }; Iterator is nested in MyArray and CAN NOT be instantiated outside of MyArray MyArray Iterator Not-equal operator class Iterator { private: // Iterator data and private functions public: Iterator(MyArray<T>* array, size_t index) { ... } bool operator!=(const Iterator& other) const { ... } // not-equal Iterator& operator++() { ... } // pre-increment ++ T& operator*() const { ... } // dereference string toString() const { ... } friend ostream& operator<< (ostream& os, const Iterator& iter) { ... } }; Iterator begin() { ... } // pointer to first element Iterator end() { ... } // pointer AFTER last element Pre-increment operator Dereference operator

main Open I/O Streams Read / push_back integer into numbers container. Iterators int main(int argc, char * argv[]) { VS_MEM_CHECK ifstream in(argv[1]); std::ostream& out = (argc < 3) ? std::cout : *(new std::ofstream(argv[2]); out << endl << endl; MyArray<int> numbers(MAX_ARRAY_SIZE); int i; while (in >> i) numbers.push_back(i); out << numbers << endl << endl; out << "SEQUENTIAL" << endl; MyArray<int>::Iterator iter = numbers.begin(); out << "iter: " << iter << endl; for (; iter != numbers.end(); ++iter) out << *iter << " "; return 0; } Open I/O Streams Read / push_back integer into numbers container. Instantiate Iterator Use friend insertion operator to examine Iterator Output number's contents using dereferencing operator Loop until iter and numbers.end() are equal

Output of Iterator Lab myArray: 1 2 3 4 5 6 7 8 9 10 Iterators myArray: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Output myArray contents using MyArray friend << operator. ITERATORS: begin(): size=20 index=0 end(): size=20 index=20 Output iterators using Iterator friend << operator. SEQUENTIAL: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Output myArray contents using a MyArray iterator. PRIME: 2 3 5 7 11 13 17 19 (BONUS) Output all prime numbers in myArray using a MyArray iterator. COMPOSITE: 4 6 8 9 10 12 14 15 16 18 20 (BONUS) Output all composite numbers in myArray using a MyArray iterator. FIBONACCI: 3 = 1 + 2 (BONUS) Output all numbers in myArray equal to the sum of the two previous numbers using a MyArray iterator.