CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2012
    Location
    USA
    Posts
    91

    [RESOLVED] STL string concatenation does not work.

    Hello,

    Why it's not working and how to make it work?

    Code:
        string str = "hello" + " *** ";
    Thank you.

  2. #2
    Join Date
    May 2009
    Posts
    2,413

    Re: STL string concatenation does not work.

    Quote Originally Posted by vincegata View Post

    Why it's not working and how to make it work?

    It's because "hello" and " *** " aren't of the string type. They're const char* and they cannot be added like that.

    But fortunately there's a string constructor that takes a const char* so this should work,

    Code:
        string str = string("hello") + string(" *** ");
    Last edited by nuzzle; May 18th, 2012 at 12:30 PM.

  3. #3
    Join Date
    Jan 2012
    Location
    USA
    Posts
    91

    Re: STL string concatenation does not work.

    -- thank you.

  4. #4
    Join Date
    May 2009
    Posts
    2,413

    Re: STL string concatenation does not work.

    It's possible to avoid the explicit conversions I suggested. For example you can do this,

    Code:
    string str = "hello";
    str = str + " *** ";
    Here instead implicit conversions (from const char* to string) take place behind the scene.

  5. #5
    Join Date
    Jan 2012
    Location
    USA
    Posts
    91

    Re: STL string concatenation does not work.

    Quote Originally Posted by nuzzle View Post
    It's possible to avoid the explicit conversions I suggested. For example you can do this,

    Code:
    string str = "hello";
    str = str + " *** ";
    Here instead implicit conversions (from const char* to string) take place behind the scene.
    That's the way I did it but I need to build long strings from pieces so I had to declare several variables.

  6. #6
    Join Date
    May 2012
    Location
    Bonn, Germany
    Posts
    43

    Re: [RESOLVED] STL string concatenation does not work.

    If you know in advance that the string will become very long, it could make sense to call "reserve" upfront. This way there will be less memory reallocations during the string building process. But this is only important when it has to be quick.

  7. #7
    Join Date
    Jan 2012
    Location
    USA
    Posts
    91

    Re: [RESOLVED] STL string concatenation does not work.

    > If you know in advance that the string will become very long, it could make sense to call "reserve" upfront. This way there will be less memory reallocations during the string building process. But this is only important when it has to be quick.

    Actually, it does have to be fast, I'll keep "reserve" in mind, I am just not at the stage to optimize the strings -- thx.

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