Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2003 Prentice Hall, Inc. All rights reserved. 1 21.2.1 vector Sequence Container Declarations –std::vector v; type : int, float, etc. Iterators –std::vector.

Similar presentations


Presentation on theme: " 2003 Prentice Hall, Inc. All rights reserved. 1 21.2.1 vector Sequence Container Declarations –std::vector v; type : int, float, etc. Iterators –std::vector."— Presentation transcript:

1  2003 Prentice Hall, Inc. All rights reserved. 1 21.2.1 vector Sequence Container Declarations –std::vector v; type : int, float, etc. Iterators –std::vector ::const_iterator iterVar; const_iterator cannot modify elements –std::vector ::reverse_iterator iterVar; Visits elements in reverse order (end to beginning) Use rbegin to get starting point Use rend to get ending point

2  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 v(a, a + SIZE) Creates vector v with elements from array a up to (not including) a + SIZE

3  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

4  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

5  2003 Prentice Hall, Inc. All rights reserved. 5 21.2.1 vector Sequence Container ostream_iterator –std::ostream_iterator Name( outputStream, separator ); type : outputs values of a certain type outputStream : iterator output location separator : character separating outputs Example –std::ostream_iterator output( cout, " " ); –std::copy( iterator1, iterator2, output ); Copies elements from iterator1 up to (not including) iterator2 to output, an ostream_iterator

6  2003 Prentice Hall, Inc. All rights reserved. Outline 6 fig21_14.cpp (1 of 3) 1 // Fig. 21.14: fig21_14.cpp 2 // Demonstrating standard library vector class template. 3 #include 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 #include // vector class-template definition 10 11 // prototype for function template printVector 12 template 13 void printVector( const std::vector &integers2 ); 14 15 int main() 16 { 17 const int SIZE = 6; 18 int array[ SIZE ] = { 1, 2, 3, 4, 5, 6 }; 19 20 std::vector integers; 21 22 cout << "The initial size of integers is: " 23 << integers.size() 24 << "\nThe initial capacity of integers is: " 25 << integers.capacity(); 26 Create a vector of int s. Call member functions.

7  2003 Prentice Hall, Inc. All rights reserved. Outline 7 fig21_14.cpp (2 of 3) 27 // function push_back is in every sequence collection 28 integers.push_back( 2 ); 29 integers.push_back( 3 ); 30 integers.push_back( 4 ); 31 32 cout << "\nThe size of integers is: " << integers.size() 33 << "\nThe capacity of integers is: " 34 << integers.capacity(); 35 36 cout << "\n\nOutput array using pointer notation: "; 37 38 for ( int *ptr = array; ptr != array + SIZE; ++ptr ) 39 cout << *ptr << ' '; 40 41 cout << "\nOutput vector using iterator notation: "; 42 printVector( integers ); 43 44 cout << "\nReversed contents of vector integers: "; 45 Add elements to end of vector using push_back.

8  2003 Prentice Hall, Inc. All rights reserved. Outline 8 fig21_14.cpp (3 of 3) 46 std::vector ::reverse_iterator reverseIterator; 47 48 for ( reverseIterator = integers.rbegin(); 49 reverseIterator!= integers.rend(); 50 ++reverseIterator ) 51 cout << *reverseIterator << ' '; 52 53 cout << endl; 54 55 return 0; 56 57 } // end main 58 59 // function template for outputting vector elements 60 template 61 void printVector( const std::vector &integers2 ) 62 { 63 std::vector ::const_iterator constIterator; 64 65 for ( constIterator = integers2.begin(); 66 constIterator != integers2.end(); 67 constIterator++ ) 68 cout << *constIterator << ' '; 69 70 } // end function printVector Walk through vector backwards using a reverse_iterator. Template function to walk through vector forwards.

9  2003 Prentice Hall, Inc. All rights reserved. Outline 9 fig21_14.cpp output (1 of 1) The initial size of v is: 0 The initial capacity of v is: 0 The size of v is: 3 The 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

10  2003 Prentice Hall, Inc. All rights reserved. Outline 10 fig21_15.cpp (1 of 3) 1 // Fig. 21.15: fig21_15.cpp 2 // Testing Standard Library vector class template 3 // element-manipulation functions. 4 #include 5 6 using std::cout; 7 using std::endl; 8 9 #include // vector class-template definition 10 #include // copy algorithm 11 12 int main() 13 { 14 const int SIZE = 6; 15 int array[ SIZE ] = { 1, 2, 3, 4, 5, 6 }; 16 17 std::vector integers( array, array + SIZE ); 18 std::ostream_iterator output( cout, " " ); 19 20 cout << "Vector integers contains: "; 21 std::copy( integers.begin(), integers.end(), output ); 22 23 cout << "\nFirst element of integers: " << integers.front() 24 << "\nLast element of integers: " << integers.back(); 25 Create vector (initialized using an array) and ostream_iterator. Copy range of iterators to output ( ostream_iterator ).

11  2003 Prentice Hall, Inc. All rights reserved. Outline 11 fig21_15.cpp (2 of 3) 26 integers[ 0 ] = 7; // set first element to 7 27 integers.at( 2 ) = 10; // set element at position 2 to 10 28 29 // insert 22 as 2nd element 30 integers.insert( integers.begin() + 1, 22 ); 31 32 cout << "\n\nContents of vector integers after changes: "; 33 std::copy( integers.begin(), integers.end(), output ); 34 35 // access out-of-range element 36 try { 37 integers.at( 100 ) = 777; 38 39 } // end try 40 41 // catch out_of_range exception 42 catch ( std::out_of_range outOfRange ) { 43 cout << "\n\nException: " << outOfRange.what(); 44 45 } // end catch 46 47 // erase first element 48 integers.erase( integers.begin() ); 49 cout << "\n\nVector integers after erasing first element: "; 50 std::copy( integers.begin(), integers.end(), output ); 51 More vector member functions. at has range checking, and can throw an exception.

12  2003 Prentice Hall, Inc. All rights reserved. Outline 12 fig21_15.cpp (3 of 3) 52 // erase remaining elements 53 integers.erase( integers.begin(), integers.end() ); 54 cout << "\nAfter erasing all elements, vector integers " 55 << ( integers.empty() ? "is" : "is not" ) << " empty"; 56 57 // insert elements from array 58 integers.insert( integers.begin(), array, array + SIZE ); 59 cout << "\n\nContents of vector integers before clear: "; 60 std::copy( integers.begin(), integers.end(), output ); 61 62 // empty integers; clear calls erase to empty a collection 63 integers.clear(); 64 cout << "\nAfter clear, vector integers " 65 << ( integers.empty() ? "is" : "is not" ) << " empty"; 66 67 cout << endl; 68 69 return 0; 70 71 } // end main

13  2003 Prentice Hall, Inc. All rights reserved. Outline 13 fig21_15.cpp output (1 of 1) 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 Exception: invalid vector 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


Download ppt " 2003 Prentice Hall, Inc. All rights reserved. 1 21.2.1 vector Sequence Container Declarations –std::vector v; type : int, float, etc. Iterators –std::vector."

Similar presentations


Ads by Google