C++之C++11字符串字面量后缀总结(二百四十七)

编程入门 行业动态 更新时间:2024-10-24 19:14:41

C++之C++11字符串<a href=https://www.elefans.com/category/jswz/34/1743906.html style=字面量后缀总结(二百四十七)"/>

C++之C++11字符串字面量后缀总结(二百四十七)

简介: CSDN博客专家,专注Android/Linux系统,分享多mic语音方案、音视频、编解码等技术,与大家一起成长!

优质专栏:Audio工程师进阶系列原创干货持续更新中……】🚀

人生格言: 人生从来没有捷径,只有行动才是治疗恐惧和懒惰的唯一良药.

更多原创,欢迎关注:Android系统攻城狮

1.前言

本篇目的:理解C++11字符串字面量后缀总结。

2.C++11字符串字面量后缀介绍

  • C++11引入了字符串字面量后缀,可以用于在字符串字面量的末尾添加后缀以指定其类型。这样做的好处是增强了类型安全性,使得字符串字面量可以根据后缀的类型进行解析和处理。

  • 以下是一些常用的字符串字面量后缀:

  1. 后缀"s":用于表示字符串字面量,其类型为const char[]。例如:"hello"s表示类型为const char[]的字符串。

  2. 后缀"u8":用于表示UTF-8字符串字面量,其类型为const char[]。例如:"你好"u8表示类型为const char[]的UTF-8字符串。

  3. 后缀"u":用于表示宽字符字符串字面量,其类型为const wchar_t[]。例如:L"hello"u表示类型为const wchar_t[]的宽字符字符串。

  4. 后缀"U":用于表示Unicode字符串字面量,其类型为const char32_t[]。例如:U"hello"表示类型为const char32_t[]的Unicode字符串。

  5. 后缀"L":用于表示宽字符串字面量,其类型为const wchar_t[]。例如:L"hello"表示类型为const wchar_t[]的宽字符串。

这些字符串字面量后缀提供了更灵活的字符串字面量表示方式,可以根据需要选择适合的类型和编码方式。

  1. 使用s后缀表示字符串字面量:

    auto str1 = "hello"s; // 类型为std::string
    
  2. 使用u8后缀表示UTF-8字符串字面量:

    auto str2 = u8"你好"; // 类型为const char[]
    
  3. 使用u后缀表示宽字符字符串字面量:

    auto str3 = L"hello"u; // 类型为const wchar_t[]
    
  4. 使用U后缀表示Unicode字符串字面量:

    auto str4 = U"hello"; // 类型为const char32_t[]
    
  5. 使用L后缀表示宽字符串字面量:

    auto str5 = L"hello"; // 类型为const wchar_t[]
    

3.代码实例

1.后缀"s"

#include <iostream>
#include <string>
#include <string_view>using namespace std::literals::string_literals;int main() {std::string message = "Hellow"s;std::cout << message << std::endl;return 0;
}

1.后缀"u8": 表示字符串字面量,其类型为const char[]。例如:"hello"s表示类型为const char[]的字符串。

#include <iostream>
#include <string>
#include <string_view>using namespace std::literals::string_literals;int main() {std::string message = "Hellow"u8;std::cout << message << std::endl;return 0;
}

2.后缀"u":表示UTF-8字符串字面量,其类型为const char[]。例如:"你好"u8表示类型为const char[]的UTF-8字符串。

#include <iostream>
#include <string>
#include <string_view>using namespace std::literals::string_literals;int main() {std::string message = "Hellow"s;std::cout << message << std::endl;return 0;
}

3.后缀"U":表示宽字符字符串字面量,其类型为const wchar_t[]。例如:L"hello"u表示类型为const wchar_t[]的宽字符字符串。

#include <iostream>
#include <string>
#include <string_view>using namespace std::literals::string_literals;int main() {std::string message = "Hellow"s;std::cout << message << std::endl;return 0;
}

4.后缀"s"

#include <iostream>
#include <string>
#include <string_view>using namespace std::literals::string_literals;int main() {std::string message = "Hellow"s;std::cout << message << std::endl;return 0;
}

5.后缀"L":表示宽字符串字面量,其类型为const wchar_t[]。例如:L"hello"表示类型为const wchar_t[]的宽字符串。

#include <iostream>
#include <string>
#include <string_view>using namespace std::literals::string_literals;int main() {std::string message = "Hellow"s;std::cout << message << std::endl;return 0;
}

更多推荐

C++之C++11字符串字面量后缀总结(二百四十七)

本文发布于:2023-12-03 08:37:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1653297.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字面   后缀   字符串   二百   四十七

发布评论

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

>www.elefans.com

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