Nested For Each Loop and Nest For loop with std::list

I am starting to use the standard template library.

I can write the nested for loop working on an array as shown below.

a) When using a list<double> structure to do the same thing how do I set it2=it+1 to start the innner loop?

b) How can I do the same thing using a for_each call on a list<double> structure?

c) Any other comments on how to make this better?

The code I am using is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <list>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	cout << "Easy nested for loop an working on an array" << endl;
	double lista[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

	for (int i=0;i<10;i++) {
		for (int j=i+1;j<10;j++) {
			cout << lista[i] << "," << lista[j] << " ";
		}
		cout << endl;
	}

	list<double> listb;
	listb.push_back(0);
	listb.push_back(1);
	listb.push_back(2);
	listb.push_back(3);
	listb.push_back(4);
	listb.push_back(5);
	listb.push_back(6);
	listb.push_back(7);
	listb.push_back(8);
	listb.push_back(9);

	cout << "Nested for loop working on a list" << endl;
	list<double>::iterator it;
	for (it=listb.begin(); it != listb.end(); it++) {
		list<double>::iterator it2;
		// how do I start it2 at it+1?
		for (it2=it; it2 != listb.end(); it2++) {
			cout << *it << "," << *it2 << " ";
		}
		cout << endl;
	}

	cout << "Nested For each loop working on a list" << endl;
	for_each( listb.begin(), listb.end(), [&](double n) {
		// How do I do this the same as the nested for loop above???
		cout << n;
	});

	return 0;
}


With the output:

Easy nested for loop an working on an array
0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9
1,2 1,3 1,4 1,5 1,6 1,7 1,8 1,9
2,3 2,4 2,5 2,6 2,7 2,8 2,9
3,4 3,5 3,6 3,7 3,8 3,9
4,5 4,6 4,7 4,8 4,9
5,6 5,7 5,8 5,9
6,7 6,8 6,9
7,8 7,9
8,9

Nested for loop working on a list
0,0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9
1,1 1,2 1,3 1,4 1,5 1,6 1,7 1,8 1,9
2,2 2,3 2,4 2,5 2,6 2,7 2,8 2,9
3,3 3,4 3,5 3,6 3,7 3,8 3,9
4,4 4,5 4,6 4,7 4,8 4,9
5,5 5,6 5,7 5,8 5,9
6,6 6,7 6,8 6,9
7,7 7,8 7,9
8,8 8,9
9,9
Nested For each loop working on a list
0123456789


Thanks for any assistance.
std::list iterators don't have the + operator but they have ++ and --:

1
2
3
4
5
6
7
8
9
list<double>::iterator it;
for (it=listb.begin(); it != listb.end(); it++) {
    list<double>::iterator it2 = it; // initialize it2 = it

    for ( ++it2 /*increase it2*/; it2 != listb.end(); it2++) {
        cout << *it << "," << *it2 << " ";
    }
    cout << endl;
}


If you want an interface similar to the built-in arrays you should try std::vector or std::deque, the support subscripting [ ] and their iterators have the + operator overloaded

For the for each:
1
2
3
4
for_each( listb.begin(), listb.end(), [&](double n) {
	// How do I do this the same as the nested for loop above???
	cout << n;
});
Lambda expressions aren't standard yet. Define a function instead

Hi Bazzy,

I need to stick to the std::list. This is a portion of what I am doing. Outside of this I wil be doing a lot of changes to the items in the list (adding and removing items). What I need is how to iterate through all items in a list in conjunction with all items in the list after the current item. Essentially I am looking for all possible combinations of items in the list.

Is this possible with either the for_each function or for function operating on a list?

Thanks,

LasCondes
Hi Bazzy,

Just went through your post in detail. The nested for on the std::list is now working. Thank you.

The for each will take me more time to look at.

Regards,

LasCondes
Topic archived. No new replies allowed.