Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dr. Yingwu Zhu STL Vector and Iterators. STL (Standard Template Library) 6:14:43 AM 2 A library of class and function templates Components: 1. Containers:

Similar presentations


Presentation on theme: "Dr. Yingwu Zhu STL Vector and Iterators. STL (Standard Template Library) 6:14:43 AM 2 A library of class and function templates Components: 1. Containers:"— Presentation transcript:

1 Dr. Yingwu Zhu STL Vector and Iterators

2 STL (Standard Template Library) 6:14:43 AM 2 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)

3 STL’s 10 Containers, p474 6:14:43 AM 3 Kind of Container STL Containers Sequential: deque, list, vector Associative: map, multimap, multiset, set Adapters: priority_queue, queue, stack Non-STL: bitset, valarray, string

4 The vector Container 6:14:43 AM 4 A type-independent pattern for an array class (dynamic array-based) capacity can expand self contained Declaration template class vector { public:... private: T* myArray; } ;

5 The vector Container 6:14:43 AM 5 Constructors vector 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?

6 vector Operations 6:14:43 AM 6 Information about a vector's contents v.size() v.empty() v.capacity()//expand by doubling its size v.reserve()// grow its capacity to para Adding, removing, accessing elements v.push_back() v.pop_back() v.front ()//return a reference to v’s first item v.back()

7 vector Operations 6:14:43 AM 7 Assignment v1 = v2 Swapping v1.swap(v2) Relational operators == implies element by element equality less than < behaves like string comparison

8 Exercises 6:14:43 AM 8 vector v; //right or wrong? Why?

9 Exercises 6:14:43 AM 9 vector v; cout << v.capacity() << " " << v.size() << endl; vector v(3); cout << v.capacity() << " " << v.size() << endl; vector v(4, 5); cout << v.capacity() << " " << v.size() << endl; vector v; v.push_back(9); v.push_back(8); v.push_back(7); cout << v.capacity() << " " << v.front() << endl;

10 Iterators 6:14:43 AM 10 Note from table 9.3 that a subscript operator is provided, p478 BUT … this is not a generic way to access container elements STL provides objects called iterators can point at an element can access the value within that element can move from one element to another They are independent of any particular container … thus a generic mechanism

11 Iterators 6:14:43 AM 11 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 94153 vector v v.begin() v.end()

12 Iterators 6:14:43 AM 12 Each STL container declares an iterator type can be used to define iterator objects To declare an iterator object the identifier iterator must be preceded by name of container scope operator :: Example: vector ::iterator vecIter = v.begin()

13 Iterators 6:14:43 AM 13 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 n th element from current position

14 Iterators 6:14:43 AM 14 Contrast use of subscript vs. use of iterator ostream & operator & v) { for (int i = 0; i < v.size(); i++) out << v[i] << " "; return out; } for (vector ::iterator it = v.begin(); it != v.end(); it++) out << *it << " ";

15 Exercise: output? 6:14:43 AM 15 vector v; for (int i=2; i<=5; i++) v.push_back(1.1 * i); cout << v.capacity() << “ “ << v.size() << endl; vector ::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;

16 Lecture Review 6:14:43 AM 16 Know containers such as vector Like an array, but no need to worry about capacity Generic arrays, can be instantiated with any data types Familiar with basic operations Iterators provide a generic way to access elements in a container Like a pointer, generic pointer Use when bundled with a container


Download ppt "Dr. Yingwu Zhu STL Vector and Iterators. STL (Standard Template Library) 6:14:43 AM 2 A library of class and function templates Components: 1. Containers:"

Similar presentations


Ads by Google