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

前端 未结 15 1197
深忆病人
深忆病人 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 11:09

    Why to work to find the EOF so that you dont give something to it.

    Simply, exclude it;

    for (iter = someMap.begin(); someMap.end() - 1; ++iter) {
        //apply to all from begin to second last element
    }
    

    KISS (KEEP IT SIMPLY SIMPLE)

    0 讨论(0)
  • 2021-01-01 11:09
    #include <boost/lambda/lambda.hpp>
    #include <boost/lambda/bind.hpp>
    #include <algorithm>
    
    using namespace boost::lambda;
    
    // call the function foo on each element but the last...
    if( !someMap.empty() )
    {
      std::for_each( someMap.begin(), --someMap.end(), bind( &Foo, _1 ) );
    }
    

    Using std::for_each will ensure that the loop is tight and accurate... Note the introduction of the function foo() which takes a single argument (the type should match what is contained in someMap). This approach has the added addition of being 1 line. Of course, if Foo is really small, you can use a lambda function and get rid of the call to &Foo.

    0 讨论(0)
  • 2021-01-01 11:12

    How about this, no one mentioning but...

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

    this seems simple, mm...

    0 讨论(0)
  • 2021-01-01 11:13

    Full program:

    #include <iostream>
    #include <list>
    
    void process(int ii)
    {
       std::cout << " " << ii;
    }
    
    int main(void)
    {
       std::list<int> ll;
    
       ll.push_back(1);
       ll.push_back(2);
       ll.push_back(3);
       ll.push_back(4);
       ll.push_back(5);
       ll.push_back(6);
    
       std::list<int>::iterator iter = ll.begin();
       if (iter != ll.end())
       {
          std::list<int>::iterator lastIter = iter;
          ++ iter;
          while (iter != ll.end())
          {
             process(*lastIter);
             lastIter = iter;
             ++ iter;
          }
          // todo: think if you need to process *lastIter
          std::cout << " | last:";
          process(*lastIter);
       }
    
       std::cout << std::endl;
    
       return 0;
    }
    

    This program yields:

     1 2 3 4 5 | last: 6
    
    0 讨论(0)
  • 2021-01-01 11:14

    If you just want to use a ForwardIterator, this should work:

    for ( i = c.begin(); i != c.end(); ) {
            iterator cur = i++;
            // do something, using cur
            if ( i != c.end() ) {
                    // do something using cur for all but the last iteration
            }
    }
    
    0 讨论(0)
  • 2021-01-01 11:14

    For someone who likes C++11 range-based loop:

        for (const auto& pair : someMap) {
          if (&pair != &*someMap.rbegin()) ...
        }
    

    Notice only reference type works here, not auto pair

    0 讨论(0)
提交回复
热议问题