Sep 9, 2011

Iterator in C++

Iterator an object which help in traversing a container. Its like a navigator.
For beginners its very difficult to digest/learn Iterator after Array. Since access to array is very easy just pass an index and get the value.
So this post will help beginners to build understanding about Iterator.

To start with lets look out for simple array traversal.

const int nLength = 10;
int account[nLength] = {0};
//to traverse the array one can simply provide for loop upto length and access it by index
for(int i = 0; i< nLength; i++)
{
cout<<account[i];
}

Now lets solve the above problem if its a list container.

std::list lstAccount; //list container for account
stAccount.push_back(1);
lstAccount.push_back(2);
lstAccount.push_back(3);
lstAccount.push_back(4);
std::list::iterator lstAccountIterator;//iterator for traversing list
for(lstAccountIterator = lstAccount.begin(); //initialize iterator with begining
lstAccountIterator != lstAccount.end(); // traverse until iterator rach end
lstAccountIterator++) //move itertor to next element.
{
cout<<*lstAccountIterator; //print value at itertor
}

So above code will help in traversing a list container.
Some good article on Iterator can be found at following links
       http://www.cplusplus.com/reference/std/iterator/
       http://www.cprogramming.com/tutorial/stl/iterators.html
       http://en.wikipedia.org/wiki/Iterator

Now moving to next level, lets understand the iterator design pattern.

Purpose:
       To provide a way for accessing and traversing the collection of elements, without actually exposing the internal structure/representation of the collection organisation.


Benefits:
     An abstraction which helps in simplifying the traversal mechanism.

Design Layout:
Fig. Iterator Design Pattern
   





74 comments:

Writing this on one query of a newbie, who is facing problem in printing the list :-

lets assume one class say Account, which has methods GetName.

Assuming a list of account
std::list lstAccount;

Account aStudent("Jhony");
lstAccount.push_back(aStudent);

now for printing this list, define an iterator
std::list::iterator lstAccountIterator ;

then defining a loop to traverse it
for(lstAccountIterator = lstAccount.begin(); //initialize iterator with begining
lstAccountIterator != lstAccount.end(); // traverse until iterator rach end
lstAccountIterator++) //move itertor to next element.
{
cout<<(*lstAccountIterator).GetName(); //print value at itertor
}

Repairing of Lenovo laptop are very well done by us as we have experts that are well versed with the technology used in Lenovo laptop to build them. Get in touch with us for swift & skilled help in repairing your Lenovo laptop system. Lenovo service center in Gurgaon. Call us: 7217871051
Lenovo Service Center in Gurgaon

Repairing of Lenovo laptop are very well done by us as we have experts that are well versed with the technology used in Lenovo laptop to build them. Get in touch with us for swift & skilled help in repairing your Lenovo laptop system. Lenovo service center in Delhi. Call us: 7217871051
Lenovo Service Center in Delhi

Repairing of hp laptop are very well done by us as we have experts that are well versed with the technology used in hp laptop to build them. Get in touch with us for swift & skilled help in repairing your hp laptop system. hp service center in Delhi. Call us: 7217871051
Hp Service Center in Delhi

Lenovo Laptop Service Center offer repairing for your laptop needs, we are experts in laptop chip level repair, call us: 7217871051
Lenovo Service Center in Ghaziabad

Lenovo Laptop Service Center offer fixing for your PC needs, we are experts in PC chip level fix, call us: 7217871051
Lenovo Service Center in Uttam Nagar

Hp Laptop Service Center offer fixing for your PC needs, we are experts in PC chip level fix, call us: 7217871051
Hp Service Center in Neta ji subhash Place

Hp Laptop Service Center offer fixing for your PC needs, we are experts in PC chip level fix, call us: 7217871051
Hp Service in Center in Shadra

Lenovo Laptop Service Center offer fixing for your PC needs, we are experts in PC chip level fix, call us: 9891868324
Lenovo Service Center in Sec-14 Gurgaon

Hp Laptop Service Center offer fixing for your PC needs, we are experts in PC chip level fix, call us: 9891868324
Hp Service Center in Palam Vihar

