for(i = 0; i < 5; ++i) iterator++;
?Iterators <-> Offsets
Can I set an iterator to position 5 in a string via some member
You can use std::advance
std::advance(iterator, 5);
or
iterator += 5;
Given an Iterator, how can I convert that to a numeric offset in the string?
You can use std::distance
std::distance(string.begin(), iterator);
or
iterator - string.begin()
std::string iterators are random access iterators, which define the +operator. You can get a iterator to position 5 with begin(str) + 5
. Offset can be computed via std::distance
which uses -operator for random access iterators.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With