1. 含义

感觉这东西叫做string_slice比较合适,就是它不管理string的生命周期,只是拿来了一个切片?左指针和右指针来引用这个string,这样就不会有拷贝构造的开销了。

2. 用法

基本上很多都是和string一样的,不同的有remove_prefixremove_suffix

下面写了个样例

#include <string_view>
#include <string>
#include <iostream>

int main()
{
    std::string str("helloworldfuckworld");

    std::string_view sv = str;

    std::string tstr("world");

    sv.remove_prefix( sv.find(tstr) + tstr.size()) ;
    std::cout << sv << std::endl;

    sv.remove_suffix( sv.size() - sv.rfind(tstr));
    std::cout << sv << std::endl;

    return 0;
}

// fuckworld
// fuck
Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