Solved

Iterator

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

773 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