Lenovo Laptop Service Center offer fixing for your PC needs, we are experts in PC chip level fix, call us: 9891868324
Lenovo Service Center in Palam Vihar

Lenovo Laptop Service Center offer fixing for your PC needs, we are experts in PC chip level fix, call us: 9891868324
Lenovo Service Center in Manesar

Lenovo Laptop Service Center offer fixing for your PC needs, we are experts in PC chip level fix, call us: 9891868324
Lenovo Service Center in Medicity

Hp Laptop Service Center offer fixing for your PC needs, we are experts in PC chip level fix, call us: 9891868324
Hp Service Center in Manesar

Lenovo Laptop Service Center offer fixing for your PC needs, we are experts in PC chip level fix, call us: 9891868324
Lenovo Service Center in M.G. Road

Lenovo Laptop Service Center offer fixing for your PC needs, we are experts in PC chip level fix, call us: 9891868324
Lenovo Service Center in Ghaziabad

Hp Laptop Service Center offer fixing for your PC needs, we are experts in PC chip level fix, call us: 9891868324
Hp Service Center in Ghaziabad

Hp Laptop Service Center offer fixing for your PC needs, we are experts in PC chip level fix, call us: 9891868324
Hp Service Center in DLF Garden City

Hp Laptop Service Center offer fixing for your PC needs, we are specialists in PC chip level fix, call us: 9891868324
Hp Service Center in Mangolpuri

Hp Laptop Service Center offer fixing for your PC needs, we are specialists in PC chip level fix, call us: 9891868324
Hp Service Center in Sohna-Road

Hp Laptop Service Center offer fixing for your PC needs, we are specialists in PC chip level fix, call us: 9891868324
Hp service center in Goregaon

Hp Laptop Service Center offer fixing for your PC needs, we are specialists in PC chip level fix, call us: 9891868324
Hp service center in Borivali

Hp Laptop Service Center offer fixing for your PC needs, we are specialists in PC chip level fix, call us: 9891868324
Hp Service Center in Andheri West

Hp Laptop Service Center offer fixing for your PC needs, we are specialists in PC chip level fix, call us: 9891868324
Hp Service in Center in Malad

Lenovo Laptop Service Center offer fixing for your PC needs, we are specialists in PC chip level fix, call us: 9891868324.
Lenovo service center in Andheri West

Lenovo Laptop Service Center offer fixing for your PC needs, we are specialists in PC chip level fix, call us: 9891868324.
Lenovo service center in Borivali

Lenovo Laptop Service Center offer fixing for your PC needs, we are experts in PC chip level fix, call us: 9891868324
Lenovo Service Center in Hero Honda Chowk

Hp Laptop Service Center offer fixing for your PC needs, we are specialists in PC chip level fix, call us: 9891868324
Hp Service Center in Malad

Hp Laptop Service Center offer fixing for your PC needs, we are experts in PC chip level fix, call us: 9891868324
Hp Service in Center in Santacruz

Hp Laptop Service Center offer fixing for your PC needs, we are experts in PC chip level fix, call us: 9891868324
Hp Service Center in Rohini

Lenovo Laptop Service Center offer fixing for your PC needs, we are experts in PC chip level fix, call us: 9891868324
Lenovo Service Center in Rohini

Hp Laptop Service Center offer repairing for your laptop needs, we are experts in laptop chip level repair, call us: 9891868324
Hp Service Center in Paschim Vihar

Lenovo Laptop Service Center offer fixing for your PC needs, we are specialists in PC chip level fix, call us: 9891868324
Lenovo Service Center in Chattarpur

Hp Laptop Service Center offer fixing for your PC needs, we are specialists in PC chip level fix, call us: 9891868324
Hp Service Center in Chattarpur

Dell Laptop Service Center offer fixing for your PC needs, we are specialists in PC chip level fix, call us: 9891868324
Dell Service Center in Rajouri Garden

Dell Laptop Service Center offer fixing for your PC needs, we are specialists in PC chip level fix, call us: 9891868324
Dell Service Center in Dwarka

Dell Laptop Service Center offer fixing for your PC needs, we are specialists in PC chip level fix, call us: 9891868324
Dell Service Center in Lajpat-Nagar

