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


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

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

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

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

    欢迎关注Android系统攻城狮

    1.前言

    本篇目的:在Android代码中:例如:“–display-id"s、”–dispsync"s等这种用法会让开发者疑惑,其实这是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[]的宽字符串。

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

    3.代码实例

    0.前缀u:用于创建char16_t类型(占2个byte)

    #include 
    #include 
    #include 
    
    
    using namespace std::literals::string_literals;
    
    int main() {
       //v1.0 char16_t*转char* 
      const char16_t *str1 = u"Test char16_t";
      std::string str3 = converter.to_bytes(str1);
      printf("xxx--------------->%s(), line = %d, str3 = %s\n",__FUNCTION__,__LINE__,str3.c_str());
      return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    1.后缀s:用于创建 std::string 对象

    #include 
    #include 
    #include 
    
    using namespace std::literals::string_literals;
    
    int main() {
        std::string message = "Hellow"s;
        std::cout << message << std::endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2.后缀u8:表示UTF-8字符串字面量,其类型为const char[]

    #include 
    #include 
    #include 
    
    using namespace std::literals::string_literals;
    
    int main() {
        const char* str2 = u8"你好";
        std::cout << str2 << std::endl;
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    3.后缀L: 用于表示宽字符串字面量,其类型为const wchar_t[]

    #include 
    #include 
    #include 
    
    using namespace std::literals::string_literals;
    
    int main() {
        const wchar_t* str3 = L"hello";
        std::wcout << str3 << std::endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    4. U 后缀用于创建 UTF-32 编码的字符串:

    #include 
    #include 
    
    int main() {
        std::wstring wideString = L"Hello";
        std::wcout << wideString << std::endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    5. i 后缀用于创建 std::complex 复数对象:

    #include 
    #include 
    
    int main() {
        std::complex<double> complexNumber = 1.0 + 2.0i;
        std::cout << complexNumber << std::endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    6. if 后缀用于创建 std::complex 复数对象,其中虚部为浮点数:

    #include 
    #include 
    
    int main() {
        std::complex<float> complexNumber = 1.0 + 2.0if;
        std::cout << complexNumber << std::endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • 相关阅读:
    多机器人仓储巡逻路径规划问题的A*算法实现(附带MATLAB代码)
    leetcode 216. 组合总和 III
    深入理解 XML 和 HTML 之间的区别
    sqli-labs less9详解
    2022/07/22:服务504超时响应告警 - 线程池的秘密
    C++实现单链表的各种操作(数据结构)
    神经网络画图?PPT就够了
    IntelliJ IDEA生成时序图的插件(超级好用)
    ios上架上传构建版本的windows工具
    72.编辑距离 | 583.两个字符串的删除操作
  • 原文地址:https://blog.csdn.net/u010164190/article/details/134061484