std :: string :: replace()是否针对相同长度的字符串进行了优化? [英] Is std::string::replace() optimized for same length strings?

查看:178
本文介绍了std :: string :: replace()是否针对相同长度的字符串进行了优化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设,大多数情况下,我有以下替换情况:

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"

在这里,string类必须放入"xy",然后擦除2个字符,这有效地将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"

仅当replace()针对相同长度的字符串进行了优化时,上述练习才有帮助.应该是因为这很简单.但只是想确认是否有人掌握第一手知识.
我尝试通过Eclipse IDE浏览到字符串类,但是无法深入探讨.

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.

推荐答案

我只看一下MSVC 2008的实现.他们优化(我省略了一些东西):

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.

编辑:可以得出结论,如果在复制数据后使用相同长度的字符串,则必须移动/填充总共"0"个字符/孔(即不移动).因此,实际上并不需要优化,但是却很小心.

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.

这篇关于std :: string :: replace()是否针对相同长度的字符串进行了优化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