在C ++中使用后缀的任何优势

编程入门 行业动态 更新时间:2024-10-26 08:26:27
本文介绍了在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?

推荐答案

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

Null characters can be included in the raw string trivially; example from en.cppreference/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 ++中使用后缀的任何优势

本文发布于:2023-11-29 19:36:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647423.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:后缀   优势

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!