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

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    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,336
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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
    28,413
    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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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,336
    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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    ...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, 08: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, 07:59 AM

Tags for this Thread