Help and Support
 

powered byLive Search

How to use the vector::size, vector::capacity, and vector::push_back STL functions in Visual C++

Article ID:160740
Last Review:July 15, 2005
Revision:4.0
This article was previously published under Q160740
Note Microsoft Visual C++ NET (2002) supported both the managed code model that is provided by the .NET Framework and the unmanaged native Windows code model. The information in this article applies to unmanaged Visual C++ code only.
On This Page

SUMMARY

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

Back to the top

MORE INFORMATION

Required header

   <vector>

Back to the top

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.

Back to the top

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.

Back to the top

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>

#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;

    // 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;
}

Back to the top

REFERENCES

For more information about the vector functions, visit the following MSDN Web site: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang98/html/sample_vectorCCsize__vectorCCcapacity__and_more_(STL_Sample).asp (http://msdn.microsoft.com/library/en-us/vclang98/html/sample_vectorCCsize__vectorCCcapacity__and_more_(STL_Sample).asp?frame=true)

Back to the top


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

Back to the top

Keywords: 
kbhowto kbinfo kbtemplate kbstl kbcode KB160740

Back to the top

Article Translations

 

Related Support Centers

Other Support Options

  • Need More Help?
    Contact a Support professional by Email, Online or Phone.
  • Customer Service
    For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
  • Newsgroups
    Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.