5

My problem is as follows: I use an iterator, and I want to compare each element to the next element. Prototype looks like below, how can I increase the iterator to be able to compare? Also, how can I set a proper condition for this to happen? I mean how to point on the last element, not on the next after the last like with end() function:

std::vector<T>::const_iterator it;
std::vector<T>::const_iterator it2;
for (it = set.begin(), it != set.end(); it++)
{
  // some things happen
  if ( final == it )
  {
     if ( it != set.end()-1 ) // how to write properly condition?
     {
        it2 = it + 1; //how to assign the next here?
        if (...)//some condition
        {
          if ( it->func1() - it2->func1()) < 20 ) //actual comparison of two consecutive element values
            // do something
        }
      }
   }
}
matsjoyce
  • 5,744
  • 6
  • 31
  • 38
berndh
  • 1,355
  • 4
  • 14
  • 19
  • What is the problem you're trying to solve with those multiple iterators? – tehlexx Feb 12 '13 at 12:52
  • I'm using till now just one iterator (this is just part of code). I want to be able to compare func1 of two consecutive in the set. so I need one iterator pointing on actual, one on the next one. is there a better approach? I might have done some overkill here, plz correct me – berndh Feb 12 '13 at 12:55
  • See [my answer](http://stackoverflow.com/questions/14826893/how-do-i-loop-over-consecutive-pairs-in-an-stl-container-using-range-based-loop/14827924#14827924) to this recent question [*How to loop over consecutive pairs in an STL container?*](http://stackoverflow.com/q/14826893/1084416). – Peter Wood Feb 12 '13 at 12:58
  • 1
    for `set.end()-1`, use `set.rbegin()` – Deamonpog Feb 12 '13 at 13:08

5 Answers5

6

In C++11 use the functions std::next() and std::prev().

Your code could become:

// before
it != std::set.end()-1

// after
it != std::prev(set.end())

and

// before
it2 = it + 1;

// after
it2 = std::next(it);

That is true also for non-vector containers, such as map,set or others.

NOTE: after std::next(it), "it" iterator remains unmodified!

NOTE 2: Use it2 = std::next(it,n); to increment as much as you need.

Gabrer
  • 465
  • 5
  • 21
2

You can use adjacent_find to solve that. You should use the second form of that function (with predicate) and pass to the predicate your some things happen and some condition in c-tor

auto found = std::adjacent_find( set.begin(), set.end(),
    [some_comdition]( const T & left, const T & right ) {
        if ( some_comdition ) {
            if ( left.func1() - right.func1() < 20 ) {
                do_smth();
                // return true; if there's no need to continue
            }
        }
        return false;
    }
);
borisbn
  • 4,988
  • 25
  • 42
1

Based on the fact that it++ is acceptable, we should define a new iterator called itplusone, which is initialized as itplusone = ++it. In this way, you can safely use the meaning of an iterator pointing to the next item of it. Also clearly, the range of iterator of itplusone bounded by terms itplusone != set.end(). I use this method to compute the total weight of a path, which is defined as a list object.

matsjoyce
  • 5,744
  • 6
  • 31
  • 38
Ming Liu
  • 11
  • 1
-1

In the for loop, you use it++ which means it = it + 1, which is perfectly ok. So this one will be fine also it2 = it + 1. it2 will be pointing to the next value.

In the for loop again, you use it != set.end(), which is again perfectly ok. So you can also it + 1 < set.end(), just like you did in your code.

I don't see anything wrong in your code, just wanted to explain.

ogzd
  • 5,532
  • 2
  • 26
  • 27
  • That's true for `std::vector`, but in general, `it++` does **not** mean `it = it + 1`. All iterators support increment, but only random access iterators support addition. – Pete Becker Feb 12 '13 at 13:03
  • 1
    I may want use it for other containers also, so that's my problem, how to assign the next element to my new iterator. I pointed in the answers above this problem with number – berndh Feb 12 '13 at 13:14
-1

somewhat late, just discovered it, but like mentioned above, ++ iterator works fine.

vector<string> P
auto itA = begin(P);

while(itA != end(P))
{
    if(itA != end(P))
    {   
        ++itA; // 
    } 
}
NaturalDemon
  • 934
  • 1
  • 9
  • 21