Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you find the iterator in the middle of two iterators?

Tags:

c++

I'm trying to convert my implementation of quicksort into a template that can be used with other containers besides a vector.

Originally I used indexes to find the middle index, e.g. (first + last) / 2. How can I find the middle of two iterators?

like image 862
Roy Avatar asked Dec 29 '11 02:12

Roy


People also ask

Where does the iterator’s cursor point to?

When we run the above code snippet, Iterator’s Cursor points to the second element in the list as shown in the above diagram. Do this process to reach the Iterator’s Cursor to the end element of the List.

What is an iterator?

Introduction to Iterators in C++ Difficulty Level : Medium Last Updated : 26 May, 2020 An iterator is an object (like a pointer) that points to an element inside the container.

How many methods of Iterator interface are available for list iterator?

So all three methods of Iterator interface are available for ListIterator. In addition, there are six more methods. 1. Forward direction 1.2 next (): Same as next () method of Iterator. Returns the next element in the iteration. 1.3 nextIndex (): Returns the next element index or list size if the list iterator is at the end of the list.

What are the methods of iterator in Java with “C”?

Note: Here “c” is any Collection object. itr is of type Iterator interface and refers to “c”. Iterator interface defines three methods as listed below: 1. hasNext (): Returns true if the iteration has more elements. 2. next (): Returns the next element in the iteration. It throws NoSuchElementException if no more element is present.


1 Answers

std::distance can measure the distance between two iterators as efficiently as possible.

std::advance can increment an iterator as efficiently as possible.

I still wouldn't want to quicksort a linked list, though :)

like image 148
StilesCrisis Avatar answered Sep 26 '22 03:09

StilesCrisis