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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:44:13+00:00 2026-06-16T09:44:13+00:00

It seems that std::string – because it doesn’t use expression templates – has a

  • 0

It seems that std::string – because it doesn’t use expression templates – has a O(n^2) complexity instead of a possibly O(n) complexity for some operations like concatenation. Same thing with a std::stringstream class when you have to insert many elements.

I would like to understand this, at least if someone could have some good links about this point that would be great.

  • 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. Editorial Team
    Editorial Team
    2026-06-16T09:44:15+00:00Added an answer on June 16, 2026 at 9:44 am

    Concatenating multiple strings together has different complexities in C++ depending how it is done. I believe the situation that you’re thinking of is:

    string result = string("Hello, ") + username + "! " +
                    "Welcome to " + software_product + ".";
    

    which concatenates 6 strings. The first string is copied 5 times, the second is copied 4 times, and so forth. As Leonid Volnitsky notes in his answer, the exact bound for this Θ(NM), where M is the number of concatenation operations, and N is the total length of the strings being concatenated. We can also call this O(N^2) when M <= N. Note that it’s not guaranteed that M <= N, because you can increase M without increasing N by trying to concatenate the empty string.

    Expression templates could help speed up this use case, though it would cause problems with auto and decltype type inference in C++11, as well as with template type inference in C++98. All of these would deduce the type of

    auto result = string("Hello, ") + username + "! " +
                  "Welcome to " + software_product + ".";
    

    to be the lazily-evaluated string template type that was used to make the expression template magic happen. Other reasons why expression templates are not a great idea include Leonid Volnitsky‘s answer about this slowing compilation time. It would probably also increase the size of your compiled binary.

    Instead, there are other solutions in C++ could be used to get Θ(N) concatenation:

    string result = "Hello, ";
    result += username;
    result += "! ";
    result += "Welcome to ";
    result += software_product;
    result += ".";
    

    In this version, the string is modified in place, and while data that has already been copied into result sometimes needs to be recopied, C++ strings are typically implemented as dynamic arrays that allocate new space exponentially, so that the insertion of each new character takes amortized constant time, leading to overall Θ(N) behavior for repeated concatenation.

    The following is a way of doing the same thing, almost on one line. It uses the same principle internally, but also supports converting non-string types to strings using << overloading.

    stringstream result;
    result << "Hello, " << username << "! " << "Welcome to " << software_product
           << ".";
    // do something with result.str()
    

    Lastly, the C++ standard library doesn’t include this, but one could define the following function with some stringstream magic inside it. The implementation is left as an exercise for the reader.

    template <typename... Items>
    std::string concat(std::string const& a, std::string const& b, Items&&... args)
    

    You can then call concat for repeated concatenation on one line in O(N) time:

    string result = concat("Hello, ", username, "! ", "Welcome to ",
                           software_product, ".");
    

    Presumably, all of these are better solutions than messing up type inference by creating an expression template type.

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

Sidebar

Related Questions

It seems that I can sort a std::vector<std::pair<int, std::string>> , and it will sort
I'm having some issues with std::cout, std::stringstream, and std::string.c_str(). Mainly, it seems that there's
It seems that we can show layers and even use a different zPosition for
It seems that runClasses() doesn't terminate the code being tested even after the test
It seems that it is not advisable to use <meta http-equiv=REFRESH CONTENT=3;url=url> for redirects
It seems that NHibernate Linq doesn't understand grouped .Where method conditions. I got the
String literals are array objects: typeid(hello).name() // char [6] This seems convenient because the
I have a C++ program already formed that has a string that I want
Possible Duplicate: What happens if I return literal instead of declared std::string? Consider the
Seems that This will be an easy question for you but this problem is

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.