sv和s可以使包含\0的字符串不截断,s是C++14新出的 sv是C++17新出的
想使用后缀sv和s的话 using namespace std::literals
函数原型如下
constexpr std::string_view operator "" sv( const 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:
s1: 3 abc s2: 8 abc^@^@def bcde
参考 std::literals::string_literals::operator""s - cppreference.com
