1

I am trying to iterate two maps with one loop. it works fine with one map. when I add second map(see code below) it gives me error "Cannot deduce 'auto' type"

both variables are of same type.

what is the best way to iterate two maps.

 for ( auto& insertEntry = insertedInstances.begin(),
       auto& updateEntry = toUpdateInstances.begin(); 
       insertEntry != insertedInstances.end(); insertEntry++,updateEntry++
     )
 {
    //do something
 }
Muhammad Zaighum
  • 560
  • 4
  • 21
  • 2
    How is iteration in parallel supposed to work - is there a guarantee that `insertedInstances` and `toUpdateInstances` have the same number of elements? Also, the code doesn't seem to be advancing the `updateEntry` iterator anywhere. – user4815162342 Jun 19 '14 at 07:27
  • yes they both have same number of contents – Muhammad Zaighum Jun 19 '14 at 07:30

2 Answers2

4

it is equivalent to

for(int i=0, j=0; i<10;i++) { //do something }

so remove the extra auto&

for ( auto insertEntry = insertedInstances.begin(),
      updateEntry = toUpdateInstances.begin(); 
      insertEntry != insertedInstances.end(); 
      ++insertEntry , ++updateEntry //pre-increment might produce better code
    )
{
  //do something
}

you can not use same type twice with comma operator.

Rakib
  • 7,435
  • 7
  • 29
  • 45
  • @MuhammadZaighum, look [here](http://stackoverflow.com/questions/24901/is-there-a-performance-difference-between-i-and-i-in-c) and [here](http://stackoverflow.com/questions/2020184/preincrement-faster-than-postincrement-in-c-true-if-yes-why-is-it) – Rakib Jun 19 '14 at 07:35
  • @RakibulHasan, `auto& insertEntry = ...` is not good. Try to compile some code with that construct. It should be `auto insertEntry = ...`. – R Sahu Jun 19 '14 at 07:42
3

You can't have multiple type declarators inside the loop. In your case you use auto& twice. As long as both maps have the same type you could used

for (auto insertEntry = insertInstances.begin(), updatedEntry = insertInstances.begin();
     insertEntry != insertInstances.end(); ++insertEntry, ++updateEntry) {
    ...
}

(you probably don't want to use auto& in this context but auto anyway).

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • I think you need to increment the second iterator at some point. – juanchopanza Jun 19 '14 at 07:29
  • @juanchopanza: there wasn't an increment for the second iterator anywhere in the original code. I don't know what these iterators are being used for, i.e., I don't speculate what is done in the `...` part. OK, just saw the update to the question and updated my answer correspondingly – Dietmar Kühl Jun 19 '14 at 07:31
  • Fair enough! But I suspect it is an oversight or a but in OP's code. – juanchopanza Jun 19 '14 at 07:32
  • is there a better way to Iterate two maps ? I have two maps with same number of content but one of them contains ids that I want to copy to the other and do some operations – Muhammad Zaighum Jun 19 '14 at 07:35
  • @MuhammadZaighum This way seems perfectly fine to me. This kind of flexibility is what iterators and the generic `for` loop are for. – user4815162342 Jun 19 '14 at 07:56
  • @MuhammadZaighum "one of them contains ids that I want to copy to the other" - you should think about whether those insertions will always be before the node you've iterated to in that destination container - if not, you'll have to design your code to handle visiting the newly inserted node, and also do something to ensure the check for end doesn't trigger prematurely or too late.... – Tony Delroy Jun 19 '14 at 08:06