Help with iterator

Hi im trying to use iterator in line 41 of my code but it dont work please help.
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
51
52
53
#include <iostream>
#include <list>
#include <iterator>

class neuron{
public:
    double w;
    std::list<neuron*> frn;
    ~neuron(){while(!frn.empty()) delete frn.front(), frn.pop_front();}
};

class layer{
public:
    unsigned short n;
    std::list<neuron*> dom;
    layer(unsigned short);
    ~layer(){while(!dom.empty()) delete dom.front(), dom.pop_front();}
};

layer::layer(unsigned short a){
    n = a;
    for(unsigned short i; i < a; i++){
        dom.push_back(new neuron);
    };
};

int main()
{

    std::list<layer*> neuron_net;

    unsigned short ailayers = 9;
    for(unsigned short i = 0; i < ailayers; i++){
        neuron_net.push_back(new layer(i));
    };

    for(std::list<layer*>::iterator it = neuron_net.begin(); it != neuron_net.end(); it++){

        std::cout<<"\n neuron_net->layer->n = "<<(*it)->n;

        for(std::list<neuron*>::iterator jt = (*it)->dom.begin(); jt != (*it)->dom.end(); jt++){
            (*jt)->w = 1.2345;
            std::cout<<"\n neuron_net->layer->dom->w = "<<(*jt)->w;
        };

    };

    while(!neuron_net.empty()){delete neuron_net.front(); neuron_net.pop_front();};//delete pointers from list

    std::cin>>ailayers;//system pause

    return 0;
};

It looks like the inner one isnt populated and the inner for is being skipped.
I don't see why right off, though.
found it.
you forgot to initialize i in your for loop. (22)
Last edited on
Also note that using auto makes the code much easier. eg:

 
for(auto it = neuron_net.begin(); it != neuron_net.end(); it++){


 
for(auto jt = (*it)->dom.begin(); jt != (*it)->dom.end(); jt++){

one could also do these things:

* use a range based for loop, not iterators
* avoid using new, so no pointers, no need for delete
* pass by reference
It has the 'smell' of C++98 code...
Thank all you for replies.
Thank you jonnin. That fixed it. Also looks like i'm blind.
Instead of having a C++ container contain raw pointers consider using smart pointers. std::unique or std::shared. Smart pointers manage the memory for you, no need for new or delete.
https://en.cppreference.com/w/cpp/memory

https://www.internalpointers.com/post/beginner-s-look-smart-pointers-modern-c

There are helper non-member functions to create unique and shared pointers from an object.
https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique
https://en.cppreference.com/w/cpp/memory/shared_ptr/make_shared

Passing smart pointers into functions and returning a smart pointer:

https://www.internalpointers.com/post/move-smart-pointers-and-out-functions-modern-c
Since C++17,
struct A { std::list<A> as; };
is guaranteed to work.
Last edited on
Possibly something like:

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
#include <iostream>
#include <list>

class Neuron {
public:
	double w {};
	std::list<Neuron> frn;
};

class Layer {
public:
	unsigned short n {};
	std::list<Neuron> dom;

	Layer(unsigned short a) : n(a), dom(a, Neuron {}) {}
};

int main() {
	const unsigned short ailayers { 9 };

	std::list<Layer> neuron_net;

	for (unsigned short i {}; i < ailayers; neuron_net.emplace_back(i++));

	for (auto& [n, dom] : neuron_net) {
		std::cout << "\n neuron_net->layer->n = " << n;

		for (auto& [w, frn] : dom) {
			w = 1.2345;
			std::cout << "\n neuron_net->layer->dom->w = " << w;
		}
	}
}

Topic archived. No new replies allowed.