How to use the vector::reserve, vector::max_size, vector::resize, and vector::capacity STL functions in Visual C++

Article translations Article translations
Article ID: 158621 - View products that this article applies to.
This article was previously published under Q158621
This article has been archived. It is offered "as is" and will no longer be updated.
Note Microsoft Visual C++ .NET 2002 and Microsoft Visual C++ .NET 2003 support both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code. Microsoft Visual C++ 2005 supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model.
Expand all | Collapse all

On This Page

SUMMARY

The following sample code illustrates how to use the vector::reserve, vector::max_size, vector::resize, and vector::capacity STL functions in Visual C++.

MORE INFORMATION

Required Header

   <vector>
				

Prototypes

   template<class _TYPE, class _A>
   void vector::reserve(size_type _N);

   template<class _TYPE, class _A>
   size_type vector::max_size() const;

   template<class _TYPE, class _A>
   void vector::resize(size_type _N, _TYPE _X = _TYPE());

   template<class _TYPE, class _A>
   size_type vector::capacity() const;

   template<class _TYPE, class _A>
   size_type vector::size() const;
				
NOTE: The class/parameter names in the prototype may not match the version in the header file. Some have been modified to improve readability.

Description

The sample declares an empty vector of integers. It adds a single integer element to the vector, and then shows information about the vector's size, maximum size, and capacity. It reserves storage for 1000 elements and displays the same information about the vector. Finally, it resizes the vector to 2000 elements, and then displays the information.

Sample Code

NOTE: The first line in the sample code section says:
// Compile options needed: /GX
In Visual C++ .NET and in Visual C++ 2005, /EHsc is set by default and is equivalent to /GX.
////////////////////////////////////////////////////////////////////// 
// 
// Compile options needed: /GX
// 
//    Remax.cpp: Illustrates vector::reserve, vector::max_size,
//               vector::resize, vector::resize, and vector::capacity.
// 
// Functions:
// 
//    vector::max_size - Returns maximum number of elements vector could
//                       hold.
// 
//    vector::capacity - Returns number of elements for which memory has
//                       been allocated.
// 
//    vector::size - Returns number of elements in the vector.
// 
//    vector::resize - Reallocates memory for vector, preserves its
//                     contents if new size is larger than existing size.
// 
//    vector::reserve - Allocates elements for vector to ensure a minimum
//                      size, preserving its contents if the new size is
//                      larger than existing size.
// 
//    vector::push_back - Appends (inserts) an element to the end of a
//                        vector, allocating memory for it if necessary.
// 
// Written by Tom Campbell
// of Microsoft Corporation
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
////////////////////////////////////////////////////////////////////// 

// The debugger can't handle symbols more than 255 characters long.
// STL often creates symbols longer than that.
// When symbols are longer than 255 characters, the warning is disabled.

#pragma warning(disable:4786)

// disable warning C4267: possible loss of data,
// okay to ignore

#pragma warning(disable: 4267)

#include <iostream>
#include <vector>

#if _MSC_VER > 1020   // if VC++ version is > 4.2
   using namespace std;  // std c++ libs implemented in std
   #endif

typedef vector<int, allocator<int> > INTVECTOR;

void main()

{

    // Dynamically allocated vector begins with 0 elements.
    INTVECTOR theVector;

    // Add one element to the end of the vector, an int with the value 42.
    theVector.push_back(42) ;

    // Show statistics about vector.
    cout << "theVector's size is: " << theVector.size() << endl;
    cout << "theVector's maximum size is: " << theVector.max_size()
         << endl;
    cout << "theVector's capacity is: " << theVector.capacity() << endl;

    // Ensure there's room for at least 1000 elements.
    theVector.reserve(1000);
    cout << endl << "After reserving storage for 1000 elements:" << endl;
    cout << "theVector's size is: " << theVector.size() << endl;
    cout << "theVector's maximum size is: " << theVector.max_size()
         << endl;
    cout << "theVector's capacity is: " << theVector.capacity() << endl;

    // Ensure there's room for at least 2000 elements.
    theVector.resize(2000);
    cout << endl << "After resizing storage to 2000 elements:" << endl;
    cout << "theVector's size is: " << theVector.size() << endl;
    cout << "theVector's maximum size is: " << theVector.max_size()
         << endl;
    cout << "theVector's capacity is: " << theVector.capacity() << endl;

}
				

Program Output

theVector's size is: 1
theVector's maximum size is: 1073741823
theVector's capacity is: 1

After reserving storage for 1000 elements:
theVector's size is: 1
theVector's maximum size is: 1073741823
theVector's capacity is: 1000

After resizing storage to 2000 elements:
theVector's size is: 2000
theVector's maximum size is: 1073741823
theVector's capacity is: 2000
				

REFERENCES

Visual C++ Books Online: Visual C++ Books; C/C++; Standard C++ Library Reference

Properties

Article ID: 158621 - Last Review: February 28, 2014 - Revision: 4.0
APPLIES TO
  • The Standard C++ Library, when used with:
    • Microsoft Visual C++ 4.2 Enterprise Edition
    • Microsoft Visual C++ 5.0 Enterprise Edition
    • Microsoft Visual C++ 6.0 Enterprise Edition
    • Microsoft Visual C++ 4.2 Professional Edition
    • Microsoft Visual C++ 5.0 Professional Edition
    • Microsoft Visual C++ 6.0 Professional Edition
    • Microsoft Visual C++ 6.0 Standard Edition
    • Microsoft Visual C++ .NET 2002 Standard Edition
    • Microsoft Visual C++ .NET 2003 Standard Edition
    • Microsoft Visual C++ 2005 Express Edition
Keywords: 
kbnosurvey kbarchive kbhowto kbinfo kbtemplate kbstl kbcode KB158621

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com