Mega Code Archive

 
Categories / C++ / Vector
 

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; }