CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Substring

  1. #1
    Join Date
    Mar 2010
    Posts
    13

    Substring

    How does substr() in the string class work?? Does it copy over each character linearly?

    Thanks in advance

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Substring

    It probably depends on the implementation. A reference-counted copy-on-write implementation is possible, although I think the next version of the standard will require substr() to actually make an O(n) copy, yes. (Trying to use copy-on-write in conjunction with multithreading would be....interesting.)

  3. #3
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Substring

    Yes, here's a way to show:

    Code:
    std::string test("Hello World");
    std::cout << (void*)test.data() << " - " << (void*)test.substr(0, 5).data() << std::endl;
    If they're the same they're copy on write, else, literal copy.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured