How can I detect the last iteration in a loop over std::map?

前端 未结 15 1372
深忆病人
深忆病人 2021-01-01 10:24

I\'m trying to figure out the best way to determine whether I\'m in the last iteration of a loop over a map in order to do something like the following:

for          


        
15条回答
  •  猫巷女王i
    2021-01-01 10:52

    The following code would be optimized by a compiler so that to be the best solution for this task by performance as well as by OOP rules:

    if (&*it == &*someMap.rbegin()) {
        //the last iteration
    }
    

    This is the best code by OOP rules because std::map has got a special member function rbegin for the code like:

    final_iter = someMap.end();
    --final_iter;
    

提交回复
热议问题