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
    Location
    Kolkata@India
    Posts
    2,498

    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...?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    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
    Location
    Kolkata@India
    Posts
    2,498
    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...
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Or ..would getting rid of the iterators and using the position as a control for the master loop be a better idea ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,443
    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().
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Location
    Kolkata@India
    Posts
    2,498
    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) ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    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
    Location
    Kolkata@India
    Posts
    2,498
    ...Well.... this illustrates the problem I'm facing...
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



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