C++ 一个字符串的代理类(String View)

在学习muduo/base源码的时候看到一个StringPiece类,是个string view,记录下来。

关于string view,看http://stackoverflow.com/questions/20803826/what-is-string-view

简单摘抄下来就是

The purpose of any and all kinds of “string reference” and “array reference” proposals is to avoid copying data which is already owned somewhere else and of which only a non-mutating view is required. The string_view in question is one such proposal; there were earlier ones called string_ref and array_ref, too.

The idea is always to store a pair of pointer-to-first-element and size of some existing data array or string.

string view解释过来就是个窗口,你通过这个窗口查看代理的字符串的内容,移动指针和改变长度就可以移动窗口,而且,自身不需要存储字符串,只读,不可修改,生命期应和代理的字符串一致。

muduo的StringPiece代码如下,个人做了一点修改:

class StringPiece {
public:
    StringPiece()
        : _ptr(nullptr),
          _length(0)
    {
    }

    StringPiece(const char* str)
        : _ptr(str),
          _length(strlen(_ptr))
    {
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值