Creating and Resizing Vectors : Vector « Data Structure « C++






Creating and Resizing Vectors

Creating and Resizing Vectors


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

typedef vector<int>    intVector;

template<class T, class A>
void ShowVector(const vector<T, A>& v);

int main()
{
    intVector    intValueVector;        
    cout << "intValueVector" << "\n";
    ShowVector(intValueVector);
    intVector    intValueVector2(3);    
    cout << "intValueVector2(3)" << "\n";
    ShowVector(intValueVector2);


    intValueVector2.resize(5, 100);    
                             
    cout << "intValueVector2 after resize(5, 100)\n";
    ShowVector(intValueVector2);

    intValueVector2.reserve(10);       
    cout << "intValueVector2 after reserve(10)\n";
    ShowVector(intValueVector2);

    return 0;
}

template<class T, class A>
void ShowVector(const vector<T, A>& v)
{
    cout << "max_size() = " << v.max_size();
    cout << "\tsize() = " << v.size();
    cout << "\t" << (v.empty()? "empty": "not empty");
    cout << "\tcapacity() = " << v.capacity();
    cout << "\n\n";
}

           
       








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.Demonstrate count and count_if.Demonstrate count and count_if.
5.Demonstrate remove_copy in VectorDemonstrate remove_copy in Vector
6.Access a vector using an iterator.Access a vector using an iterator.
7.Demonstrate insert and erase.Demonstrate insert and erase.
8.Store a class object in a vector. Store a class object in a vector.
9.Demonstrate allocator's max_size() fucntion in vectorDemonstrate allocator's max_size() fucntion in vector
10.Demonstrate count() in vectorDemonstrate count() in vector
11.Demonstrate count_if().Demonstrate count_if().
12.Demonstrate reverse in vectorDemonstrate reverse in vector
13.Demonstrate insert_iterator in vectorDemonstrate insert_iterator in vector
14.Demonstrate adjacent_difference() in vectorDemonstrate adjacent_difference() in vector
15.Demonstrate inner_product() in vectorDemonstrate inner_product() in vector
16.Demonstrate partial_sum() in VectorDemonstrate partial_sum() in Vector
17.Storing Class Objects with overloaded operators in a Vector
18.Vector Init Array
19.Use istream_iterator with the copy algorithm
20.Demonstrate remove_copy and replace_copy.
21.Vector: Insert Erase Sort
22.Demonstrate accumulate() in vector
23.end() in vector
24.Use pop_back() and empty().
25.Access the elements of a vector through an iterator.
26.The basic operation of a vector: size, push_back,
27.Accessing a Vector Through an Iterator
28.Use istream_iterator to read various data types
29.Create permutations based on vector
30.Work with heaps: make_heap from vector
31.Demonstrating the four ways that vectors can be created.
32.Vector Capacity vs size
33. Using clear()