A question about "std::string::substr"

This is a discussion on A question about "std::string::substr" within the C++ Programming forums, part of the General Programming Boards category; In all the references I saw, the arguments to the string::substr function is in index numbers. Can it be generalized ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Posts
    2,600

    A question about "std::string::substr"

    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...?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  3. #3
    Chi! whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    8,221
    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

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Posts
    2,600
    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...

  5. #5
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Posts
    2,600
    Or ..would getting rid of the iterators and using the position as a control for the master loop be a better idea ?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,821
    Quote Originally Posted by manasij7479
    I think I need the reverse of that... I have the iterator ...but not the position number...
    You can subtract the return value of begin(), or more generally use std::distance().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Posts
    2,600
    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) ?

  8. #8
    Chi! whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    8,221
    Code:
     begin() -> s[0] = 'h';
     s[1] = 'e';
     s[2] = 'l';
     s[3] = 'l';
     s[4] = 'o';
     s[5] = ?? <- end()
    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.
    Last edited by whiteflags; 05-25-2011 at 09:36 PM.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  10. #10
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Posts
    2,600
    ...Well.... this illustrates the problem I'm facing...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 46
    Last Post: 08-24-2007, 04:52 PM
  2. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 07:25 AM
  3. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 06:59 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21