substr question

This is a discussion on substr question within the C++ Programming forums, part of the General Programming Boards category; Hi! I need to use the string::substr function, but its header says: basic_string substr( size_t index, size_t num ); Canīt ...

  1. #1
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    substr question

    Hi!
    I need to use the string::substr function, but its header says:
    basic_string substr( size_t index, size_t num );
    Canīt I just pass an int or an iterator???
    Nothing more to tell about me...
    Happy day =)

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    161
    Yes, you can just pass an int. size_t is usually just a typedef'd unsigned int.

  3. #3
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Thanks!!! I didnīt know the typedef stuff!!
    Jut one more question: and if I am using iterators to search the string? What can I do?
    I need this thing because I am making a parser.
    Last edited by gustavosserra; 08-30-2003 at 04:49 PM.
    Nothing more to tell about me...
    Happy day =)

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    "and if I am using iterators to search the string?"

    You don't get to decide what parameters you're going to pass the function: the function is defined to take two int parameters.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    161
    You can try subtracting iterators:

    Code:
    string s = str.substr(itr - str.begin(), len);
    I'm not sure about the portability of this, so your mileage may vary.

  6. #6
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    Talking Cool!

    Really thanks!!!
    Nothing more to tell about me...
    Happy day =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 03:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 06:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 01:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM

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