다음을 통해 공유


vector::resize

벡터에 대해 요소 수로 지정되는 새 크기를 지정합니다.

void resize( size_type Newsize ); void resize( size_type Newsize, Type Val );

매개 변수

  • Newsize
    벡터의 새 크기입니다.

  • Val
    새 크기가 원래 크기보다 큰 경우 벡터에 추가된 새 요소의 초기화 값입니다. 이 값이 생략되면 새 개체는 기본 생성자를 사용합니다.

설명

컨테이너의 크기가 요청된 크기 Newsize보다 작으면 요청한 크기에 도달할 때까지 벡터에 요소가 추가됩니다. 컨테이너의 크기가 요청된 크기보다 크면 컨테이너가 요청된 크기 Newsize에 도달할 때까지 컨테이너 끝에서 가장 가까운 요소가 삭제됩니다. 컨테이너의 현재 크기와 요청된 크기가 동일하면 아무런 작업도 실행되지 않습니다.

크기는 벡터의 현재 크기를 반영합니다.

예제

// vectorsizing.cpp
// compile with: /EHsc /W4
// 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.
// The warning can be disabled:
//#pragma warning(disable:4786)

#include <iostream>
#include <vector>
#include <string>

using namespace std;

template <typename C> void print(const string& s, const C& c) {
    cout << s;

    for (const auto& e : c) {
        cout << e << " ";
    }
    cout << endl;
}

void printvstats(const vector<int>& v) {
    cout << "   the vector's size is: " << v.size() << endl;
    cout << "   the vector's capacity is: " << v.capacity() << endl;
    cout << "   the vector's maximum size is: " << v.max_size() << endl;
}

int main()
{
    // declare a vector that begins with 0 elements.
    vector<int> v;

    // Show statistics about vector.
    cout << endl << "After declaring an empty vector:" << endl;
    printvstats(v);
    print("   the vector's contents: ", v);

    // Add one element to the end of the vector.
    v.push_back(-1);
    cout << endl << "After adding an element:" << endl;
    printvstats(v);
    print("   the vector's contents: ", v);

    for (int i = 1; i < 10; ++i) {
        v.push_back(i);
    }
    cout << endl << "After adding 10 elements:" << endl;
    printvstats(v);
    print("   the vector's contents: ", v);

    v.resize(6);
    cout << endl << "After resizing to 6 elements without an initialization value:" << endl;
    printvstats(v);
    print("   the vector's contents: ", v);

    v.resize(9, 999);
    cout << endl << "After resizing to 9 elements with an initialization value of 999:" << endl;
    printvstats(v);
    print("   the vector's contents: ", v);

    v.resize(12);
    cout << endl << "After resizing to 12 elements without an initialization value:" << endl;
    printvstats(v);
    print("   the vector's contents: ", v);

    // Ensure there's room for at least 1000 elements.
    v.reserve(1000);
    cout << endl << "After vector::reserve(1000):" << endl;
    printvstats(v);

    // Ensure there's room for at least 2000 elements.
    v.resize(2000);
    cout << endl << "After vector::resize(2000):" << endl;
    printvstats(v);
}

출력

After declaring an empty vector:
   the vector's size is: 0
   the vector's capacity is: 0
   the vector's maximum size is: 1073741823
   the vector's contents:

After adding an element:
   the vector's size is: 1
   the vector's capacity is: 1
   the vector's maximum size is: 1073741823
   the vector's contents: -1

After adding 10 elements:
   the vector's size is: 10
   the vector's capacity is: 13
   the vector's maximum size is: 1073741823
   the vector's contents: -1 1 2 3 4 5 6 7 8 9

After resizing to 6 elements without an initialization value:
   the vector's size is: 6
   the vector's capacity is: 13
   the vector's maximum size is: 1073741823
   the vector's contents: -1 1 2 3 4 5

After resizing to 9 elements with an initialization value of 999:
   the vector's size is: 9
   the vector's capacity is: 13
   the vector's maximum size is: 1073741823
   the vector's contents: -1 1 2 3 4 5 999 999 999

After resizing to 12 elements without an initialization value:
   the vector's size is: 12
   the vector's capacity is: 13
   the vector's maximum size is: 1073741823
   the vector's contents: -1 1 2 3 4 5 999 999 999 0 0 0

After vector::reserve(1000):
   the vector's size is: 12
   the vector's capacity is: 1000
   the vector's maximum size is: 1073741823

After vector::resize(2000):
   the vector's size is: 2000
   the vector's capacity is: 2000
   the vector's maximum size is: 1073741823

요구 사항

헤더: <vector>

네임스페이스: std

참고 항목

참조

vector 클래스

표준 템플릿 라이브러리