Visual C++에서 vector::erase, vector::empty 및 vector::p ush_back STL 함수 사용

이 문서에서는 Visual C++에서 함수, vector::empty 함수 및 vector::push_back STL(표준 템플릿 라이브러리) 함수를 사용하는 vector::erase 방법을 보여 줍니다. 이 정보는 관리되지 않는 Visual C++ 코드에만 적용됩니다.

원래 제품 버전: Visual C++
원본 KB 번호: 158612

필수 헤더

<vector>

프로토 타입

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

template<class _TYPE, class _A>
iterator vector::erase(iterator Iterator);

template<class _TYPE, class _A>
iterator vector::erase(iterator First, iterator Last);

template<class _TYPE, class _A>
bool vector::empty() const;

참고

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

설명

샘플은 정수의 빈 벡터를 선언합니다. 벡터에 10개의 정수 를 추가한 다음 벡터의 내용을 표시합니다. 지우기를 사용하여 여섯 번째 요소를 삭제한 다음 벡터의 내용을 다시 표시합니다. 다른 형태의 지우기를 사용하여 나머지 요소를 삭제한 다음 벡터(이제 비어 있음)를 다시 표시합니다. 루틴은 ShowVector 빈 함수를 사용하여 벡터의 콘텐츠를 생성할지 여부를 결정합니다.

샘플 코드

Visual C++ .NET 및 Visual C++에서 /EHsc 는 기본적으로 설정되며 /GX와 동일합니다.

참고

Visual C++에서는 샘플 코드를 실행하려는 경우 코드를 에서 로 const ARRAY_SIZE = 10;const int ARRAY_SIZE = 10; 변경해야 합니다.

//////////////////////////////////////////////////////////////////////
// Compile options needed: /GX
// Empty.cpp -- Illustrates the vector::empty and vector::erase
// functions.
// Also demonstrates the vector::push_back function.
// Functions:
// vector::empty - Returns true if vector has no elements.
// vector::erase - Deletes elements from a vector (single & range).
// vector::begin - Returns an iterator to start traversal of the
// vector.
// vector::end - Returns an iterator for the last element of the
// vector.
// vector::push_back - Appends (inserts) an element to the end of a
// vector, allocating memory for it if necessary.
// vector::iterator - Traverses the vector.
// 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.
// When symbols are longer than 255 characters, the warning is disabled.
#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;

const ARRAY_SIZE = 10;

void ShowVector(INTVECTOR &theVector);

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

     // Intialize the vector to contain the numbers 0-9.
     for (int cEachItem = 0; cEachItem < ARRAY_SIZE; cEachItem++)
         theVector.push_back(cEachItem);

     // Output the contents of the dynamic vector of integers.
     ShowVector(theVector);

     // Using void iterator erase(iterator Iterator) to
     // delete the 6th element (Index starts with 0).
     theVector.erase(theVector.begin() + 5);

     // Output the contents of the dynamic vector of integers.
     ShowVector(theVector);

     // Using iterator erase(iterator First, iterator Last) to
     // delete a range of elements all at once.
     theVector.erase(theVector.begin(), theVector.end());

     // Show what's left (actually, nothing).
     ShowVector(theVector);
}

// Output the contents of the dynamic vector or display a
// message if the vector is empty.
void ShowVector(INTVECTOR &theVector)
{
     // First see if there's anything in the vector. Quit if so.
     if (theVector.empty())
     {
         cout << endl << "theVector is empty." << endl;
         return;
     }

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

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

프로그램 출력:

theVector [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
theVector [ 0, 1, 2, 3, 4, 6, 7, 8, 9 ]
theVector is empty.

참조

vector::empty, vector::erase 및 vector::p ush_back