Solved

Iterator

Posted on 2000-03-29
5
442 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 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…

632 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