Lenovo Laptop Service Center offer repairing for your laptop needs, we are experts in laptop chip level repair, call us: 9891868324
Lenovo Service Center in Nehru-Place

Hp Laptop Service Center offer repairing for your laptop needs, we are experts in laptop chip level repair, call us: 9891868324
Hp Service Center in Pitampura

Hp Laptop Service Center offer fixing for your PC needs, we are specialists in PC chip level fix, call us: 9891868324.
Hp Service Center in Mangolpuri

Lenovo Laptop Service Center offer fixing for your PC needs, we are specialists in PC chip level fix, call us: 9891868324
Lenovo Service Center in Mangolpuri

Hp Laptop Service Center offer fixing for your PC needs, we are experts in PC chip level fix, call us: 9891868324.
Hp Service in Center in Okhla

Lenovo Laptop Service Center offer fixing for your PC needs, we are experts in PC chip level fix, call us: 9891868324.
Lenovo Service in Center in Okhla

Lenovo Laptop Service Center offer fixing for your PC needs, we are experts in PC chip level fix, call us: 9891868324.
Lenovo Service Center in Greater Noida

Hp Laptop Service Center offer fixing for your PC needs, we are experts in PC chip level fix, call us: 9891868324.
Hp Service in Center in Greater Noida



Hp Laptop Service Center offer fixing for your PC needs, we are specialists in PC chip level fix, call us: 7042640351.
Hp Service Center in Chattarpur



Lenovo Laptop Service Center offer fixing for your PC needs, we are specialists in PC chip level fix, call us: 7042640351.
Lenovo Service Center in Chattarpur


The school maintains the authors' obligation to greatness in all fields, with accentuation on its maxim Higher Stronger Brighter. High ethics are an essential of a decent individual. Contact on us: 8130269997.
top 10 schools in rohini

The school keeps up the creators' commitment to significance in all fields, with complement on its saying Higher Stronger Brighter. High morals are a fundamental of a nice person. Contact on us: 8130269997.
cbse school in Rohini

The school keeps up the creators' commitment to significance in all fields, with complement on its saying Higher Stronger Brighter. High morals are a fundamental of a nice person. Contact on us: 8130269997.
Top Schools in Rohini

Arising as a broadly perceived promoting organization in all features of publicizing and marking arrangements centering in outside, on the web and electronic media spaces. Contact on us: 9868946230.
Outdoor advertising

Arising as a broadly perceived promoting organization in all features of publicizing and marking arrangements centering in outside, on the web and electronic media spaces. Contact on us: 9868946230.
Hoarding advertising

It is attempt of Mast Banarasi Paan Company to construct a chain of establishment outlets in a large portion of the urban areas of India to guarantee that heavenly Banarasi Paan is made accessible to each client with a similar flavor, taste, quality and cleanliness and we are the best paan supplier in INDIA
Paan CafƩ

It is attempt of Mast Banarasi Paan Company to construct a chain of establishment outlets in a large portion of the urban areas of India to guarantee that heavenly Banarasi Paan is made accessible to each client with a similar flavor, taste, quality and cleanliness and we are the best paan supplier in INDIA
Flavour Paan

Lenovo Laptop Service Center offer repairing for your laptop needs, we are experts in laptop chip level repair, call us: 7838122281.
Dell Service Center in Mumbai

The school maintains the authors' obligation to greatness in all fields, with accentuation on its maxim Higher Stronger Brighter. High ethics are an essential of a decent individual. Contact on us: 8130269997.
GD GOENKA
Top Schools in Rohini
cbse school in Rohini

Hp Laptop Service Center offer repairing for your laptop needs, we are experts in laptop chip level repair, call us: 7838446546.
Hp Service Center in Mumbai

Dell Laptop Service Center offer repairing for your laptop needs, we are experts in laptop chip level repair, call us: 7838446546.
Dell Service Center in Mumbai

The school maintains the authors' obligation to greatness in all fields, with accentuation on its maxim Higher Stronger Brighter. High ethics are an essential of a decent individual. Contact on us: 8130269997.
Nursery Admissions 2021-2022
Best Schools in Delhi
GD GOENKA Rohini

