in reply to Re^3: foreach to for (related to my last question)
in thread foreach to for (related to my last question)

because I'm trying to change the language in which the algorithm is written. in c++ I don't have the use of the foreach loop, at least, the for_each loop I do have doesn't allow me to change the list.

so I can't figure out how to deal with the stuff I add or remove, so any help would be nice
  • Comment on Re^4: foreach to for (related to my last question)

Replies are listed 'Best First'.
Re^5: foreach to for (related to my last question)
by ikegami (Patriarch) on Jun 22, 2006 at 14:21 UTC

    You wouldn't use a "for" loop in C++ either. Your C++ equivalent to a hash should have an iterator, and that would be used in conjunction with a "while" loop.

    Update:

    But you have a point about not changing the list. You can't in Perl either. Try modifying a hash over which while each is interating. foreach keys, on the other hand, makes an anonymous copy of the hash keys before the loop begins, so you are not modifying the list over which you are iterating. You'll have to do the same (create a copy) in C++. However, the copy will have to be named (not anonymous) in C++.

    foreach my $seq (keys %SEQ) { foreach my $codon (@codons) { $SEQ{$seq.$codon} = 1; } delete $SEQ{$seq}; }
    would look more like:
    my @seq_keys = keys %SEQ; foreach my $seq_key_num (0..$#SEQ) { my $seq = $seq_keys[$seq_key_num]; foreach my $codon (@codons) { $SEQ{$seq.$codon} = 1; } delete $SEQ{$seq}; }

    @seq_keys is the copy in the above snippet. Notice how we are iterating over (the indexes of) @seq_keys and not over (the keys of) %SEQ? foreach does the same thing, but you just don't see foreach doing it. That's why it's safe to modify %SEQ.

    Your question deceptivly looks like a Perl question, but it has nothing to do with Perl. I had to convert from C++ to Perl (in my head) to answer you. Having a discussion in Perl on the subject of C++ doesn't make it a discussion about Perl any more than having a discussion in French on the subject of English language makes it a discussion about French. Please continue this discussion in a more appropriate forum.

Re^5: foreach to for (related to my last question)
by roboticus (Chancellor) on Jun 22, 2006 at 22:46 UTC
    tricolaire:

    OK, then, when you translate it to C++, make your life simpler and use STL. Then you'll find that your hashes, vectors and foreach loops translate fairly easily:

    vector<X> aas; vector<X>::iterator aa; map<K,V> SEQ; map<K,V>::iterator seq; vector<Z> codons; vector<Z>::iterator codon; for (aa=aas.begin(); aa!=aas.end(); ++aa) { for (seq=SEQ.begin(); seq!=SEQ.end(); ++seq) { for (codon=codons.begin(); codon!=codons.end(); ++codon) { ... do something ... } } }
    Also, what I've shown isn't great STL. There are functors and predicates that let you do things like map and grep. Give it a try. You'll find that it makes C++ much nicer. (Though it does take a bit of time to wrap your head around it when you start.)

    --roboticus

      Are you sure it's safe to add and delete items from a map while iterating over it? I personally don't know, so I'm just raising the issue.
        ikegami:

        Good question. I'll have to check...

        Update: It appears that you're correct--it seems that deleting a map element via the iterator would potentially shuffle the map enough that the iterator would be invalid. So to do the STL conversion, we'd need a bit of an algorithm change. (Like make a stack of iterators and push the ones you want to delete on the stack, and when the loop is done, pop off the items in the stack, deleting the things they point to.

        Yecch. Probably a better way out there. But I don't know it...

        Update 2: I've done some more digging. Deleting the element invalidates the iterator doing the pointing, but doesn't invalidate other iterators. So we could use a pair of iterators, one to scan and one to delete. A bit tricky looking, so I'll leave it as an exercise to the reader.... 8^)

        I hope this is a permanent link!

        Update 3 (and last!): I found a link in a mailing list that gives this handy list:

        vector -- all vector iterators are invalidated on any change
        string & wstring -- all string iterators are invalidated on any change
        deque -- all deque iterators are invalidated on any change, except for beginning and end removals
        list -- no list iterators are invalidated, except for those referring to a deleted element
        set & multiset -- no iterators are invalidated except ones pointing to a removed element
        map & multimap -- no iterators are invalidated except ones pointed to a removed element

        --roboticus