1 21.2.1 vector Sequence Container • Declarations – std::vector <type> v; • type: int, float, etc. • Iterators – std::vector<type>::const_iterator iterVar; • const_iterator cannot modify elements – std::vector<type>::reverse_iterator iterVar; • Visits elements in reverse order (end to beginning) • Use rbegin to get starting point • Use rend to get ending point 2003 Prentice Hall, Inc. All rights reserved. 2 21.2.1 vector Sequence Container • vector functions – v.push_back(value) • Add element to end (found in all sequence containers). – v.size() • Current size of vector – v.capacity() • How much vector can hold before reallocating memory • Reallocation doubles size – vector<type> v(a, a + SIZE) • Creates vector v with elements from array a up to (not including) a + SIZE 2003 Prentice Hall, Inc. All rights reserved. 3 21.2.1 vector Sequence Container • vector functions – v.insert(iterator, value ) • Inserts value before location of iterator – v.insert(iterator, array , array + SIZE) • Inserts array elements (up to, but not including array + SIZE) into vector – v.erase( iterator ) • Remove element from container – v.erase( iter1, iter2 ) • Remove elements starting from iter1 and up to (not including) iter2 – v.clear() • Erases entire container 2003 Prentice Hall, Inc. All rights reserved. 4 21.2.1 vector Sequence Container • vector functions operations – v.front(), v.back() • Return first and last element – v.[elementNumber] = value; • Assign value to an element – v.at[elementNumber] = value; • As above, with range checking • out_of_bounds exception 2003 Prentice Hall, Inc. All rights reserved. 5 21.2.1 vector Sequence Container • ostream_iterator – std::ostream_iterator< type > Name( outputStream, separator ); • type: outputs values of a certain type • outputStream: iterator output location • separator: character separating outputs • Example – std::ostream_iterator< int > output( cout, " " ); – std::copy( iterator1, iterator2, output ); • Copies elements from iterator1 up to (not including) iterator2 to output, an ostream_iterator 2003 Prentice Hall, Inc. All rights reserved. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 // Fig. 21.14: fig21_14.cpp // Demonstrating standard library vector class template. #include <iostream> Outline fig21_14.cpp (1 of 3) using std::cout; using std::cin; using std::endl; #include <vector> 6 // vector class-template definition // prototype for function template printVector template < class T > void printVector( const std::vector< T > &integers2 ); int main() { Create a const int SIZE = 6; int array[ SIZE ] = { 1, 2, 3, 4, 5, 6 }; std::vector< int > integers; cout << << << << vector of ints. Call member functions. "The initial size of integers is: " integers.size() "\nThe initial capacity of integers is: " integers.capacity(); 2003 Prentice Hall, Inc. All rights reserved. 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 // function push_back integers.push_back( 2 integers.push_back( 3 integers.push_back( 4 is in every sequence collection ); ); ); 7 Add elements to end of Outline vector using push_back. cout << "\nThe size of integers is: " << integers.size() << "\nThe capacity of integers is: " << integers.capacity(); fig21_14.cpp (2 of 3) cout << "\n\nOutput array using pointer notation: "; for ( int *ptr = array; ptr != array + SIZE; ++ptr ) cout << *ptr << ' '; cout << "\nOutput vector using iterator notation: "; printVector( integers ); cout << "\nReversed contents of vector integers: "; 2003 Prentice Hall, Inc. All rights reserved. 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 8 std::vector< int >::reverse_iterator reverseIterator; for ( reverseIterator = integers.rbegin(); reverseIterator!= integers.rend(); ++reverseIterator ) cout << *reverseIterator << ' '; Outline Walk through vector fig21_14.cpp backwards using (3 a of 3) reverse_iterator. cout << endl; return 0; } // end main // function template for outputting vector elements template < class T > void printVector( const std::vector< T > &integers2 ) { std::vector< T >::const_iterator constIterator; Template function to walk through vector forwards. for ( constIterator = integers2.begin(); constIterator != integers2.end(); constIterator++ ) cout << *constIterator << ' '; } // end function printVector 2003 Prentice Hall, Inc. All rights reserved. The The The The initial size of v is: 0 initial capacity of v is: 0 size of v is: 3 capacity of v is: 4 Contents of array a using pointer notation: 1 2 3 4 5 6 Contents of vector v using iterator notation: 2 3 4 Reversed contents of vector v: 4 3 2 9 Outline fig21_14.cpp output (1 of 1) 2003 Prentice Hall, Inc. All rights reserved. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 10 // Fig. 21.15: fig21_15.cpp // Testing Standard Library vector class template // element-manipulation functions. #include <iostream> Outline fig21_15.cpp (1 of 3) using std::cout; using std::endl; #include <vector> #include <algorithm> // vector class-template definition // copy algorithm int main() { const int SIZE = 6; int array[ SIZE ] = { 1, 2, 3, 4, 5, 6 }; std::vector< int > integers( array, array + SIZE ); std::ostream_iterator< int > output( cout, " " ); Create vector (initialized using an array) and ostream_iterator. Copy range of iterators to output (ostream_iterator). cout << "Vector integers contains: "; std::copy( integers.begin(), integers.end(), output ); cout << "\nFirst element of integers: " << integers.front() << "\nLast element of integers: " << integers.back(); 2003 Prentice Hall, Inc. All rights reserved. 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 integers[ 0 ] = 7; integers.at( 2 ) = 10; // set first element to 7 // set element at position 2 to 10 // insert 22 as 2nd element integers.insert( integers.begin() + 1, 22 ); 11 Outline More vector member fig21_15.cpp functions. (2 of 3) cout << "\n\nContents of vector integers after changes: "; std::copy( integers.begin(), integers.end(), output ); // access out-of-range element try { integers.at( 100 ) = 777; at has range checking, and can throw an exception. } // end try // catch out_of_range exception catch ( std::out_of_range outOfRange ) { cout << "\n\nException: " << outOfRange.what(); } // end catch // erase first element integers.erase( integers.begin() ); cout << "\n\nVector integers after erasing first element: "; std::copy( integers.begin(), integers.end(), output ); 2003 Prentice Hall, Inc. All rights reserved. 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 // erase remaining elements integers.erase( integers.begin(), integers.end() ); cout << "\nAfter erasing all elements, vector integers " << ( integers.empty() ? "is" : "is not" ) << " empty"; // insert elements from array integers.insert( integers.begin(), array, array + SIZE ); cout << "\n\nContents of vector integers before clear: "; std::copy( integers.begin(), integers.end(), output ); 12 Outline fig21_15.cpp (3 of 3) // empty integers; clear calls erase to empty a collection integers.clear(); cout << "\nAfter clear, vector integers " << ( integers.empty() ? "is" : "is not" ) << " empty"; cout << endl; return 0; } // end main 2003 Prentice Hall, Inc. All rights reserved. Vector integers contains: 1 2 3 4 5 6 First element of integers: 1 Last element of integers: 6 Contents of vector integers after changes: 7 22 2 10 4 5 6 13 Outline fig21_15.cpp output (1 of 1) Exception: invalid vector<T> subscript Vector integers after erasing first element: 22 2 10 4 5 6 After erasing all elements, vector integers is empty Contents of vector integers before clear: 1 2 3 4 5 6 After clear, vector integers is empty 2003 Prentice Hall, Inc. All rights reserved.