vector::empty, vector::erase, 및 vector::push_back
사용 하는 방법을 보여 줍니다 있는 vector::empty, vector::erase, 및 vector::push_back STL 함수를 Visual C++.
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 루틴이 빈 함수를 사용 하 여 벡터의 내용을 생성 여부를 결정 합니다.
예제
// Empty.cpp
// compile with: /EHsc
// 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.
//
//////////////////////////////////////////////////////////////////////
// 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;
const int ARRAY_SIZE = 10;
void ShowVector(INTVECTOR &theVector);
int 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 << "theVector is empty." << endl;
return;
}
// Iterator is used to loop through the vector.
INTVECTOR::iterator theIterator;
// Output contents of theVector.
cout << "theVector [ " ;
for (theIterator = theVector.begin(); theIterator != theVector.end();
theIterator++)
{
cout << *theIterator;
if (theIterator != theVector.end()-1) cout << ", ";
// cosmetics for the output
}
cout << " ]" << endl ;
}
요구 사항
헤더: <vector>