Home > Back-end >  Are there c like iterators in dart
Are there c like iterators in dart

Time:10-05

In c we have iterators that act as reference to some element in list. Assigning to the iterator it is possible to change elements inside the list. for example:

        std::map<std::pair<int,int> ,Ship> playgroundMap;
        playgroundMap.insert(std::make_pair(std::make_pair(2,2),Ship(100,Cannon(50))));
        playgroundMap.insert(std::make_pair(std::make_pair(3,3),Ship(200,Cannon(60))));
        std::cout<<"Iterators : "<<std::endl;
        auto shipPtr = playgroundMap.find(std::make_pair(2,2));
        std::cout<<" address of  ship Before                 : "<<&shipPtr->second<<std::endl;
        shipPtr->second.cannon.firepower = 1000;
        auto tmp = Ship(200,Cannon(90));
        shipPtr->second = tmp;
        std::cout<<" address of  ship After newly Assigned   : "<<&shipPtr->second<<std::endl;

        std::cout<<"finalList : "<<std::endl;
        for(auto a : playgroundMap)
        {
            std::cout<<a.second.durability<<std::endl<<a.second.cannon.firepower<<std::endl;
        }

Let alone the fact that reference in c are enough to achieve this. References in c do not rebound on assignment like they do in dart. for example in c :

        std::map<std::pair<int,int> ,Ship> playgroundMap;
        playgroundMap.insert(std::make_pair(std::make_pair(2,2),Ship(100,Cannon(50))));
        playgroundMap.insert(std::make_pair(std::make_pair(3,3),Ship(200,Cannon(60))));
        std::cout<<"Reference : "<<std::endl;
        auto &shipRef = playgroundMap[std::make_pair(2,2)];
        std::cout<<" address of  ship Before                 : "<<&shipRef<<std::endl;
        shipRef.cannon.firepower = 1000;
        auto tmp = Ship(200,Cannon(90));
        auto &tmpRef = tmp;
        std::cout<<" address of  tmpref (newly created ship) : "<<&tmpRef<<std::endl;
        shipRef = tmpRef;
        std::cout<<" address of  ship After newly Assigned   : "<<&shipRef<<std::endl;

        std::cout<<" address of  finalList : "<<std::endl;
        for(auto a : playgroundMap)
        {
            std::cout<<a.second.durability<<std::endl<<a.second.cannon.firepower<<std::endl;
        }

This would modify the list element in c while in dart the reference would rebind and element in list would not change.That is whole another thing i understand that both these concepts are different in both languages.

My question is, Is there some kind of c iterator like way in dart that allows me to do these kind of operations. I know i can just store the index but i don't want to do that.

CodePudding user response:

There is nothing like that in the platform libraries. In general, Dart iterators give access to the values of a collection, not to changing the collection itself.

One of the reasons for this restriction could be that Dart doesn't have reference variables to begin with, but nothing inherently prevents Dart from having an iterator that allows you to change the value of the current element. At least for a list, it could work. A hash set probably wouldn't work, because the changed value wouldn't be in the same place of the iteration.

All in all, it's not something the platform libraries support, or have wanted to support. The Dart platform libraries are, in most places, slightly more functional programming like than C .

  • Related