234k views
4 votes
Throughout the semester we have looked at a number of C++ and Java examples. We have seen examples of iterators in both languages. You may have used iterators in other languages (e.g., Python or PHP). Briefly describe how iterators allow us to develop our ADT interfaces and work with ADT interfaces provided by others (e.g., the std::vector in C++ or java.util.ArrayList in Java).

1 Answer

3 votes

Answer:

Check the explanation

Step-by-step explanation:

Iterators in C++

==================

Iterators are used to point at the memory addresses of STL containers. They are primarily used in sequence of numbers, characters etc. They reduce the complexity and execution time of program.

Operations of iterators :-

1. begin() :- This function is used to return the beginning position of the container.

2. end() :- This function is used to return the after end position of the container.

// C++ code to demonstrate the working of

// iterator, begin() and end()

#include<iostream>

#include<iterator> // for iterators

#include<vector> // for vectors

using namespace std;

int main()

{

vector<int> ar = { 1, 2, 3, 4, 5 };

// Declaring iterator to a vector

vector<int>:: iterator ptr;

// Displaying vector elements using begin() and end()

cout << "The vector elements are : ";

for (ptr = ar. begin(); ptr < ar. end(); ptr++)

cout << *ptr << " ";

return 0;

}

Iterators in Java

==================

‘Iterator’ is an interface which belongs to collection framework. It allows us to traverse the collection, access the data element and remove the data elements of the collection.

java. util package has public interface Iterator and contains three methods:

boolean hasNext(): It returns true if Iterator has more element to iterate.

Object next(): It returns the next element in the collection until the hasNext()method return true. This method throws ‘NoSuchElementException’ if there is no next element.

void remove(): It removes the current element in the collection. This method throws ‘IllegalStateException’ if this function is called before next( ) is invoked.

// Java code to illustrate the use of iterator

import java. io.*;

import java. util.*;

class Test {

public static void main(String[] args)

{

ArrayList<String> list = new ArrayList<String>();

list. add("A");

list. add("B");

list. add("C");

list. add("D");

list. add("E");

// Iterator to traverse the list

Iterator iterator = list. iterator();

System. out. println("List elements : ");

while (iterator. hasNext())

System. out. print(iterator. next() + " ");

System. out. println();

}

}

Every class that implements Iterable interface appropriately, can be used in the enhanced For loop (for-each loop). The need to implement the Iterator interface arises while designing custom data structures.

Example:

for(Item item: customDataStructure) {

// do stuff

}

To implement an iterable data structure, we need to:

Implement Iterable interface along with its methods in the said Data Structure

Create an Iterator class which implements Iterator interface and corresponding methods.

We can generalize the pseudo code as follows:

class CustomDataStructure implements Iterable<> {

// code for data structure

public Iterator<> iterator() {

return new CustomIterator<>(this);

}

}

class CustomIterator<> implements Iterator<> {

// constructor

CustomIterator<>(CustomDataStructure obj) {

// initialize cursor

}

// Checks if the next element exists

public boolean hasNext() {

}

// moves the cursor/iterator to next element

public T next() {

}

// Used to remove an element. Implement only if needed

public void remove () {

// Default throws Unsupported Operation Exception.

}

}

Note: The Iterator class can also, be implemented as an inner class of the Data Structure class since it won’t be used elsewhere.

User Jigsore
by
5.3k points