查看关键字:
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);

总结

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

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

06-28 13:59