다음을 통해 공유


vector::push_back 및 vector::pop_back

사용 하는 방법을 보여 줍니다 있는 vector::push_backvector::pop_back Visual C++에서 표준 템플릿 라이브러리 (STL) 함수입니다.

template<class _TYPE, class _A>
   void vector::push_back( 
      const _TYPE& X 
   );
template<class _TYPE, class _A>
   void vector::pop_back();

설명

[!참고]

프로토타입에 클래스/매개 변수 이름은 헤더 파일에서 버전이 일치 하지 않습니다.일부 가독성을 높이기 위해 수정 되었습니다.

이 샘플의 정수 빈 벡터를 선언합니다.세 개의 정수를 벡터에 추가 하 고 하나를 삭제 합니다.마지막으로 나머지 요소는 벡터를 생성합니다.

예제

// Pushpop.cpp
// compile with: /EHsc
// Illustrates how to use the push and pop member
// functions of the vector container.
//
// Functions:
//
// vector::push_back - Appends (inserts) an element to the end of a
// vector, allocating memory for it if necessary.
//
// vector::pop_back -  Erases the last element of the vector.
//
// vector::begin - Returns an iterator to start traversal of the vector.
//
// vector::end - Returns an iterator for the last element of the vector.
//
// vector::iterator - Traverses the vector.
//
//////////////////////////////////////////////////////////////////////

// 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)

#include <iostream>
#include <vector>

using namespace std ;

typedef vector<int> INTVECTOR;

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

    // Iterator is used to loop through the vector.
    INTVECTOR::iterator theIterator;

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

    // Add two more elements to the end of the vector.
    // theVector will contain [ 42, 1, 109 ].
    theVector.push_back(1) ;
    theVector.push_back(109) ;

    // Erase last element in vector.
    theVector.pop_back();

    // Print contents of theVector. Shows [ 42, 1 ]
    cout << "theVector [ " ;
    for (theIterator = theVector.begin(); theIterator != theVector.end();
         theIterator++)
    {
        cout << *theIterator;
        if (theIterator != theVector.end()-1) cout << ", ";
                                             
    }
    cout << " ]" << endl ;
}
  

요구 사항

헤더: <vector>

참고 항목

참조

abs (<valarray>)

개념

표준 템플릿 라이브러리 샘플