Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Iterator

Posted on 2000-03-29
5
Medium Priority
?
447 Views
Last Modified: 2013-12-14
What is an iterator in C++ and where it can be used ?
Give a simple example.
0
Comment
Question by:ajaysoni032800
5 Comments
 
LVL 1

Accepted Solution

by:
ntdragon earned 10 total points
ID: 2668462
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
 
LVL 3

Expert Comment

by:MDarling
ID: 2668823
homework?
0
 
LVL 33

Expert Comment

by:hongjun
ID: 2668869
MDarling,
Obviously this is a specific question and definitely not a assignment.

hongjun
0
 
LVL 4

Expert Comment

by:captainkirk
ID: 2669597
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
 
LVL 4

Expert Comment

by:captainkirk
ID: 2669607
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question