Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 749847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:28:56+00:00 2026-05-14T14:28:56+00:00

I know how to add integers to strings, but I’m not sure I’m doing

  • 0

I know how to add integers to strings, but I’m not sure I’m doing it in an efficient matters. I have a class where I often have to return a string plus an integer (a different integer each time), in Java I would do something like

public class MyClass {
  final static String S = "MYSTRING";
  private int id = 0;

  public String getString() {
    return S + (id++);
  }
}

But in C++ I have to do;

class MyClass {
 private:
  std::string S; // For some reason I can't do const std::string S = "MYSTRING";
  int id;

 public:
  MyClass() {
    S = "MYSTRING";
    id = 0;
  }

  std::string getString() {
    std::ostringstream oss;
    oss << S << id++;
    return oss.str();
  }
}

An additional constraint: I don’t want (in fact, in can’t) use Boost or any other librairies, I’ll have to work with the standard library.

So the thing is; the code works, but in C++ I have to create a bunch of ostringstream objects, so it seems inefficient. To be fair, perhaps Java do the same and I just don’t notice it, I say it’s inefficient mostly because I know very little about strings.

Is there a more efficient way to do this ?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T14:28:57+00:00Added an answer on May 14, 2026 at 2:28 pm

    std::ostringstream is the “standard” way to do this in C++. You might be able to make something more efficient via some custom coding, or laboriously comparing the performance of ostringstream, itoa, and sprintf on all the systems where you’ll be deploying this program, but it’s probably not worth the effort.

    I’d say the real problem with the std::ostringstream solution is not about efficiency. The real problem is that the code just looks too complicated.

    I know you don’t want to use Boost, but if you look at Herb Sutter’s The String Formatters of Manor Farm, you could just copy the (very tiny) definition of the lexical_cast<>() template into your program. Then your code would look like this:

    std::string getString() {
        return S + lexical_cast<std::string>(id++);
    }
    

    Whether this is more efficient than your existing solution depends on a lot of factors (how well your compiler inlines template instantiations, for example), but it definitely looks cleaner.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Close mysql connection (PHP) Please help me, I not know where add
I know I can add service behaviors with some XML configuration, but I'd like
I know how to add the Google Map on my restaurant's webpage, but how
Does anyone know how to add an item to a list but do it
I add C# group too in this, because this is not VB problem but
I know add() adds the specified (signed) amount of time to the given time
Why does C++ need and use pointers? I know they add power to the
I know one can add event listener for window.error. However when working with Iframes,
I know how to add SL page as a whole to existing ASP.net application.
Does anyone know how to add full (n.n.n.n) Windows file version information to COM

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.