Udostępnij za pośrednictwem


vector::size i vector::capacity

Ilustruje sposób użycia vector::size i vector::capacity funkcje biblioteki szablon standardowy (STL) w programie Visual C++.

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;

Uwagi

[!UWAGA]

Nazwy klasy/parametr w prototyp nie pasują do wersji w pliku nagłówkowym.Niektóre zostały zmodyfikowane w celu poprawienia czytelności.

Próbki deklaruje pusty wektorem liczb całkowitych.Dodaje element pojedynczy całkowitą wektor, a następnie są wyświetlane informacje dotyczące rozmiaru instancji klasy vector, maksymalny rozmiar i zdolności.On rezerw magazynowania dla elementów 1000 i wyświetla te same informacje dotyczące funkcji globalnej vector.Wreszcie zmienia wielkość wektora do 2 000 elementów i następnie wyświetla informacje.

Przykład

// Remax.cpp
// compile with: /EHsc
// 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.
//
//////////////////////////////////////////////////////////////////////

// The debugger cannot 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)

#include <iostream>
#include <vector>

using namespace std ;

typedef vector<int> INTVECTOR;

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

Przykładowe dane wyjściowe

Następujące dane wyjściowe są dla architektury x 86.

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

Wymagania

Nagłówek: <vector>

Zobacz też

Koncepcje

Standardowy szablon biblioteki próbek