In all the references I saw, the arguments to the string::substr function is in index numbers.
Can it be generalized to string::iterator"s?
or..Is the iterator value same as the position...
or How do I generate the ^later from the ^former...?
In all the references I saw, the arguments to the string::substr function is in index numbers.
Can it be generalized to string::iterator"s?
or..Is the iterator value same as the position...
or How do I generate the ^later from the ^former...?
substr does not take iterators. string has a constructor that takes iterators, plus you have all the stuff in <algorithm>, like copy.
(EDIT: Looks like I read it backwards. If you have indices and want iterators (for reasons unknown), then ignore this and read whiteflags.)
Last edited by tabstop; 05-25-2011 at 08:18 PM.
A pointer is an iterator type, so &string[pos] is an expression that will get you an iterator, but not necessarily a std::string::iterator. For that instead, write:Code:string.begin() + pos
Sorry..I didn't fully get that..
@whiteflags : I think I need the reverse of that... I have the iterator ...but not the position number...
Or ..would getting rid of the iterators and using the position as a control for the master loop be a better idea ?
You can subtract the return value of begin(), or more generally use std::distance().Originally Posted by manasij7479
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Does iterator - xx.begin() give the position or is there a +1 necessary..?
And When I have the two positions a and b... is (b-a) the distance or is it (b-a +1) ?
std::distance() makes this irrelevant, but index number 4 is the 5th character, because indexes start from 0. If you want to use an index, a - b is correct math. The other number, a-b+1, has no technical value, it's just how you look at it.Code:begin() -> s[0] = 'h'; s[1] = 'e'; s[2] = 'l'; s[3] = 'l'; s[4] = 'o'; s[5] = ?? <- end()
Last edited by whiteflags; 05-25-2011 at 09:36 PM.
Also to keep in mind: usual custom, especially with iterators, is for the second iterator to not be included in the range (e.g., begin() points in the string, end() points one-past-the-end). So depending on how you set that up also plays a factor in how many items you need.
...Well.... this illustrates the problem I'm facing...