1
list<int> l;
list<int>::iterator start;
list<int>::iterator end;
list<int>::iterator mid;
for (int i = 0; i < 100; ++i)
    l.push_back(i);
start= l.begin();
end= l.end();
mid = start+ (end- start) / 2;
cout << *mid << endl;

Okay I have a concrete example now. end- start does not compile.

9
  • Did you intend to write ble.begin() + (ble.begin() - ble.end()) / 2 or was it supposed to be ble.begin() + (ble.end() - ble.begin()) / 2 ? Commented Nov 16, 2013 at 0:23
  • What do you mean it doesn't work? Doesn't compile? Doesn't give you the result you want? Commented Nov 16, 2013 at 0:24
  • I think he literally means last - start / 2, so start/2 obviously doesn't compile. Commented Nov 16, 2013 at 0:25
  • just fixed it thanks Adrian Panasiuk Commented Nov 16, 2013 at 0:25
  • Another typo ... pardon my mistakes.. - Adrian Panasiuk Commented Nov 16, 2013 at 0:26

1 Answer 1

3

list iterator is Bidirectional iterator, it's notRandom access iterator. You can't calloperator-` on it.

you could just try to use std::advance to move your iterator to the middle of container

std::advance(ble.begin(), ble.size()/2);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.