DJ_AM_Juicebox
asked on
interacting with elements of a templated container
Hi,
I made a templated container class for testing out templates. It is a simple doubly linked list. Previously I had specified the exact type the container used so there were class specific operations in there, one of which was dumping the contents of the container (just pseudo code):
class CLinkedList {
CSpecificClass *m_pLinkedList;
void DumpToScreen()
{
while (m_pLinkedList) {
next->PrintContents();
}
};
Now CLinkedList uses templates for the data it stores and so what can I do for an operation to dump all the contents of the list since I don't know what the user is going to stick in there? I can't do anything really, can I? I'd have to do it externally now like:
int main()
{
CLinkedList<CWhatever> list;
list.AddNode(...);
list.AddNode(...);
list.AddNode(...);
// Iterate through each element somehow out here since I know the type only outside, not internally:
while (somehow iterate externally over the list's elements) {
*it->Print();
}
}
I guess this is why stl implemented the iterators, I'd have to do the same thing?
Thanks
I made a templated container class for testing out templates. It is a simple doubly linked list. Previously I had specified the exact type the container used so there were class specific operations in there, one of which was dumping the contents of the container (just pseudo code):
class CLinkedList {
CSpecificClass *m_pLinkedList;
void DumpToScreen()
{
while (m_pLinkedList) {
next->PrintContents();
}
};
Now CLinkedList uses templates for the data it stores and so what can I do for an operation to dump all the contents of the list since I don't know what the user is going to stick in there? I can't do anything really, can I? I'd have to do it externally now like:
int main()
{
CLinkedList<CWhatever> list;
list.AddNode(...);
list.AddNode(...);
list.AddNode(...);
// Iterate through each element somehow out here since I know the type only outside, not internally:
while (somehow iterate externally over the list's elements) {
*it->Print();
}
}
I guess this is why stl implemented the iterators, I'd have to do the same thing?
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yep that's perfect, thanks as always.
Hi DJ_AM_Juicebox,
> I guess this is why stl implemented the iterators, I'd have to do the same thing?
>>I guess this is why stl implemented the iterators, I'd have to do the same thing?
Are you sure you need a link list. Rarely is a link list the correct container type, and for most requirements, a std::vector type contianer is much more efficient and more appropriate.
Moreover, with a std::vector, you can use operator[] instead of iterators.
David Maisonave (Axter)
Cheers!
> I guess this is why stl implemented the iterators, I'd have to do the same thing?
>>I guess this is why stl implemented the iterators, I'd have to do the same thing?
Are you sure you need a link list. Rarely is a link list the correct container type, and for most requirements, a std::vector type contianer is much more efficient and more appropriate.
Moreover, with a std::vector, you can use operator[] instead of iterators.
David Maisonave (Axter)
Cheers!
CNode<T>* First() { m_pCurrent = m_pNodes; return m_pCurrent;}
CNode<T>* Next() {
if (!m_pCurrent) return NULL;
m_pCurrent = m_pCurrent->m_pNext;
return m_pCurrent;
}