| Instructor: | David Goldschmidt, Ph.D. |
|---|---|
| Office Hours: | after class |
| Email: | click here to email me |
// iterator-example-v1.cpp <== click here for file #include <iostream> #include <vector> using namespace std; // this does not use an iterator (yet); just a pointer int main() { vector<string> courses; courses.push_back( "CSCI 1200" ); courses.push_back( "CSCI 4210" ); courses.push_back( "CSCI 1100" ); courses.push_back( "CSCI 4430" ); courses.push_back( "CSCI 4440" ); for ( unsigned int i = 0 ; i < courses.size() ; i++ ) { cout << courses[i]; if ( courses[i] == "CSCI 4210" ) { cout << " (hey, that's Operating Systems!)"; } cout << '\n'; } // find a course and cancel it string course_to_delete = "CSCI 4210"; bool found = false; unsigned int loc = 0; while ( !found && loc < courses.size() ) { found = ( courses[loc] == course_to_delete ); if ( !found ) { loc++; } } if ( found ) { courses.erase( courses.begin() + loc ); cout << "Course deleted: " << course_to_delete << '\n'; } for ( unsigned int i = 0 ; i < courses.size() ; i++ ) { cout << courses[i] << '\n'; } return 0; }
Order Analysis:
Defining Iterators:
// iterator-example-v2.cpp <== click here for file #include <iostream> #include <vector> using namespace std; // this DOES use an iterator (on vector) int main() { vector<string> courses; courses.push_back( "CSCI 1200" ); courses.push_back( "CSCI 4210" ); courses.push_back( "CSCI 1100" ); courses.push_back( "CSCI 4430" ); courses.push_back( "CSCI 4440" ); // create an iterator vector<string>::iterator p = courses.begin(); while ( p != courses.end() ) { cout << *p; if ( *p == "CSCI 4210" ) { cout << " (hey, that's Operating Systems!)"; } cout << '\n'; p++; // Advance the iterator } // find a course and cancel it string course_to_delete = "CSCI 4210"; bool found = false; vector<string>::iterator loc = courses.begin(); while ( !found && loc != courses.end() ) { found = ( *loc == course_to_delete ); if ( !found ) { loc++; } } if ( found ) { courses.erase( loc ); cout << "Course deleted: " << course_to_delete << '\n'; } for ( p = courses.begin() ; p != courses.end() ; p++ ) { cout << *p << '\n'; } return 0; }
// define an uninitialized pointer p that will refer to // an element in a vector of string objects vector<string>::iterator p; // define an uninitialized pointer q that will refer to // an element in a vector of string objects, though // note that the element referred to CANNOT be changed vector<string>::const_iterator q;
vector<string> courses; vector<string>::iterator p; p = courses.begin(); *p = "CSCI 1100";
vector<Student> students; vector<Student>::iterator i = students.begin(); // Note that the parentheses are required double gpa = (*i).get_gpa(); // Rewrite the line above using -> double gpa = i->get_gpa();
courses.erase( courses.begin() + loc );
// iterator-example-v3.cpp <== click here for file #include <iostream> #include <list> using namespace std; // this DOES use an iterator (on list) int main() { list<string> courses; courses.push_back( "CSCI 1200" ); courses.push_back( "CSCI 4210" ); courses.push_back( "CSCI 1100" ); courses.push_back( "CSCI 4430" ); courses.push_back( "CSCI 4440" ); // create an iterator list<string>::iterator p = courses.begin(); while ( p != courses.end() ) { cout << *p; if ( *p == "CSCI 4210" ) { cout << " (hey, that's Operating Systems!)"; } cout << '\n'; p++; // Advance the iterator } // find a course and cancel it string course_to_delete = "CSCI 4210"; bool found = false; list<string>::iterator loc = courses.begin(); while ( !found && loc != courses.end() ) { found = ( *loc == course_to_delete ); if ( !found ) { loc++; } } if ( found ) { courses.erase( loc ); cout << "Course deleted: " << course_to_delete << '\n'; } for ( p = courses.begin() ; p != courses.end() ; p++ ) { cout << *p << '\n'; } return 0; }
Lists: