C++字符串后面的sv和s

sv和s可以使包含\0的字符串不截断,s是C++14新出的  sv是C++17新出的

想使用后缀sv和s的话 using namespace std::literals 

函数原型如下

constexpr std::string_view   operator "" svconst char* str, std::size_t len noexcept;

 

如下例

#include <string>
#include <iostream>
 
int main()
{
    using namespace std::string_literals;
 
    std::string s1 = "abc\0\0def";
    std::string s2 = "abc\0\0def"s;
    std::cout << "s1: " << s1.size() << ' ' << s1 << '\n'
              << "s2: " << s2.size() << ' ' << s2 << '\n'
              << "abcdef"s.substr(1,4) << '\n';
}
output:
posted @ 2021-09-28 11:59  孙敬  阅读(1862)  评论(0)    收藏  举报