Upload
jewel-reynolds
View
231
Download
1
Embed Size (px)
Citation preview
Dr. Yingwu Zhu
STL Vector and Iterators
STL (Standard Template Library)
02:39:02 AM2
A library of class and function templates
Components:1.Containers:
• Generic "off-the-shelf" class templates for storing collections of data
2.Algorithms: • Generic "off-the-shelf" function templates for operating
on containers
3.Iterators: • Generalized "smart" pointers that allow algorithms to
operate on almost any container (access container elements)
STL’s 10 Containers, p474
02:39:02 AM3
Kind of Container STL ContainersSequential: deque, list, vector
Associative: map, multimap,
multiset, set
Adapters: priority_queue, queue, stack
Non-STL: bitset, valarray, string
The vector Container
02:39:02 AM4
A type-independent pattern for an array class (dynamic array-based)capacity can expandself contained
Declarationtemplate <typename T>class vector { public: . . .private: T* myArray;
} ;
The vector Container
02:39:02 AM5
Constructors
vector<T> v, // empty vector
v1(100), // 100 elements of type T
v2(100, val), // 100 copies of val
v3(fptr,lptr); // contains copies of // elements in memory
// locations fptr to lptr
Exercises? Examples?
vector Operations
02:39:02 AM6
Information about a vector's contentsv.size()v.empty()v.capacity()//expand by doubling its size
v.reserve()//grow its capacity to paraAdding, removing, accessing elements
v.push_back()v.pop_back()v.front()//return a reference to v’s first itemv.back()
vector Operations
02:39:02 AM7
Assignmentv1 = v2
Swappingv1.swap(v2)
Relational operators == implies element by element equalityless than < behaves like string comparison
Exercises
02:39:02 AM8
vector v; //right or wrong? Why?
Exercises
02:39:02 AM9
vector<double> v;cout << v.capacity() << " " << v.size() << endl;
vector<int> v(3); cout << v.capacity() << " " << v.size() << endl;
vector<int> v(4, 5); cout << v.capacity() << " " << v.size() << endl;
vector<int> v;v.push_back(9); v.push_back(8); v.push_back(7);cout << v.capacity() << " " << v.front() << endl;
Iterators
02:39:02 AM10
Note from table 9.3 that a subscript operator is provided, p478BUT … this is not a generic way to access
container elementsSTL provides objects called iterators
can point at an elementcan access the value within that elementcan move from one element to another
They are independent of any particular container … thus a generic mechanism
Iterators
02:39:02 AM11
Given a vector which has had values placed in the first 4 locations:
v.begin() will return the iterator value for the first slot,
v.end() for the next empty slot
9 4 15 3
vector<int> v
v.begin()v.begin() v.end()v.end()
Iterators
02:39:02 AM12
Each STL container declares an iterator typecan be used to define iterator objects
To declare an iterator objectthe identifier iterator must be preceded by
name of containerscope operator ::
Example:vector<int>::iterator vecIter = v.begin()
Iterators
02:39:02 AM13
Basic operators that can be applied to iterators:increment operator ++decrement operator --dereferencing operator *Assignment =Addition, subtraction +, -, +=, -=vecIter + n returns iterator positioned n elements away
Subscript operator [ ]vecIter[n] returns reference to nth element from current position
Iterators
02:39:03 AM14
Contrast use of subscript vs. use of iteratorostream & operator<<(ostream & out, const vector<double> & v){ for (int i = 0; i < v.size(); i++) out << v[i] << " "; return out;}
for (vector<double>::iterator it = v.begin(); it != v.end(); it++) out << *it << " ";
Exercise: output?
02:39:03 AM15
vector<double> v;for (int i=2; i<=5; i++)
v.push_back(1.1 * i);cout << v.capacity() << “ “ << v.size() << endl;vector<double>::iterator it, it1, it2;for (it = v.begin(); it != v.end(); it++)
cout << *it << “ “;cout << endl;it1 = v.begin(); it2 = v.end();*it1 = 8.8; *(it2-1) = 9.9;for (it = v.begin(); it != v.end(); it++)
cout << *it << “ “;cout << endl;it1 += 2; it2--;cout << it1[1] << “ “ << it2[-1] << endl;
Lecture Review
02:39:03 AM16
Know containers such as vectorLike an array, but no need to worry about
capacityGeneric arrays, can be instantiated with any data
typesFamiliar with basic operations
Iterators provide a generic way to access elements in a container
Like a pointer, generic pointerUse when bundled with a container