Visual C++ で vector::erase、vector::empty、vector::p ush_back STL 関数を使用する
この記事では、Visual C++ で関数、関数、vector::empty
および標準テンプレート ライブラリ (STL) 関数をvector::push_back
使用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 個の整数を追加し、ベクターの内容を表示します。 消去を使用して 6 番目の要素を削除し、ベクターの内容をもう一度表示します。 別の形式の消去を使用して残りの要素を削除し、ベクター (現在は空) をもう一度表示します。 このルーチンでは 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.
関連情報
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示