Shallow copy The modified vector is returned vectorint A4 0 A 0 0 0 0 vectorint

# Shallow copy the modified vector is returned

This preview shows page 53 - 68 out of 105 pages.

Shallow copy The modified vector is returned vector<int> A(4, 0); // A: 0 0 0 0 vector<int> B(3, 1); // B: 1 1 1 A = B; // A: 1 1 1 Vector Interface reference operator [] (size_type i) Returns a reference to element i of the vector L-value const_reference operator [] (size_type i) const Returns a constant reference to element i of the vector R-value Example vector< int > A(4, 0); // A: 0 0 0 0 const vector< int > B(4, 0); // B: 0 0 0 0 for ( int i = 0; i < A.size(); ++i) { A[i] = 3; } // A: 3 3 3 3 for ( int i = 0; i < A.size(); ++i) { cout << A[i] << endl; // l-value cout << B[i] << endl; // r-value } Vector Interface reference at (size_type i) If i is in bounds, returns a reference to element i of the vector; otherwise, throws an exception const_reference at (size_type i) const If i is in bounds, returns a constant reference to element i of the vector; otherwise, throws an exception Example vector< int > A(4, 0); // A: 0 0 0 0 for ( int i = 0; i <= A.size(); ++i) { A[i] = 3; } // A: 3 3 3 3 ?? for ( int i = 0; i <= A.size(); ++i) { A.at(i) = 3; // program terminates } // when i is 4 Vector Interface void resize (size_type s, T val = T()) The number of elements in the vector is now s . To achieve this size, elements are deleted or added as necessary Deletions if any are performed at the end Additions if any are performed at the end New elements have value val vector< int > A(4, 0); // A: 0 0 0 0 A.resize(8, 2); // A: 0 0 0 0 2 2 2 2 A.resize(3, 1); // A: 0 0 0 Function Examples void GetList(vector< int > &A) { int n = 0; while ((n < A.size()) && (cin >> A[n])){ ++n; } A.resize(n); } void PutList(const vector< int > &A) { for ( int i = 0; i < A.size(); ++i) { cout << A[i] << endl; } } vector< int > MyList(3); cout << "Enter numbers: "; GetList(MyList); PutList(MyList); Vector Interface pop_back () Removes the last element of the vector push_back ( const T &val) Inserts a copy of val after the last element of the vector Example void GetValues(vector< int > &A) { A.resize(0); int iVal; while (cin >> iVal) { A.push_back(iVal); } } vector< int > List; cout << "Enter numbers: "; GetValues(List); Overloading >> istream& operator >>(istream& sin, vector< int > &A) { A.resize(0); int iVal; while (sin >> iVal) { A.push_back(iVal); } return sin; } vector< int > B; cout << "Enter numbers: "; cin >> B; Vector Interface reference front () Returns a reference to the first element of the vector const_reference front () const Returns a constant reference to the first element of the vector Example vector< int > B(4,1); // B: 1 1 1 1 int & val = B.front(); val = 7; // B: 7 1 1 1 Vector Interface reference back () Returns a reference to the last element of the vector const_reference back () const Returns a constant reference to the last element of the vector Example vector< int > C(4,1); // C: 1 1 1 1 int & val = C.back(); val = 5; // C: 1 1 1 5 Iterators Iterator is a pointer to an element Really pointer abstraction Mechanism for sequentially accessing the elements in the list Alternative to subscripting There is an iterator type for each kind of vector list 14 723 512 621 114 A P iterator Sentinel Iterators Notes Algorithm component of STL uses iterators Code using iterators rather than subscripting can often be reused by other objects using different container representations 14 723 512 621 114 A P iterator Sentinel Vector Interface iterator begin () Returns an iterator that points to the first element of the vector iterator end ()  #### You've reached the end of your free preview.

Want to read all 105 pages?

• • •  