在C ++中使用后缀的任何优势 [英] Any advantage of using the s suffix in C++

查看:69
本文介绍了在C ++中使用后缀的任何优势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与在C ++中使用"s"后缀有关?

My question is related to the use of the "s" suffix in C++?

使用"s"后缀的代码示例:

Example of code using the "s" suffix:

auto hello = "Hello!"s; // a std::string

可以这样写:

auto hello = std::string{"Hello!"};

我能够在网上找到应该使用"s"后缀来最大程度地减少错误并在代码中阐明我们的意图.

I was able to find online that the "s" suffix should be used to minimizes mistakes and to clarify our intentions in the code.

因此,使用"s"后缀仅对代码读者有用吗?还是使用它还有其他优势?

Therefore, is the use of the "s" suffix only meant for the reader of the code? Or are there others advantages to its use?

推荐答案

空字符可以简单地包含在原始字符串中;来自http://en.cppreference.com/w/cpp/string/basic_string/operator%22%22s

Null characters can be included in the raw string trivially; example from http://en.cppreference.com/w/cpp/string/basic_string/operator%22%22s

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";
    std::cout << "s2: " << s2.size() << " \"" << s2 << "\"\n";
}

可能的输出:

s1: 3 "abc"
s2: 8 "abc^@^@def"

这篇关于在C ++中使用后缀的任何优势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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