0

I've never walked into the following problem with iterators, so I really do not know where the solution might start. I have a LIST "StringOfPearls" which is permuted right before I do the following:

DNAr = StringOfPearls->begin(); // Added 1 element to the beginning of this list in earlier line of code
cout << "pos DNAr before bumping by 1: " << distance(DNAreplicase, StringOfPearls->begin()) << endl;
advance(DNAreplicase, 1);
cout << "pos DNAr after bumping by 1: " << distance(DNAreplicase, StringOfPearls->begin()) << endl;

Returns:

pos DNAr before bumping by 1: 0
pos DNAr after bumping by 1: 10

No doubt I'm just missing something silly, but can anyone help me out?

Brem
  • 21
  • 1

1 Answers1

0

You are not using std::distance correctly. begin iterator is the first argument, e.g.

distance(StringOfPearls->begin(), DNAreplicase) 

This is the convention for all functions taking pairs of iterators denoting a range.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
  • 1
    Update for C++11: _"The value may be negative if random-access iterators are used and first is reachable from last."_ source: http://en.cppreference.com/w/cpp/iterator/distance – Richard Critten Sep 19 '17 at 10:33