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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:30:19+00:00 2026-06-17T09:30:19+00:00

Suppose, most of the time I have below scenario for replacement: std::string line; //

  • 0

Suppose, most of the time I have below scenario for replacement:

std::string line; // "line" contains a big string.
std::string from = "abcd";
std::string to = "xy";  // to.length() < from.length()
// replace "from" with "to" everywhere in "line"

Here the string class has to put "xy" and then erase 2 characters which effectively shifts all character in line towards left. There are so many such replacements happening throughout the life of my code.

Now coming to the real question. Below is also acceptable for me:

// ...
if(to.legnth() < from.length())
  to.resize(from.length(), ' ');
// now to.length() = from.length()
// replace "from" with "to" everywhere in "line"

Above exercise is helpful only if replace() is optimized for same length strings. It should be because that’s trivial; but just wanted to confirm if someone has the 1st hand knowledge.
I tried browsing through Eclipse IDE into string class, but couldn’t dig into so much.

  • 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-17T09:30:20+00:00Added an answer on June 17, 2026 at 9:30 am

    I just look at MSVC 2008’s implementation. They do optimize (I omit some stuff):

    _Myt& __CLR_OR_THIS_CALL replace(size_type _Off,
        size_type _N0, const _Myt& _Right, size_type _Roff, size_type _Count)
    {
        ...
            if (_Count <= _N0)
            {   // hole doesn't get larger, just copy in substring
            _Traits_helper::move_s<_Traits>(_Myptr() + _Off, _Myres - _Off,
                _Myptr() + _Roff, _Count);  // fill hole
            _Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,
                _Myptr() + _Off + _N0, _Nm);    // move tail down
            }
        else if (_Roff <= _Off)
            {   // hole gets larger, substring begins before hole
            _Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,
                _Myptr() + _Off + _N0, _Nm);    // move tail down
            _Traits_helper::move_s<_Traits>(_Myptr() + _Off, _Myres - _Off,
                _Myptr() + _Roff, _Count);  // fill hole
            }
        else if (_Off + _N0 <= _Roff)
            {   // hole gets larger, substring begins after hole
            _Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,
                _Myptr() + _Off + _N0, _Nm);    // move tail down
            _Traits_helper::move_s<_Traits>(_Myptr() + _Off, _Myres - _Off,
                _Myptr() + (_Roff + _Count - _N0), _Count); // fill hole
            }
        else
            {   // hole gets larger, substring begins in hole
            _Traits_helper::move_s<_Traits>(_Myptr() + _Off, _Myres - _Off,
                _Myptr() + _Roff, _N0); // fill old hole
            _Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,
                _Myptr() + _Off + _N0, _Nm);    // move tail down
            _Traits_helper::move_s<_Traits>(_Myptr() + _Off + _N0, _Myres - _Off - _N0, _Myptr() + _Roff + _Count,
                _Count - _N0);  // fill rest of new hole
            }
            ...
        }
    

    Take an attantion, that the case when new length is smaller and the case when lengths are equal are similar.

    Edit: It can be concluded that in the case of same length strings after copying data, total “0” characters/holes have to be moved/filled (i.e. no movement). Thus no optimization is really needed, but it’s taken care trivially.

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

Sidebar

Related Questions

Suppose I have a class Baz that inherits from classes Foo and Bar ,
Most of the time I have been programming little apps either for myself or
I suppose most of the developers have an idea of multi-layer architecture. We have
suppose I have an object like this: class Spline { public: Spline(std::size_t const dim);
Suppose we have an arbitrary string, s . s has the property of being
Suppose we have an application that's stable. Tomorrow, someone reports a big ol' bug
What is the most efficient way to print out time as HH:MM:SS? I have
I have this exception raised from time to time causing my mysql calls to
I am implementing a file sharing system in ASP.NET MVC3. I suppose most file
suppose i have a .on() function wherein i select multiple ids $(#i, #am, #your,

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.