UE学习日志#21 C++笔记#7 基础复习7 string和string_view1

注:本文内容来自《C++20高级编程》,作为笔记

1 动态字符串

1.1 C风格的字符串

        1.C语言中,字符串的最后一个字符是null字符(\0),官方将这个null字符定义为NUL,只有一个L。

       2. <cstring>中,strlen()返回的是字符串的长度,而不是实际内存的长度,实际内存在字符串长度的基础上加1,因为结尾的'\0'。

       3. 若char[]的字符串,sizeof()返回实际内存大小,而char*存储的字符串sizeof()返回的是指针的大小,根据平台各不相同,是const char*的大小。

       4. 使用“安全C库”(ISO/IEC TR 24731)标准,能避免一些警告。

1.2 字符串字面量(string literal)

1.2.1 字面量池(literal pooling)

        字符串字面量实际上存储在内存的只读部分,通过这种方式,编译器可重用相同字符串字面量的引用,从而优化内存使用。这种技术称为字面量池(literal pooling)。

char arr[]{"hello"};
arr[1]='a';

        这种情况编译器会创建一个足以放下整个字符串的数组,然后将字符串复制到数组中,而不会将字面量放在只读的内存中,也不会使用字面量池。

1.2.2 原始字符串字面量(raw string literal)

R"(......)"

        这种方式会忽略中间的转义字符,如\t \n等,若想插入)",必须用可选的分隔符,例如:

const char* str{ R"-(Embedded)" characters)-"};

1.3 C++std::string类

        虽然string是一个类,但是几乎可以把string当作内建类型使用。

        重载了+,表示串联字符串。

        重载了+=,表示追加一个字符串。

        string的内存会自动分配,不会出现内存泄漏。

        一些操作:

                substr(pos,len);返回从给定位置开始的给定长度的字符串

                find(str);找到返回位置,没找到返回string::npos

                replace(pos,len,str);指定位置和长度替换为另一个字符串

                starts_with(str)/ends_with(str)如果以一个给定的字符串开头或结尾,返回true

        从C++20开始,std::string是constexpr类,这意味着string可以用于在编译期执行操作,并可用于constexpr函数和类的实现。

        auto string{"Hello World"s };可以把字符串字面量解释为std::string而不是const char*.

        使用vector<string>时要注意vector names{"Name1"s,"Name2"s,"Name3"s};

        string s{to_string(T val)};

        高级数值转换和低级数值转换。

### UE_BUILD_SHIPPING Macro Definition and Configuration In Unreal Engine, `UE_BUILD_SHIPPING` is a preprocessor macro used primarily during the build process when compiling code intended for final product releases. When this macro is defined, it indicates that the engine should compile with optimizations specific to shipping builds while excluding debug information or development-specific functionalities. The inclusion of `UE_BUILD_SHIPPING` ensures that only necessary components are included in the executable, leading to better performance and smaller file sizes suitable for end-user distribution[^1]. To configure projects using `UE_BUILD_SHIPPING`, developers typically adjust settings within `.ini` files such as `DefaultEngine.ini`. For instance, certain features like Distance Field Global Illumination require explicit enabling through console variables set in `ConsoleVariables.ini` alongside ensuring relevant renderer project configurations support these advanced graphical options. Additionally, transitioning towards newer formats for representing mesh assets enhances compatibility and introduces improved workflows over time[^2], although directly related changes might not involve `UE_BUILD_SHIPPING`. For practical demonstration purposes on how to apply `UE_BUILD_SHIPPING`, consider modifying C++ source files where conditional compilation directives check whether `UE_BUILD_SHIPPING` has been defined before including particular pieces of code: ```cpp #if UE_BUILD_SHIPPING // Code block executed exclusively under SHIPPING configuration. #endif ``` --related questions-- 1. How does defining `UE_BUILD_SHIPPING` impact game performance? 2. What other macros exist besides `UE_BUILD_SHIPPING` for different build types? 3. Can custom modules be excluded from shipping builds via `UE_BUILD_SHIPPING`? 4. In what scenarios would one disable features specifically for non-shipping configurations?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值