Change element with const iterator : vector elements « vector « C++ Tutorial






#include <vector> 
#include <iostream> 

using namespace std; 

vector<double> makevector(int size){

  vector<double> result;
  for (int i=1; i<=size; i++) { 
    result.push_back(double(i)); 
  } 
  return result; 
} 

void print(const vector<double>& l) 
{ 

  cout << "Size of vector is: " << l.size() << endl; 

  vector<double>::const_iterator i; 

  for (i=l.begin(); i!=l.end(); i++) 
  { 
    cout << (*i) << " "; 
  } 

  cout << endl; 
} 

int main() 
{ 
  vector<double> vector1=makevector(5); 
  vector<double>::iterator bi; 

  vector<double>::reverse_iterator ri; 

  ri=vector1.rbegin();          // Set iterator 
  while (ri!=vector1.rend()) cout << (*ri++) << " "; 
  cout << endl << endl; 

  // Change element with const iterator 
  cout << "Change first element with const iterator" << endl; 
  vector<double>::const_iterator ci; 
  ci=vector1.end(); 
  ci--; 
  cout << *ci << endl; 
}








16.26.vector elements
16.26.1.Vector Creation and Element Access
16.26.2.Change element with pointer
16.26.3.Change element with const iterator
16.26.4.Accessing Elements in a Vector Using Array Semantics
16.26.5.Accessing Elements in a Vector Using Pointer Semantics (Iterators)
16.26.6.Set Element Values in a vector Using Array Semantics
16.26.7.Instantiate a vector with 10 elements (it can grow larger)
16.26.8.Instantiate a vector with 10 elements, each initialized to 90
16.26.9.Instantiate a vector to 5 elements taken from another
16.26.10.The first and last element in vector as pointed to by begin() and end() - 1
16.26.11.The first and last element in vector as pointed to by rbegin() and rend() - 1
16.26.12.Add elements to the end of vector with push_back
16.26.13.Constructing a Container That Has Identical Elements
16.26.14.Constructing a Container That Has Specified Values
16.26.15.Elements Being Created, Assigned, and Destroyed
16.26.16.Add all the elements from vector Two to the end of vector One
16.26.17.append one more element. This causes reallocation
16.26.18.Instantiate a vector with 4 elements, each initialized to 90
16.26.19.Access the elements of a vector through an iterator.