STL Sample for the vector::(capacity size push_back) Functions

Last reviewed: October 9, 1997
Article ID: Q160740
The information in this article applies to:
  • The Standard C++ Library included with: - Microsoft Visual C++, 32-bit Edition, version 4.2

SUMMARY

The sample code below illustrates how to use the vector::size, vector::capacity, and vector::push_back functions.

MORE INFORMATION

Required Header

   <vector>

Prototypes

   size_type vector::capacity() const
   size_type vector::size() const
   void vector::push_back(const _TYPE& _X)

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 outputs the size and capacity of the vector.
  • It adds a single element to the vector, then outputs the size and capacity again.

Sample Code

//////////////////////////////////////////////////////////////////////
//
// Compile options needed: /GX
//
// Capacity.cpp  - Illustrates vector::capacity and vector::size
//
// Functions:
//
//    vector::capacity  - Return # of elements for which memory has
//                        been allocated.
//
//    vector::size      - Return # of elements in the vector
//
//    vector::push_back - Append (insert) 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.
// This disables the warning issued when symbols are longer than 255 chars.
#pragma warning(disable:4786)

#include <iostream>
#include <vector>

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

void main()
{
    // Dynamically allocated vector begins with 0 elements.
    INTVECTOR theVector;

    // Show size (current # of elements) and capacity (# of elements
    // there's room for) of the empty vector.
    cout << endl << "Vector with no elements:" << endl;
    cout << "theVector's size is: " << theVector.size() << endl;
    cout << "theVector's capacity is: " << theVector.capacity() << endl;

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

    // Show size & capacity after adding an element to the vector.
    cout << endl << "After adding 1 element:" << endl;
    cout << "theVector's size is: " << theVector.size() << endl;
    cout << "theVector's capacity is: " << theVector.capacity() << endl;
}

REFERENCES

Visual C++ Books On Line: Visual C++ Books:C/C++: Standard C++ Library Reference.


Additional query words: STL STLSample vector capacity size push_back
Keywords : STLIss kbcode
Version : WINNT:4.2;
Platform : NT WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 9, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.