Add strings together

This is a discussion on Add strings together within the C++ Programming forums, part of the General Programming Boards category; This is the wrong thing: Code: out.open("page"+pageNo+".html"); this is the correct thing: Code: out.open((std::string("page")+pageNo+".html").c_str()); They are not that different. So ...

  1. #16
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    This is the wrong thing:
    Code:
    out.open("page"+pageNo+".html");
    this is the correct thing:
    Code:
    out.open((std::string("page")+pageNo+".html").c_str());
    They are not that different. So there is nothing more simple than the above as the correct answer.

    About efficiency I don't think there is a huge difference.
    I would expect them to have the same speed more or less. Of course you give the total size so it allocates only once. You could call string::reserve for that to be fair.
    But in any case you allocate space, initialize it, and write again on that memory.
    The above "correct" method allocates space, initializes it, allocates space, initializes, allocates space initializes it.
    Since the string is not really big there won't be probably any need to copy data for the std::string. So it is 3 allocations vs 1 allocation and rewriting a memory space (page[4]).
    Now, do this with reserve (which is fair). Now you allocate space, initialize on part, initialize other part, initialize other part.
    So it is 3 allocations+initialization and 1 rewrite vs 3 allocations+initialization. So the std::string method might actually be faster.

    Of course the std::string will have some function calls overhead. But those are not that big of a time waste. And they are totally worth it.

    But the point is that you might NOT know the size. Especially considering that pageNo WILL change. It might go over 9. So you would need one more space.
    Last edited by C_ntua; 12-04-2008 at 09:51 AM.

  2. #17
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Assuming the OP isn't optimizing for some tiny embedded system, and is already using sstream rather than implementing his own itoa() to convert the integer pageNo to a string, why not simply stick with stringstream:

    Code:
    ...
        std::stringstream ss;               // he already has the equivalent of this in his code
        int pageNo = 31;                    // he already has the equivalent of this in his code
        ss << "page" << pageNo << ".html";  // he already has 'ss << pageNo;' in his code
        std::string s;
        ss >> s;
        out.open(s.c_str());
    ...

    or, if you object to the overhead of std::string (and he's not already using it someplace anyway):

    Code:
    ...
        std::stringstream ss;               // he already has the equivalent of this in his code
        int pageNo = 31;                    // he already has the equivalent of this in his code
        ss << "page" << pageNo << ".html";  // he already has 'ss << pageNo;' in his code
        char cs[5];
        const char* cp = cs;
        ss >> cs;
        out.open(cp);
    ...
    Now the only added overhead is a 5-byte char array (assuming his page numbers are no more than 4 digits) and a const char*.
    Last edited by R.Stiltskin; 12-04-2008 at 11:19 AM.

  3. #18
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by R.Stiltskin View Post
    Assuming the OP isn't optimizing for some tiny embedded system, and is already using sstream rather than implementing his own itoa() to convert the integer pageNo to a string, why not simply stick with stringstream:

    Code:
    ...
        std::stringstream ss;               // he already has the equivalent of this in his code
        int pageNo = 31;                    // he already has the equivalent of this in his code
        ss << "page" << pageNo << ".html";  // he already has 'ss << pageNo;' in his code
        std::string s;
        ss >> s;
        out.open(s.c_str());
    ...

    or, if you object to the overhead of std::string (and he's not already using it someplace anyway):

    Code:
    ...
        std::stringstream ss;               // he already has the equivalent of this in his code
        int pageNo = 31;                    // he already has the equivalent of this in his code
        ss << "page" << pageNo << ".html";  // he already has 'ss << pageNo;' in his code
        char cs[5];
        const char* cp = cs;
        ss >> cs;
        out.open(cp);
    ...
    Now the only added overhead is a 5-byte char array and a const char*.
    Well, it might work for this situation. But if you want it to be both valid in case the filename would be "page #.html" (with space) as well, and faster, you could use:
    Code:
    ss << "page" << pageNo << ".html";
    out.open(ss.str().c_str());

  4. #19
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    I got it working. Thanks guys!

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector vs. array.
    By matsp in forum C++ Programming
    Replies: 37
    Last Post: 06-23-2008, 12:41 PM
  2. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  3. Problem with Strings, Please help!
    By varus in forum C++ Programming
    Replies: 8
    Last Post: 11-27-2006, 10:47 PM
  4. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  5. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 11:04 AM

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