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

前端 未结 15 1192
深忆病人
深忆病人 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条回答
  •  借酒劲吻你
    2021-01-01 10:59

    Surprised no one mentioned it yet, but of course boost has something ;)

    Boost.Next (and the equivalent Boost.Prior)

    Your example would look like:

    for (iter = someMap.begin(); iter != someMap.end(); ++iter) {
        // do something for all iterations
        if (boost::next(iter) != someMap.end()) {
            // do something for all but the last iteration
        }
    }
    

提交回复
热议问题