Arising as a broadly perceived promoting organization in all features of publicizing and marking arrangements centering in outside, on the web and electronic media spaces. Contact on us: 9868946230.
Outdoor Advertising Companies in Delhi

Arising as a broadly perceived promoting organization in all features of publicizing and marking arrangements centering in outside, on the web and electronic media spaces. Contact on us: 9868946230.
Hoardings in Delhi

The Company eyes the vision of getting one of the India's notable and rumored Ad Agency by satisfying our customer's requirements and giving our significant customers the best quality outcomes utilizing our ability and rich encounters of publicizing area. We have practical experience in vital and inventive Brand Promotion arrangements in OOH.
Hoardings in Delhi
Rural wall painting advertising
Digital wall painting
Transit Media Service

G.D. Goenka Public School Rohini Sector- 9 is a Co-educational Senior Secondary School, recognized and affiliated to the Central Board of Secondary Education (CBSE). It has classes from Preschool till Class 12th. The school upholds the founders' commitment to excellence in all fields, with emphasis on its motto Higher Stronger Brighter. High morals are a prerequisite of a good human being.
top 10 schools in rohini
cbse school in Delhi
Nursery Admissions 2021-2022

Arising as a broadly perceived promoting organization in all features of publicizing and marking arrangements centering in outside, on the web and electronic media spaces. Contact on us: 9868946230.
Hoardings in Delhi
Digital wall painting
Auto Rickshaw advertising

Four Square Media Services is engineered in Delhi NCR. The Company eyes the vision of getting one of the India's striking and assumed Outdoor Advertising Companies in Delhi by fulfilling our client's basics and giving our colossal clients the best quality results using our ability and rich experiences of advancing. Contact on us: 9868946230.
Hoardings in Delhi
Digital wall painting
Advertising wall painting

Bs system solution in Delhi provides the best solution for laptop repair. Our experienced engineers take complete care and responsibility for your device and provide you a high-quality solution. The Lenovo Laptop Technical support is available for all range of customers. IF YOU ARE INTERESTED YOU MAY CALL -9891868324, 9643406897, and 9643409808
laptop repairing center in Delhi
best laptop service center in Delhi

Bs system solution in Delhi provides the best solution for laptop repair. Our experienced engineers take complete care and responsibility for your device and provide you a high-quality solution. The Lenovo Laptop Technical support is available for all range of customers. IF YOU ARE INTERESTED YOU MAY CALL -9891868324, 9643406897, and 9643409808
laptop service in Delhi
laptop reparing center in Mumbai

Spotting the best wedding photographer in Kolkata can be quite a hectic task. Here’s Crazy lens photography to end your quest. We specialize in a fun and candid style of wedding photography, which can be customized according to your needs.
best pre wedding photographer in kolkata
pre wedding photography in kolkata

Klever Trade is a new-generation online stock trading platform which will change the way you trade forever. Klever Trade is a new-generation online stock trading platform which will change the way you trade forever. 02614245940
How to trade in algo
Best api for algo trading

Denex International - India's largest manufacturer and exporter of surgical disposables has a wide product base and are regularly adding new medical disposables to the list. The products include I.V. Cannula with PTFE Catheter of different gauges/ sizes, non-woven fabric products such as SURGEON'S GOWNS & APRONS, O.T. SHEETS and are manufactured under the brand name Amie IV Cannula (All Gauges). 9310148912
IV Cannula Manufacturer
IV Catheter Manufacturer

We always attempt to keep international standards in our vending machines to give the superior quality and taste of natural tea and coffee to our customers and users. We are one of the well-known providers of Coffee Machine in Gurgaon. 09873633436
Coffee Vending Machine in gurgaon
Vending Machine in Gurgaon

If you are planning to take the CELPIP test and looking forward to achieve your highest score, then there is great chances our course can really help you achieve your desired score in all four modules. In fact more than 85% of our students have achieved their dream score through our live online sessions.
celpip training near me
celpip training center near me

T-shaped front points are designed for excellent penetration of hard ice and comfort on mixed terrain, while providing solid purchase in snow
Petzl ICE AXES Quark price in Mumbai
Petzl CRAMPONS Sarken price in Mumbai

Post a Comment