CObArray::InsertAt
配列の指定したインデックス位置に要素 (または別の配列のすべての要素) を挿入します。
void InsertAt(
INT_PTR nIndex,
CObject* newElement,
INT_PTR nCount = 1
);
void InsertAt(
INT_PTR nStartIndex,
CObArray* pNewArray
);
パラメーター
nIndex
整数インデックス。GetUpperBound が返す値より大きい値も指定できます。newElement
配列に挿入する CObject ポインターを指定します。 newElement の値には、NULL 値も指定できます。nCount
要素を挿入する回数 (既定では 1)。nStartIndex
整数インデックス。GetUpperBound が返す値より大きい値も指定できます。pNewArray
配列に追加する要素を持つ別の配列。
解説
InsertAt の最初の構文は、1 つの要素 (または、その要素の複数のコピー) を配列の指定された位置に挿入します。 挿入処理では、そのインデックス位置にある要素を (インデックスを増加することで) シフト アップし、さらにその位置以降にあるすべての要素を順次シフト アップします。
2 つ目の構文は、他の CObArray コレクションからすべての要素を nStartIndex で指定した位置から挿入します。
対照的に、SetAt 関数は、指定されている配列要素を置き換えるだけで、要素のシフトは行いません。
CObArray::InsertAt に類似している他のメンバー関数を以下に示します。
Class |
メンバー関数 |
---|---|
void InsertAt( INT_PTR nIndex, BYTE newElement, int nCount = 1 ); throw( CMemoryException* ); void InsertAt( INT_PTR nStartIndex, CByteArray* pNewArray ); throw( CMemoryException* ); |
|
void InsertAt( INT_PTR nIndex, DWORD newElement, int nCount = 1 ); throw( CMemoryException* ); void InsertAt( INT_PTR nStartIndex, CDWordArray* pNewArray ); throw( CMemoryException* ); |
|
void InsertAt( INT_PTR nIndex, void* newElement, int nCount = 1 ); throw( CMemoryException* ); void InsertAt( INT_PTR nStartIndex, CPtrArray* pNewArray ); throw( CMemoryException* ); |
|
void InsertAt( INT_PTR nIndex, LPCTSTR newElement, int nCount = 1 ); throw( CMemoryException* ); void InsertAt( INT_PTR nStartIndex, CStringArray* pNewArray ); throw( CMemoryException* ); |
|
void InsertAt( INT_PTR nIndex, UINT newElement, int nCount = 1 ); throw( CMemoryException* ); void InsertAt( INT_PTR nStartIndex, CUIntArray* pNewArray ); throw( CMemoryException* ); |
|
void InsertAt( INT_PTR nIndex, WORD newElement, int nCount = 1 ); throw( CMemoryException* ); void InsertAt( INT_PTR nStartIndex, CWordArray* pNewArray ); throw( CMemoryException* ); |
使用例
すべてのコレクションの例で使われている CAge クラスのリストについては、CObList::CObList を参照してください。
CObArray arr;
arr.Add(new CAge(21)); // Element 0
arr.Add(new CAge(40)); // Element 1 (will become 2).
arr.InsertAt(1, new CAge(30)); // New element 1
#ifdef _DEBUG
afxDump.SetDepth(1);
afxDump << _T("InsertAt example: ") << &arr << _T("\n");
#endif
このプログラムの実行結果は次のようになります。
InsertAt example: A CObArray with 3 elements
[0] = a CAge at $45C8 21
[1] = a CAge at $4646 30
[2] = a CAge at $4606 40
必要条件
**ヘッダー:**afxcoll.h