Demonstrating the four ways that vectors can be created. : Vector « Data Structure « C++






Demonstrating the four ways that vectors can be created.


#include <iostream>
#include <vector>
using namespace std;

int main()
{
  vector<char> a;         // empty
  vector<char> b(5, 'X'); // size and initialize
  vector<char> c(b);      // initialize with another vector 
  int i;

  for(i = 0; i <5; i++) 
     a.push_back('A'+i);

  // create a vector from a range
  vector<char> d(a.begin()+1, a.end()-1); 

  for(i = 0; i <a.size(); i++)
    cout << "a[" << i << "]: " << a[i] << "  ";
  cout << "\n\n";

  for(i = 0; i <b.size(); i++)
    cout << "b[" << i << "]: " << b[i] << "  ";
  cout << "\n\n";

  for(i = 0; i <c.size(); i++)
    cout << "c[" << i << "]: " << c[i] << "  ";
  cout << "\n\n";

  for(i = 0; i <d.size(); i++)
    cout << "d[" << i << "]: " << d[i] << "  ";

  return 0;
}


           
       








Related examples in the same category

1.Perform an in-place merge for two vectorsPerform an in-place merge for two vectors
2.Using Other Search functionsUsing Other Search functions
3.Matching Elements Using the equals and mismatch OperationsMatching Elements Using the equals and mismatch Operations
4.Creating and Resizing VectorsCreating and Resizing Vectors
5.Demonstrate count and count_if.Demonstrate count and count_if.
6.Demonstrate remove_copy in VectorDemonstrate remove_copy in Vector
7.Access a vector using an iterator.Access a vector using an iterator.
8.Demonstrate insert and erase.Demonstrate insert and erase.
9.Store a class object in a vector. Store a class object in a vector.
10.Demonstrate allocator's max_size() fucntion in vectorDemonstrate allocator's max_size() fucntion in vector
11.Demonstrate count() in vectorDemonstrate count() in vector
12.Demonstrate count_if().Demonstrate count_if().
13.Demonstrate reverse in vectorDemonstrate reverse in vector
14.Demonstrate insert_iterator in vectorDemonstrate insert_iterator in vector
15.Demonstrate adjacent_difference() in vectorDemonstrate adjacent_difference() in vector
16.Demonstrate inner_product() in vectorDemonstrate inner_product() in vector
17.Demonstrate partial_sum() in VectorDemonstrate partial_sum() in Vector
18.Storing Class Objects with overloaded operators in a Vector
19.Vector Init Array
20.Use istream_iterator with the copy algorithm
21.Demonstrate remove_copy and replace_copy.
22.Vector: Insert Erase Sort
23.Demonstrate accumulate() in vector
24.end() in vector
25.Use pop_back() and empty().
26.Access the elements of a vector through an iterator.
27.The basic operation of a vector: size, push_back,
28.Accessing a Vector Through an Iterator
29.Use istream_iterator to read various data types
30.Create permutations based on vector
31.Work with heaps: make_heap from vector
32.Vector Capacity vs size
33. Using clear()