Solved

Iterator

Posted on 2000-03-29
5
436 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 5 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

829 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