Starting Out With C++(7.5)

I've been reading the "Starting Out With C++" book and I have a question. Here is the paragraph:


"The Range-Based for Loop versus the Regular for Loop

The range-based for loop can be used in any situation where you need to
step through the elements of an array, and you do not need to use the
element subscripts. It will not work, however, in situations where you
need the element subscript for some purpose. In those situations, you need
to use the regular for loop."


My question is when will I need to use element subscripts and why won't a "Range-Based for Loop" work with subscripts?

Thanks ahead of time guys!
An example of needing the subscript is when you're comparing an element of an array with the next element. e.g. if (val[i] < val[i+1]).

why won't a "Range-Based for Loop" work with subscripts?
The code inside a range-based for loop doesn't have access to the element's subscript. It may be better to think of it as "a range based for loop doesn't used an array's subscript" rather than thinking that it won't work with the subscript.
when will I need to use element subscripts
Everytime when you need to find index of element (or iterator, as it does not work with iterators either), or need to have free access to next/precious/etc. values inside the loop.

why won't a "Range-Based for Loop" work with subscripts?
Because it is by definition operates on container valuies without prociding access to their indexes(which might not exist)/iterators(which might be input iterators).
So comparing and testing arrays will need a subscript and will require a "Regular for Loop" and a "Ranged-Based for Loop" is best used to display the contents in a single array? @dhayden @MiiNiPaa
Last edited on
Range-based loop is used when all you care is values, not their position, not their neighbours, etc. For example finding sum of all array elements, copying all negative numbers to another array, displaying only those solutions which satisfies other requirements...

Regular loop should be used when range-based one is not enough.

Additionally you should study what <algorithm> header has to offer. If covers about 75% of common loop usage.
@MiiNiPaa Thanks for clearing that up, makes sense now.
It will not work, however, in situations where you
need the element subscript for some purpose.
not true. it can be make work. (inconvenient - but that's other thing)

example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//
#include <iostream>
using namespace std;

int main()
{
	int ar[] = {1,3,2,4,6,5};

	cout << &ar[0] << "\n\n";

	for(auto &x: ar)
	{
		if(*&x < *(&x+1))
		cout << x << " " << &x << endl;
	}
	cout << "\n\n";	
	
return 0;
}
Your example does not get element subscript and fails miserably if someone would switch array for, say, linked list.

It is possible to get element subscript/iterator in many cases by sacrificing generality and/or speed/cleanness

Instead of fighting language feature which does not suit you it is better to use right features from the beginning.
you are right MinniPaa.
i meant
It will not work,
is wrong term.

such as, many things could be done in javascript much more easily than C++,
but that does not mean C++ cant do that.

writer should have used
inconvenient
instead of meaning
impossible
Topic archived. No new replies allowed.