Segfault when iterating over parameters, but not when using range based loop

How can it be that the first loop works fine, while the second loop gives a segfault. In my understanding, they should be the same.

        for(auto& param : actor->parameters()){
        for(auto it = actor->parameters().begin();it!=actor->parameters().end();++it)
            std::cout<<"Iterator Param"<<std::endl;

I want to copy parameters between the policy net and the target net, as follows:

         auto it=actor->parameters().begin();
         for(auto target_it = actor_target->parameters().begin();target_it!=actor_target->parameters().end();++target_it){

For which the python code would be:

# update target networks 
        for target_param, param in zip(self.actor_target.parameters(),
   * self.tau + * (1.0 - self.tau))

I fixed it by doing

        for(int i = 0; i<actor->parameters().size();++i){
            actor_target->parameters()[i].data().copy_(actor->parameters()[i].data()*tau_ + actor_target->parameters()[i].data()*(1.0-tau_));

Still wondering why I can’t dereference an iterator on parameters() though.