description | title | ms.date | f1_keywords | helpviewer_keywords | ms.assetid | ||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Learn more about: CList Class |
CList Class |
11/04/2016 |
|
|
6f6273c3-c8f6-47f5-ac2a-0a950379ae5d |
Supports ordered lists of nonunique objects accessible sequentially or by value.
template<class TYPE, class ARG_TYPE = const TYPE&>
class CList : public CObject
Name | Description |
---|---|
CList::CList |
Constructs an empty ordered list. |
Name | Description |
---|---|
CList::AddHead |
Adds an element (or all the elements in another list) to the head of the list (makes a new head). |
CList::AddTail |
Adds an element (or all the elements in another list) to the tail of the list (makes a new tail). |
CList::Find |
Gets the position of an element specified by pointer value. |
CList::FindIndex |
Gets the position of an element specified by a zero-based index. |
CList::GetAt |
Gets the element at a given position. |
CList::GetCount |
Returns the number of elements in this list. |
CList::GetHead |
Returns the head element of the list (cannot be empty). |
CList::GetHeadPosition |
Returns the position of the head element of the list. |
CList::GetNext |
Gets the next element for iterating. |
CList::GetPrev |
Gets the previous element for iterating. |
CList::GetSize |
Returns the number of elements in this list. |
CList::GetTail |
Returns the tail element of the list (cannot be empty). |
CList::GetTailPosition |
Returns the position of the tail element of the list. |
CList::InsertAfter |
Inserts a new element after a given position. |
CList::InsertBefore |
Inserts a new element before a given position. |
CList::IsEmpty |
Tests for the empty list condition (no elements). |
CList::RemoveAll |
Removes all the elements from this list. |
CList::RemoveAt |
Removes an element from this list, specified by position. |
CList::RemoveHead |
Removes the element from the head of the list. |
CList::RemoveTail |
Removes the element from the tail of the list. |
CList::SetAt |
Sets the element at a given position. |
TYPE
Type of object stored in the list.
ARG_TYPE
Type used to reference objects stored in the list. Can be a reference.
CList
lists behave like doubly-linked lists.
A variable of type POSITION
is a key for the list. You can use a POSITION
variable as an iterator to traverse a list sequentially and as a bookmark to hold a place. A position is not the same as an index, however.
Element insertion is very fast at the list head, at the tail, and at a known POSITION
. A sequential search is necessary to look up an element by value or index. This search can be slow if the list is long.
If you need a dump of individual elements in the list, you must set the depth of the dump context to 1 or greater.
Certain member functions of this class call global helper functions that must be customized for most uses of the CList
class. See Collection Class Helpers in the "Macros and Globals" section.
For more information on using CList
, see the article Collections.
[!code-cppNVC_MFCCollections#35]
CList
Header: afxtempl.h
Adds a new element or list of elements to the head of this list.
POSITION AddHead(ARG_TYPE newElement);
void AddHead(CList* pNewList);
ARG_TYPE
Template parameter specifying the type of the list element (can be a reference).
newElement
The new element.
pNewList
A pointer to another CList
list. The elements in pNewList
will be added to this list.
The first version returns the POSITION
value of the newly inserted element.
The list can be empty before the operation.
[!code-cppNVC_MFCCollections#36]
Adds a new element or list of elements to the tail of this list.
POSITION AddTail(ARG_TYPE newElement);
void AddTail(CList* pNewList);
ARG_TYPE
Template parameter specifying the type of the list element (can be a reference).
newElement
The element to be added to this list.
pNewList
A pointer to another CList
list. The elements in pNewList
will be added to this list.
The first version returns the POSITION
value of the newly inserted element.
The list can be empty before the operation.
[!code-cppNVC_MFCCollections#37]
Constructs an empty ordered list.
CList(INT_PTR nBlockSize = 10);
nBlockSize
The memory-allocation granularity for extending the list.
As the list grows, memory is allocated in units of nBlockSize
entries.
[!code-cppNVC_MFCCollections#38]
Searches the list sequentially to find the first element matching the specified searchValue
.
POSITION Find(
ARG_TYPE searchValue,
POSITION startAfter = NULL) const;
ARG_TYPE
Template parameter specifying the type of the list element (can be a reference).
searchValue
The value to be found in the list.
startAfter
The start position for the search. If no value is specified, the search begins with the head element.
A POSITION
value that can be used for iteration or object pointer retrieval; NULL
if the object is not found.
[!code-cppNVC_MFCCollections#39]
Uses the value of nIndex
as an index into the list.
POSITION FindIndex(INT_PTR nIndex) const;
nIndex
The zero-based index of the list element to be found.
A POSITION
value that can be used for iteration or object pointer retrieval; NULL
if nIndex
is negative or too large.
It starts a sequential scan from the head of the list, stopping on the nth element.
[!code-cppNVC_MFCCollections#40]
Gets the list element at a given position.
TYPE& GetAt(POSITION position);
const TYPE& GetAt(POSITION position) const;
TYPE
Template parameter specifying the type of object in the list.
position
The position in the list of the element to get.
See the return value description for GetHead
.
GetAt
returns the element (or a reference to the element) associated with a given position. It is not the same as an index, and you cannot operate on a POSITION
value yourself. A variable of type POSITION
is a key for the list.
You must ensure that your POSITION
value represents a valid position in the list. If it is invalid, then the Debug version of the Microsoft Foundation Class Library asserts.
See the example for CList::GetHeadPosition
.
Gets the number of elements in this list.
INT_PTR GetCount() const;
An integer value containing the element count.
Calling this method will generate the same result as the CList::GetSize
method.
See the example for CList::RemoveHead
.
Gets the head element (or a reference to the head element) of this list.
const TYPE& GetHead() const;
TYPE& GetHead();
TYPE
Template parameter specifying the type of object in the list.
If the list is const
, GetHead
returns a copy of the element at the head of the list. This allows the function to be used only on the right side of an assignment statement and protects the list from modification.
If the list is not const
, GetHead
returns a reference to the element at the head of the list. This allows the function to be used on either side of an assignment statement and thus allows the list entries to be modified.
You must ensure that the list is not empty before calling GetHead
. If the list is empty, then the Debug version of the Microsoft Foundation Class Library asserts. Use IsEmpty
to verify that the list contains elements.
[!code-cppNVC_MFCCollections#41]
Gets the position of the head element of this list.
POSITION GetHeadPosition() const;
A POSITION
value that can be used for iteration or object pointer retrieval; NULL
if the list is empty.
[!code-cppNVC_MFCCollections#42]
Gets the list element identified by rPosition
, then sets rPosition
to the POSITION
value of the next entry in the list.
TYPE& GetNext(POSITION& rPosition);
const TYPE& GetNext(POSITION& rPosition) const;
TYPE
Template parameter specifying the type of the elements in the list.
rPosition
A reference to a POSITION
value returned by a previous GetNext
, GetHeadPosition
, or other member function call.
If the list is const
, GetNext
returns a copy of an element of the list. This allows the function to be used only on the right side of an assignment statement and protects the list from modification.
If the list is not const
, GetNext
returns a reference to an element of the list. This allows the function to be used on either side of an assignment statement and thus allows the list entries to be modified.
You can use GetNext
in a forward iteration loop if you establish the initial position with a call to GetHeadPosition
or Find
.
You must ensure that your POSITION
value represents a valid position in the list. If it is invalid, then the Debug version of the Microsoft Foundation Class Library asserts.
If the retrieved element is the last in the list, then the new value of rPosition
is set to NULL.
[!code-cppNVC_MFCCollections#43]
Gets the list element identified by rPosition
, then sets rPosition
to the POSITION
value of the previous entry in the list.
TYPE& GetPrev(POSITION& rPosition);
const TYPE& GetPrev(POSITION& rPosition) const;
TYPE
Template parameter specifying the type of the elements in the list.
rPosition
A reference to a POSITION
value returned by a previous GetPrev
or other member function call.
If the list is const
, GetPrev
returns a copy of the element at the head of the list. This allows the function to be used only on the right side of an assignment statement and protects the list from modification.
If the list is not const
, GetPrev
returns a reference to an element of the list. This allows the function to be used on either side of an assignment statement and thus allows the list entries to be modified.
You can use GetPrev
in a reverse iteration loop if you establish the initial position with a call to GetTailPosition
or Find
.
You must ensure that your POSITION
value represents a valid position in the list. If it is invalid, then the Debug version of the Microsoft Foundation Class Library asserts.
If the retrieved element is the first in the list, then the new value of rPosition
is set to NULL
.
[!code-cppNVC_MFCCollections#44]
Returns the number of list elements.
INT_PTR GetSize() const;
The number of items in the list.
Call this method to retrieve the number of elements in the list. Calling this method will generate the same result as the CList::GetCount
method.
[!code-cppNVC_MFCCollections#45]
Gets the CObject
pointer that represents the tail element of this list.
TYPE& GetTail();
const TYPE& GetTail() const;
TYPE
Template parameter specifying the type of elements in the list.
See the return value description for GetHead
.
You must ensure that the list is not empty before calling GetTail
. If the list is empty, then the Debug version of the Microsoft Foundation Class Library asserts. Use IsEmpty
to verify that the list contains elements.
[!code-cppNVC_MFCCollections#46]
Gets the position of the tail element of this list; NULL
if the list is empty.
POSITION GetTailPosition() const;
A POSITION
value that can be used for iteration or object pointer retrieval; NULL
if the list is empty.
[!code-cppNVC_MFCCollections#47]
Adds an element to this list after the element at the specified position.
POSITION InsertAfter(POSITION position, ARG_TYPE newElement);
position
A POSITION value returned by a previous GetNext
, GetPrev
, or Find
member function call.
ARG_TYPE
Template parameter specifying the type of the list element.
newElement
The element to be added to this list.
A POSITION
value that can be used for iteration or list element retrieval.
[!code-cppNVC_MFCCollections#48]
Adds an element to this list before the element at the specified position.
POSITION InsertBefore(POSITION position, ARG_TYPE newElement);
position
A POSITION
value returned by a previous GetNext
, GetPrev
, or Find
member function call.
ARG_TYPE
Template parameter specifying the type of the list element (can be a reference).
newElement
The element to be added to this list.
A POSITION
value that can be used for iteration or list element retrieval.
If position
is NULL
, the element is inserted at the head of the list.
[!code-cppNVC_MFCCollections#49]
Indicates whether this list contains no elements.
BOOL IsEmpty() const;
Nonzero if this list is empty; otherwise 0.
[!code-cppNVC_MFCCollections#50]
Removes all the elements from this list and frees the associated memory.
void RemoveAll();
No error is generated if the list is already empty.
[!code-cppNVC_MFCCollections#51]
Removes the specified element from this list.
void RemoveAt(POSITION position);
position
The position of the element to be removed from the list.
You must ensure that your POSITION
value represents a valid position in the list. If it is invalid, then the Debug version of the Microsoft Foundation Class Library asserts.
[!code-cppNVC_MFCCollections#52]
Removes the element from the head of the list and returns a pointer to it.
TYPE RemoveHead();
TYPE
Template parameter specifying the type of elements in the list.
The element previously at the head of the list.
You must ensure that the list is not empty before calling RemoveHead
. If the list is empty, then the Debug version of the Microsoft Foundation Class Library asserts. Use IsEmpty
to verify that the list contains elements.
[!code-cppNVC_MFCCollections#53]
Removes the element from the tail of the list and returns a pointer to it.
TYPE RemoveTail();
TYPE
Template parameter specifying the type of elements in the list.
The element that was at the tail of the list.
You must ensure that the list is not empty before calling RemoveTail
. If the list is empty, then the Debug version of the Microsoft Foundation Class Library asserts. Use IsEmpty
to verify that the list contains elements.
[!code-cppNVC_MFCCollections#54]
A variable of type POSITION
is a key for the list.
void SetAt(POSITION pos, ARG_TYPE newElement);
pos
The POSITION
of the element to be set.
ARG_TYPE
Template parameter specifying the type of the list element (can be a reference).
newElement
The element to be added to the list.
It is not the same as an index, and you cannot operate on a POSITION
value yourself. SetAt
writes the element to the specified position in the list.
You must ensure that your POSITION
value represents a valid position in the list. If it is invalid, then the Debug version of the Microsoft Foundation Class Library asserts.
[!code-cppNVC_MFCCollections#55]
MFC Sample COLLECT
CObject
Class
Hierarchy Chart
CMap
Class
CArray
Class