Thread: substr and error checking

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    44

    substr and error checking

    if we have a string for example "test"

    and call substr(0,3) for it it will give us the string tes right?

    but how does substr work when we call substr(0,10000);

    it will give us the string test, but will it check until it finds a '\0' or will it check all the 10000 bytes after the first character of the string? I mean, does substr check for such errors?

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    substr doesn't take integer arguments. What are you talking about? substr(3): locate substring - Linux man page

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I think he's referring to std::string::substr(size_t, size_t).

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well...
    Quote Originally Posted by C++03 Clause 21.3.6.7 basic_string::substr
    Code:
    basic_string<charT,traits,Allocator>
    substr(size_type pos = 0, size_type n = npos) const;
    Requires: pos <= size()
    Throws: out_of_range if pos > size().
    Effects: Determines the effective length rlen of the string to copy as the smaller of n and size() - pos.
    Returns: basic_string<charT,traits,Allocator>(data()+pos,rl en).
    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

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    44
    ok I understand it now, thanks for help

Popular pages Recent additions subscribe to a feed