Iterator

What is an iterator in C++ and where it can be used ?
Give a simple example.
ajaysoni032800Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
ntdragonConnect With a Mentor Commented:
iterrator is a class that was made only for one operation
the iterator class is made for passing over a class
for example you made a class that is a dynamic list and an iterator class for it. The iterator class only pass over the list.

the easyest example even without class's

int *beg;//iterator
int *end;//iterator
int arr[50];//array
*beg=arr;
end=arr+50;
for(;beg!=end;++beg)
  cout<<*beg;

iterator class
should have operator++ and
operator *
0
 
MDarlingCommented:
homework?
0
 
hongjunCommented:
MDarling,
Obviously this is a specific question and definitely not a assignment.

hongjun
0
 
captainkirkCommented:
if by chance you are using STL (Standard Template Library), simple usage of built in iterators looks a bit like this:

for (YourSet::iterator it = m_YourSet.begin(); it != m_YourSet.end(); it++)
{
     pSomeStruct = *it;

     if (pSomeStruct->m_SomeMember == nSomeValue)
     {
      delete pSomeStruct;
      pSomeStruct = NULL;
      m_YourSet.erase(it);
      break;
     } // end if
} // end for

not specific to the core of C++, but it may provide some enlightenment...
0
 
captainkirkCommented:
by the way, the previous example is for a set. STL provides other collection classes that may use an iterator, such as multiset, vector, list, etc...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.