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 140567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:38:11+00:00 2026-05-11T07:38:11+00:00

I heard a few people expressing worries about + operator in std::string and various

  • 0

I heard a few people expressing worries about ‘+’ operator in std::string and various workarounds to speed up concatenation. Are any of these really necessary? If so, what is the best way to concatenate strings in C++?

  • 1 1 Answer
  • 0 Views
  • 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. 2026-05-11T07:38:12+00:00Added an answer on May 11, 2026 at 7:38 am

    The extra work is probably not worth it, unless you really really need efficiency. You probably will have much better efficiency simply by using operator += instead.

    Now after that disclaimer, I will answer your actual question…

    The efficiency of the STL string class depends on the implementation of STL you are using.

    You could guarantee efficiency and have greater control yourself by doing concatenation manually via c built-in functions.

    Why operator+ is not efficient:

    Take a look at this interface:

    template <class charT, class traits, class Alloc> basic_string<charT, traits, Alloc> operator+(const basic_string<charT, traits, Alloc>& s1,           const basic_string<charT, traits, Alloc>& s2) 

    You can see that a new object is returned after each +. That means that a new buffer is used each time. If you are doing a ton of extra + operations it is not efficient.

    Why you can make it more efficient:

    • You are guaranteeing efficiency instead of trusting a delegate to do it efficiently for you
    • the std::string class knows nothing about the max size of your string, nor how often you will be concatenating to it. You may have this knowledge and can do things based on having this information. This will lead to less re-allocations.
    • You will be controlling the buffers manually so you can be sure that you won’t copy the whole string into new buffers when you don’t want that to happen.
    • You can use the stack for your buffers instead of the heap which is much more efficient.
    • string + operator will create a new string object and return it hence using a new buffer.

    Considerations for implementation:

    • Keep track of the string length.
    • Keep a pointer to the end of the string and the start, or just the start and use the start + the length as an offset to find the end of the string.
    • Make sure the buffer you are storing your string in, is big enough so you don’t need to re-allocate data
    • Use strcpy instead of strcat so you don’t need to iterate over the length of the string to find the end of the string.

    Rope data structure:

    If you need really fast concatenations consider using a rope data structure.

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

Sidebar

Related Questions

I am new to iPhone. I heard from few people that it is possible
I am new to iphone programming. I heard from few people that even if
I have heard a few people say that deploying a portlet war file (or
I have heard a few developers recently say that they are simply polling stuff
I've heard a few debates in the past over which is more mature: RubyCocoa
I've heard of a few ways to implement tagging; using a mapping table between
I guess everyone has already heard the news about some key developers leaving the
Last I heard, Blizzard was one of the few companies to put client-side XSLT
Something I heard a few times from IT guys (none of which are programmers)
I've been pulling my hear out over this problem for a few hours yesterday:

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.