#include <iostream>
#include <vector>
#include <cstdint>
// 定义一个函数,演示如何使用 byte 类型
void demonstrateByteUsage() {
// C++17 引入了 std::byte 类型,它是一个表示字节的类型
std::byte b1 = std::byte{0x0A}; // 初始化一个 byte 类型的变量
// 使用 to_integer 函数将 byte 转换为整数类型进行输出
std::cout << "The value of b1 as an integer is: "
<< static_cast<unsigned int>(std::to_integer<uint8_t>(b1)) << std::endl;
// 创建一个包含多个字节的 vector
std::vector<std::byte> byteVector = {std::byte{0x01}, std::byte{0x02}, std::byte{0x03}};
// 遍历并输出 vector 中的每个字节
for (const auto& byte : byteVector) {
std::cout << "Byte value: "
<< static_cast<unsigned int>(std::to_integer<uint8_t>(byte)) << std::endl;
}
}
int main() {
demonstrateByteUsage();
return 0;
}
std::byte
是 C++17 引入的一个类型,用于表示字节。它不是一个数值类型,因此不能直接进行算术运算。std::to_integer
函数可以将 std::byte
类型转换为整数类型,以便于打印或进行数值运算。std::byte
类型的变量 b1
,并将其初始化为 0x0A
(十六进制表示的 10)。std::vector<std::byte>
来存储多个字节,并遍历输出每个字节的值。上一篇:c++ new delete用法
下一篇:c++ isdigit
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站