C++:bool关键字&字符类型char&代用关键字

查看关键字:
https://zh.cppreference.com/w/cpp/keyword

C++的bool关键字

(1)bool类型也叫逻辑类型,是个2值enum,值为true或false(这2个也是C++关键字)
(2)C语言没有bool关键字,不源生支持bool类型,一般用typedef int bool;这样来自定义
(3)C++语言源生支持bool类型,一般占1字节(平台相关),用法没什么差异
(4)bool内建和自定义至少有一个差别:函数重载机制认为bool是不同类型

字符类型char

  • char
    (1)字符类型,一般占1字节,表示字符(ASCI或unicode字符)
    (2)从C++14开始char默认是unsigned还是signed取决于目标平台,如arm默认unsigned,而X64默认是signed,建议如果在意符号最好显式使用unsigned char或signed char
    (3)char类型cout输出默认为字符,而int类型cout输出默认为数字
    (4)1 == sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)
  • wchar_t
    (1)宽字符,用于应对一个字符编码超过1字节的Unicode编码
    (2)wchar_t和char的数组都能存下unicode码,区别是需要几个单元才能存一个字
    (3)wchar_t占几个字节取决于具体实现,可能是unsigned short也可能是int
    (4)wchar_t要用wcin和wcout来输入输出,对应字符串为wstring
  • 指定具体字节数的字符类型
    (1)char8_t (C++20 起) char16_t (C++11 起) char32_t (C++11 起)
    (2)这三个类型一个套路,最大特征就是明确指定占用字节数,且都是无符号的
    (3)char8_t很大程度上等同于unsigned char
    (4)关于char8_t可以参考:https://stackoverflow.com/questions/57402464/is-c20-char8-t-the-same-as-our-old-char
    (5)C++20起,新增字符串类u8string, u16string, u32string

C++中无明显变化的关键字

if
else
for
do
while
break
continue
switch
case
default
goto
return
unsigned
signed
float
double
short
int
long
void
sizeof
register
volatile
extern
typedef
asm

C++中新增的运算符代用关键字

  • 逻辑运算代用关键字
    and &&
    or ||
    not !
  // Test logical AND (&&)
  assert((true && true) == true);
  assert((true && false) == false);
  assert((false && true) == false);
  assert((false && false) == false);

  // Test logical OR (||)
  assert((true || true) == true);
  assert((true || false) == true);
  assert((false || true) == true);
  assert((false || false) == false);

  // Test logical NOT (!)
  assert((!true) == false);
  assert((!false) == true);

  // Test logical AND (and)
  assert((true and true) == true);
  assert((true and false) == false);
  assert((false and true) == false);
  assert((false and false) == false);

  // Test logical OR (or)
  assert((true or true) == true);
  assert((true or false) == true);
  assert((false or true) == true);
  assert((false or false) == false);

  // Test logical NOT (not)
  assert((not true) == false);
  assert((not false) == true);
  • 位运算代用关键字
    bitand &
    bitor |
    xor ^
    and_eq &=
    or_eq |=
    xor_eq ^=
    compl ~
    // Test bitwise AND (&)
    assert((5 & 3) == 1);         // 0101 & 0011 = 0001
    assert((4 & 2) == 0);         // 0100 & 0010 = 0000

    // Test bitwise OR (|)
    assert((5 | 3) == 7);         // 0101 | 0011 = 0111
    assert((4 | 2) == 6);         // 0100 | 0010 = 0110

    // Test bitwise XOR (^)
    assert((5 ^ 3) == 6);         // 0101 ^ 0011 = 0110
    assert((4 ^ 2) == 6);         // 0100 ^ 0010 = 0110

    // Test bitwise NOT (~)
    assert((~5) == -6);           // ~0101 = ...11111010 (2's complement representation)
    assert((~0) == -1);           // ~0000 = ...11111111

    // Test bitwise AND (bitand)
    assert((5 bitand 3) == 1);    // 0101 bitand 0011 = 0001
    assert((4 bitand 2) == 0);    // 0100 bitand 0010 = 0000

    // Test bitwise OR (bitor)
    assert((5 bitor 3) == 7);     // 0101 bitor 0011 = 0111
    assert((4 bitor 2) == 6);     // 0100 bitor 0010 = 0110

    // Test bitwise XOR (xor)
    assert((5 xor 3) == 6);       // 0101 xor 0011 = 0110
    assert((4 xor 2) == 6);       // 0100 xor 0010 = 0110

    // Test bitwise NOT (compl)
    assert((compl 5) == -6);      // compl 0101 = ...11111010 (2's complement representation)
    assert((compl 0) == -1);      // compl 0000 = ...11111111

    // Test bitwise AND assignment (&=)
    int a = 5;
    a &= 3;                       // 0101 &= 0011 = 0001
    assert(a == 1);

    // Test bitwise OR assignment (|=)
    a = 4;
    a |= 2;                       // 0100 |= 0010 = 0110
    assert(a == 6);

    // Test bitwise XOR assignment (^=)
    a = 5;
    a ^= 3;                       // 0101 ^= 0011 = 0110
    assert(a == 6);

    // Test bitwise AND assignment (and_eq)
    a = 5;
    a and_eq 3;                   // 0101 and_eq 0011 = 0001
    assert(a == 1);

    // Test bitwise OR assignment (or_eq)
    a = 4;
    a or_eq 2;                    // 0100 or_eq 0010 = 0110
    assert(a == 6);

    // Test bitwise XOR assignment (xor_eq)
    a = 5;
    a xor_eq 3;                   // 0101 xor_eq 0011 = 0110
    assert(a == 6);

  • 不等判断运算符代用关键字
    not_eq !=
    // Test logical NOT EQUAL (!=)
    assert((5 != 3) == true);
    assert((5 != 5) == false);
    assert((3 != 3) == false);
    assert((3 != 5) == true);

    // Test logical NOT EQUAL (not_eq)
    assert((5 not_eq 3) == true);
    assert((5 not_eq 5) == false);
    assert((3 not_eq 3) == false);
    assert((3 not_eq 5) == true);

总结

了解关键字,及其相关用法

学习记录,侵权联系删除。
来源:朱老师物联网大课堂

  • 22
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

li星野

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值