Create another vector that contains a subrange of vector. : vector subscript indexer « Vector « C++






Create another vector that contains a subrange of vector.

   
#include <iostream>
#include <vector>

using namespace std;

void show(const char *msg, vector<int> vect);

int main() {
  vector<int> v(10);

  for(unsigned i=0; i < v.size(); ++i) v[i] = i*i;

  show("Contents of v: ", v);

  vector<int> v3;
  v3.assign(v.rbegin(), v.rend());
  show("v3 contains the reverse of v: ", v3);
  cout << endl;

  cout << "Size of v is " << v.size() << ". The capacity is "
       << v.capacity() << ".\n";

  v.resize(20);
  cout << "After calling resize(20), the size of v is "
       << v.size() << " and the capacity is "
       << v.capacity() << ".\n";

  v.reserve(50);
  cout << "After calling reserve(50), the size of v is "
       << v.size() << " and the capacity is "
       << v.capacity() << ".\n";

  return 0;
}

void show(const char *msg, vector<int> vect) {
  cout << msg << endl;
  for(unsigned i=0; i < vect.size(); ++i)
    cout << vect[i] << endl;
}
  
    
    
  








Related examples in the same category

1.Use indexer to add elements to a vector
2.Use indexer to reference elements in a vector
3.Loop thourgh all elements in a vector using <
4.Loop through all elements in a vector using operator [] instead of operator *
5.Raise all values in a vector by a given percentage
6.Read double from keyboard, save it to a vector and find the max value