Professional Documents
Culture Documents
COMP2006 Lecture 10 Demo
COMP2006 Lecture 10 Demo
C++ Programming
Lecture 10 demo
Dr Chao Chen
1
Demo and questions time
2
Demo
• For each loop
• References
3
For-each loops
overloading
4
Container classes
• Container classes in C++ are like Java
Collection classes
• Container classes support Iterators
• Iterators are template classes
– So are the collection classes themselves
vector<string> vstr(10);
vector<string>::iterator p1 = vstr.begin();
for ( ; p1 != vstr.end(); p1++) // ++ op
cout << *p1; // * operator
5
Range based for loop
• New for-loop version for anything with ranges
• Needs iterator to exist, with begin, end and ++
for ( <type> <variable> : <container> )
• Example:
vector<int> vec1(20), vec2(20);
6
Foreach works with arrays too
int arr[] = { 11, 22, 33, 44 };