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

Home
C++
1.Bitset
2.Class
3.Console
4.Data Structure
5.Data Type
6.Deque
7.Development
8.File
9.Function
10.Generic
11.Language
12.List
13.Map Multimap
14.Overload
15.Pointer
16.Qt
17.Queue Stack
18.Set Multiset
19.STL Algorithms Binary search
20.STL Algorithms Heap
21.STL Algorithms Helper
22.STL Algorithms Iterator
23.STL Algorithms Merge
24.STL Algorithms Min Max
25.STL Algorithms Modifying sequence operations
26.STL Algorithms Non modifying sequence operations
27.STL Algorithms Sorting
28.STL Basics
29.String
30.Valarray
31.Vector
C / ANSI-C
C Tutorial
C++ Tutorial
Visual C++ .NET
C++ » Vector » vector subscript indexerScreenshots 
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(); ++iv[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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.