C++ basic knowledge of 10,000-word long text analysis! A must-see for beginners

Follow, star public account, direct access to exciting content

Source: Network

Finishing: Li Xiaoyao

ID: Technology makes dreams greater

C++ comments

Program comments are explanatory statements. You can include comments in the C++ code, which will improve the readability of the source code. All programming languages ​​allow some form of comments.

C++ supports single-line comments and multi-line comments. All characters in the comment will be ignored by the C++ compiler.

C++ comments start with /* and end with */. E.g:

/* 这是注释 */

/* C++ 注释也可以
 * 跨行
 */

Comments can also start with // and go to the end of the line. E.g:

#include using namespace std;

main()
{   cout << "Hello World"; // 输出 Hello World     return 0; }

When the above code is compiled, the compiler will ignore  // prints Hello World and finally will produce the following result:

Hello World

Inside the /* and */ comments, the // character has no special meaning. In the // comment, the /* and */ characters also have no special meaning. Therefore, you can nest one type of comment within another. E.g:

/* 用于输出 Hello World 的注释

cout << "Hello World"; // 输出 Hello World  */

C++ data type

When programming in a programming language, various variables are needed to store various information. What a variable retains is the memory location of the value it stores. This means that when you create a variable, some space is reserved in memory.

You may need to store information of various data types (such as character, wide character, integer, floating point, double floating point, boolean, etc.). The operating system will allocate memory and make decisions based on the data type of the variable What is stored in reserved memory.

Basic built-in types

C++ provides programmers with a rich variety of built-in data types and user-defined data types. The following table lists seven basic C++ data types:

Types of Keyword
Boolean bool
Character type char
Integer int
Floating point float
Double float double
No type void
Wide character wchar_t

Some basic types can be modified with one or more type modifiers:

  • signed

  • unsigned

  • short

  • long

The following table shows the memory that various variable types need to store when they store values ​​in the memory, as well as the maximum and minimum values ​​that can be stored in this type of variable.

Types of Bit width range
char 1 byte -127 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -127 to 127
int 4 bytes -2147483648 to 2147483647
unsigned int 4 bytes 0 to 4294967295
signed int 4 bytes -2147483648 to 2147483647
short int 2 bytes -32768 to 32767
unsigned short int Range 0 to 65,535
signed short int Range -32768 to 32767
long int 4 bytes -2,147,483,647 to 2,147,483,647
signed long int 4 bytes Same as long int
unsigned long int 4 bytes 0 to 4,294,967,295
float 4 bytes +/- 3.4e +/- 38 (~7 digits)
double 8 bytes +/- 1.7e +/- 308 (~15 digits)
long double 8 bytes +/- 1.7e +/- 308 (~15 digits)
wchar_t 2 or 4 bytes 1 wide character

As can be seen from the above table, the size of the variable will vary depending on the compiler and the computer used.

The following example will output the size of various data types on your computer.

#include <iostream>using namespace std;int main(){   cout << "Size of char : " << sizeof(char) << endl;   cout << "Size of int : " << sizeof(int) << endl;   cout << "Size of short int : " << sizeof(short int) << endl;   cout << "Size of long int : " << sizeof(long int) << endl;   cout << "Size of float : " << sizeof(float) << endl;   cout << "Size of double : " << sizeof(double) << endl;   cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;   return 0;
}

This example uses  endl , which will insert a newline character after each line, and the << operator is used to pass multiple values ​​to the screen. We also use the  sizeof()  function to get the size of various data types.

When the above code is compiled and executed, it will produce the following results, which will vary depending on the computer used:

Size of char : 1
Size of int : 4
Size of short int : 2
Size of long int : 4
Size of float : 4
Size of double : 8
Size of wchar_t : 4

typedef declaration

You can use  typedef to  give an existing type a new name. Here is the syntax for defining a new type using typedef:

typedef type newname;

For example, the following statement will tell the compiler that feet is another name for int:

typedef int feet;

Now, the following declaration is perfectly legal, it creates an integer variable distance:

feet distance;

Enumerated type

An enumeration type declares an optional type name and a set of identifiers to be used as the value of the type. It has zero or more identifiers that can be used as the value of the type. Each enumerator is a constant of an enumeration type.

创建枚举,需要使用关键字 enum。枚举类型的一般形式为:

enum enum-name { list of names } var-list;

在这里,enum-name 是枚举类型的名称。名称列表 { list of names } 是用逗号分隔的。

例如,下面的代码定义了一个颜色枚举,变量 c 的类型为 color。最后,c 被赋值为 "blue"。

enum color { red, green, blue } c;
c = blue;

默认情况下,第一个名称的值为 0,第二个名称的值为 1,第三个名称的值为 2,以此类推。但是,您也可以给名称赋予一个特殊的值,只需要添加一个初始值即可。例如,在下面的枚举中,green 的值为 5。

enum color { red, green=5, blue };

在这里,blue 的值为 6,因为默认情况下,每个名称都会比它前面一个名称大 1。

C++ 变量作用域

作用域是程序的一个区域,一般来说有三个地方可以声明变量:

  • 在函数或一个代码块内部声明的变量,称为局部变量。

  • 在函数参数的定义中声明的变量,称为形式参数。

  • 在所有函数外部声明的变量,称为全局变量。

我们将在后续的章节中学习什么是函数和参数。本章我们先来讲解声明是局部变量和全局变量。

局部变量

在函数或一个代码块内部声明的变量,称为局部变量。它们只能被函数内部或者代码块内部的语句使用。下面的实例使用了局部变量:

#include <iostream>
using namespace std;
 
int main ()
{
  // 局部变量声明
  int a, b;
  int c;
 
  // 实际初始化
  a = 10;
  b = 20;
  c = a + b;
 
  cout << c;     return 0;
}

全局变量

在所有函数外部定义的变量(通常是在程序的头部),称为全局变量。全局变量的值在程序的整个生命周期内都是有效的。

全局变量可以被任何函数访问。也就是说,全局变量一旦声明,在整个程序中都是可用的。下面的实例使用了全局变量和局部变量:

#include <iostream>using namespace std;
// 全局变量声明int g;
int main (){  // 局部变量声明
  int a, b;
  // 实际初始化
  a = 10;
  b = 20;
  g = a + b;
  cout << g;     return 0;
 }

在程序中,局部变量和全局变量的名称可以相同,但是在函数内,局部变量的值会覆盖全局变量的值。下面是一个实例:

#include <iostream>using namespace std;
// 全局变量声明int g = 20;
int main (){  // 局部变量声明
  int g = 10;
  cout << g;     return 0;
}

当上面的代码被编译和执行时,它会产生下列结果:

10

初始化局部变量和全局变量

当局部变量被定义时,系统不会对其初始化,您必须自行对其初始化。定义全局变量时,系统会自动初始化为下列值:

数据类型 初始化默认值
int 0
char '\0'
float 0
double 0
pointer NULL

正确地初始化变量是一个良好的编程习惯,否则有时候程序可能会产生意想不到的结果。

C++ 常量

常量是固定值,在程序执行期间不会改变。这些固定的值,又叫做字面量

常量可以是任何的基本数据类型,可分为整型数字、浮点数字、字符、字符串和布尔值。

常量就像是常规的变量,只不过常量的值在定义后不能进行修改。

整数常量

整数常量可以是十进制、八进制或十六进制的常量。前缀指定基数:0x 或 0X 表示十六进制,0 表示八进制,不带前缀则默认表示十进制。

整数常量也可以带一个后缀,后缀是 U 和 L 的组合,U 表示无符号整数(unsigned),L 表示长整数(long)。后缀可以是大写,也可以是小写,U 和 L 的顺序任意。

下面列举几个整数常量的实例:

212         // 合法的
215u        // 合法的
0xFeeL      // 合法的
078         // 非法的:8 不是八进制的数字
032UU       // 非法的:不能重复后缀

以下是各种类型的整数常量的实例:

85         // 十进制
0213       // 八进制
0x4b       // 十六进制
30         // 整数
30u        // 无符号整数
30l        // 长整数
30ul       // 无符号长整数

浮点常量

浮点常量由整数部分、小数点、小数部分和指数部分组成。您可以使用小数形式或者指数形式来表示浮点常量。

当使用小数形式表示时,必须包含小数点、指数,或同时包含两者。当使用指数形式表示时,必须包含整数部分、小数部分,或同时包含两者。带符号的指数是用 e 或 E 引入的。

下面列举几个浮点常量的实例:

3.14159       // 合法的
314159E-5L    // 合法的
510E          // 非法的:不完整的指数
210f          // 非法的:没有小数或指数
.e55          // 非法的:缺少整数或分数

布尔常量

布尔常量共有两个,它们都是标准的 C++ 关键字:

  • true 值代表真。

  • false 值代表假。

我们不应把 true 的值看成 1,把 false 的值看成 0。

字符常量

字符常量是括在单引号中。如果常量以 L(仅当大写时)开头,则表示它是一个宽字符常量(例如 L'x'),此时它必须存储在 wchar_t 类型的变量中。否则,它就是一个窄字符常量(例如 'x'),此时它可以存储在 char 类型的简单变量中。

字符常量可以是一个普通的字符(例如 'x')、一个转义序列(例如 '\t'),或一个通用的字符(例如 '\u02C0')。

在 C++ 中,有一些特定的字符,当它们前面有反斜杠时,它们就具有特殊的含义,被用来表示如换行符(\n)或制表符(\t)等。下表列出了一些这样的转义序列码:

转义序列 含义
\\ \ 字符
\' ' 字符
\" " 字符
\? ? 字符
\a 警报铃声
\b 退格键
\f 换页符
\n 换行符
\r 回车
\t 水平制表符
\v 垂直制表符
\ooo 一到三位的八进制数
\xhh . . . 一个或多个数字的十六进制数

下面的实例显示了一些转义序列字符:

#include <iostream>using namespace std;int main(){   cout << "Hello\tWorld\n\n";   return 0;
}

当上面的代码被编译和执行时,它会产生下列结果:

Hello   World

字符串常量

字符串字面值或常量是括在双引号 "" 中的。一个字符串包含类似于字符常量的字符:普通的字符、转义序列和通用的字符。

您可以使用空格做分隔符,把一个很长的字符串常量进行分行。

下面的实例显示了一些字符串常量。下面这三种形式所显示的字符串是相同的。

"hello, dear""hello, \

dear""hello, " "d" "ear"

定义常量

在 C++ 中,有两种简单的定义常量的方式:

  • 使用 #define 预处理器。

  • 使用 const 关键字。

#define 预处理器

下面是使用 #define 预处理器定义常量的形式:

#define identifier value

具体请看下面的实例:

#include <iostream>using namespace std;#define LENGTH 10   #define WIDTH  5#define NEWLINE '\n'int main(){   int area;  
  
   area = LENGTH * WIDTH;   cout << area;   cout << NEWLINE;   return 0;
}

当上面的代码被编译和执行时,它会产生下列结果:

50

const 关键字

您可以使用 const 前缀声明指定类型的常量,如下所示:

const type variable = value;

具体请看下面的实例:

#include <iostream>using namespace std;int main(){   const int  LENGTH = 10;   const int  WIDTH  = 5;   const char NEWLINE = '\n';   int area;  
  
   area = LENGTH * WIDTH;   cout << area;   cout << NEWLINE;   return 0;
}

当上面的代码被编译和执行时,它会产生下列结果:

50

请注意,把常量定义为大写字母形式,是一个很好的编程实践

C++ 修饰符类型

C++ 允许在 char、int 和 double 数据类型前放置修饰符。修饰符用于改变基本类型的含义,所以它更能满足各种情境的需求。

下面列出了数据类型修饰符:

  • signed

  • unsigned

  • long

  • short

修饰符 signed、unsigned、long 和 short 可应用于整型,signed 和 unsigned 可应用于字符型,long 可应用于双精度型。

修饰符 signed 和 unsigned 也可以作为 long 或 short 修饰符的前缀。例如:unsigned long int

C++ 允许使用速记符号来声明无符号短整数无符号长整数。您可以不写 int,只写单词 unsigned short 或 unsigned long,int 是隐含的。例如,下面的两个语句都声明了无符号整型变量。

unsigned x;
unsigned int y;

为了理解 C++ 解释有符号整数和无符号整数修饰符之间的差别,我们来运行一下下面这个短程序:

#include <iostream>using namespace std;
/*
 * 这个程序演示了有符号整数和无符号整数之间的差别
*/int main(){   short int i;           // 有符号短整数
   short unsigned int j;  // 无符号短整数

   j = 50000;

   i = j;   cout << i << " " << j;   return 0;
}

当上面的程序运行时,会输出下列结果:

-15536 50000

上述结果中,无符号短整数 50,000 的位模式被解释为有符号短整数 -15,536。

C++ 中的类型限定符

类型限定符提供了变量的额外信息。

限定符 含义
const const 类型的对象在程序执行期间不能被修改改变。
volatile 修饰符 volatile 告诉编译器,变量的值可能以程序未明确指定的方式被改变。
restrict 由 restrict 修饰的指针是唯一一种访问它所指向的对象的方式。只有 C99 增加了新的类型限定符 restrict。

C++ 存储类

存储类定义 C++ 程序中变量/函数的范围(可见性)和生命周期。这些说明符放置在它们所修饰的类型之前。下面列出 C++ 程序中可用的存储类:

  • auto

  • register

  • static

  • extern

  • mutable

auto 存储类

auto 存储类是所有局部变量默认的存储类。

{
   int mount;
   auto int month;
}

上面的实例定义了两个带有相同存储类的变量,auto 只能用在函数内,即 auto 只能修饰局部变量。

register 存储类

register 存储类用于定义存储在寄存器中而不是 RAM 中的局部变量。这意味着变量的最大尺寸等于寄存器的大小(通常是一个词),且不能对它应用一元的 '&' 运算符(因为它没有内存位置)。

{
   register int  miles;
}

寄存器只用于需要快速访问的变量,比如计数器。还应注意的是,定义 'register' 并不意味着变量将被存储在寄存器中,它意味着变量可能存储在寄存器中,这取决于硬件和实现的限制。

static 存储类

static 存储类指示编译器在程序的生命周期内保持局部变量的存在,而不需要在每次它进入和离开作用域时进行创建和销毁。因此,使用 static 修饰局部变量可以在函数调用之间保持局部变量的值。

static 修饰符也可以应用于全局变量。当 static 修饰全局变量时,会使变量的作用域限制在声明它的文件内。

在 C++ 中,当 static 用在类数据成员上时,会导致仅有一个该成员的副本被类的所有对象共享。

#include <iostream>
 // 函数声明 void func(void);
static int count = 10; /* 全局变量 */
 int main(){    while(count--)
    {
       func();
    }    return 0;
}// 函数定义void func( void ){    static int i = 5; // 局部静态变量
    i++;    std::cout << "变量 i 为 " << i ;    std::cout << " , 变量 count 为 " << count << std::endl;
}

当上面的代码被编译和执行时,它会产生下列结果:

变量 i 为 6 , 变量 count 为 9
变量 i 为 7 , 变量 count 为 8
变量 i 为 8 , 变量 count 为 7
变量 i 为 9 , 变量 count 为 6
变量 i 为 10 , 变量 count 为 5
变量 i 为 11 , 变量 count 为 4
变量 i 为 12 , 变量 count 为 3
变量 i 为 13 , 变量 count 为 2
变量 i 为 14 , 变量 count 为 1
变量 i 为 15 , 变量 count 为 0

extern 存储类

extern 存储类用于提供一个全局变量的引用,全局变量对所有的程序文件都是可见的。当您使用 'extern' 时,对于无法初始化的变量,会把变量名指向一个之前定义过的存储位置。

当您有多个文件且定义了一个可以在其他文件中使用的全局变量或函数时,可以在其他文件中使用 extern 来得到已定义的变量或函数的引用。可以这么理解,extern 是用来在另一个文件中声明一个全局变量或函数。

extern 修饰符通常用于当有两个或多个文件共享相同的全局变量或函数的时候,如下所示:

第一个文件:main.cpp

#include <iostream>
 int count ;extern void write_extern();
 
main()
{
   count = 5;
   write_extern();
}

第二个文件:support.cpp

#include <iostream>
 extern int count;
void write_extern(void){   std::cout << "Count is " << count << std::endl;
}

在这里,第二个文件中的 extern 关键字用于声明已经在第一个文件 main.cpp 中定义的 count。现在 ,编译这两个文件,如下所示:

$g++ main.cpp support.cpp -o write

这会产生 write 可执行程序,尝试执行 write,它会产生下列结果:

$ ./write
Count is 5

mutable 存储类

mutable 说明符仅适用于类的对象,这将在本教程的最后进行讲解。它允许对象的成员替代常量。也就是说,mutable 成员可以通过 const 成员函数修改。

C++ 运算符

运算符是一种告诉编译器执行特定的数学或逻辑操作的符号。C++ 内置了丰富的运算符,并提供了以下类型的运算符:

  • 算术运算符

  • 关系运算符

  • 逻辑运算符

  • 位运算符

  • 赋值运算符

  • 杂项运算符

本章将逐一介绍算术运算符、关系运算符、逻辑运算符、位运算符、赋值运算符和其他运算符。

算术运算符

下表显示了 C++ 支持的所有算术运算符。

假设变量 A 的值为 10,变量 B 的值为 20,则:

运算符 描述 实例
+ 把两个操作数相加 A + B 将得到 30
- 从第一个操作数中减去第二个操作数 A - B 将得到 -10
* 把两个操作数相乘 A * B 将得到 200
/ 分子除以分母 B / A 将得到 2
% 取模运算符,整除后的余数 B % A 将得到 0
++ 自增运算符,整数值增加 1 A++ 将得到 11
-- 自减运算符,整数值减少 1 A-- 将得到 9

实例

请看下面的实例,了解 C++ 中所有可用的算术运算符。

复制并黏贴下面的 C++ 程序到 test.cpp 文件中,编译并运行程序。

#include <iostream>using namespace std;
 
main()
{   int a = 21;   int b = 10;   int c ;
 
   c = a + b;   cout << "Line 1 - c 的值是 " << c << endl ;
    c = a - b;
    cout << "Line 2 - c 的值是 " << c << endl ;
    c = a * b;
    cout << "Line 3 - c 的值是 " << c << endl ;
    c = a / b;
    cout << "Line 4 - c 的值是 " << c << endl ;
    c = a % b;
    cout << "Line 5 - c 的值是 " << c << endl ;
    c = a++;
    cout << "Line 6 - c 的值是 " << c << endl ;
    c = a--;
    cout << "Line 7 - c 的值是 " << c << endl ;return 0; }

当上面的代码被编译和执行时,它会产生下列结果:

Line 1 - c 的值是 31
Line 2 - c 的值是 11
Line 3 - c 的值是 210
Line 4 - c 的值是 2
Line 5 - c 的值是 1
Line 6 - c 的值是 21
Line 7 - c 的值是 22

关系运算符

下表显示了 C++ 支持的所有关系运算符。

假设变量 A 的值为 10,变量 B 的值为 20,则:

运算符 描述 实例
== 检查两个操作数的值是否相等,如果相等则条件为真。 (A == B) 不为真。
!= 检查两个操作数的值是否相等,如果不相等则条件为真。 (A != B) 为真。
> 检查左操作数的值是否大于右操作数的值,如果是则条件为真。 (A > B) 不为真。
< 检查左操作数的值是否小于右操作数的值,如果是则条件为真。 (A < B) 为真。
>= 检查左操作数的值是否大于或等于右操作数的值,如果是则条件为真。 (A >= B) 不为真。
<= 检查左操作数的值是否小于或等于右操作数的值,如果是则条件为真。 (A <= B) 为真。

实例

请看下面的实例,了解 C++ 中所有可用的关系运算符。

复制并黏贴下面的 C++ 程序到 test.cpp 文件中,编译并运行程序。

#include <iostream>using namespace std;

main()
{   int a = 21;   int b = 10;   int c ;   if( a == b )
   {      cout << "Line 1 - a 等于 b" << endl ;
     }else{
    cout << "Line 1 - a 不等于 b" << endl ;
    }
    if ( a < b ) {
        cout << "Line 2 - a 小于 b" << endl ;
    }else{
        cout << "Line 2 - a 不小于 b" << endl ;
    }
    if ( a > b ){      cout << "Line 3 - a 大于 b" << endl ;
    }else{
        cout << "Line 3 - a 不大于 b" << endl ;
    }    /* 改变 a 和 b 的值 */    a = 5;
    b = 20;
    if ( a <= b ){
    cout << "Line 4 - a 小于或等于 b" << endl ;
    }
    if ( b >= a ){      cout << "Line 5 - b 大于或等于 b" << endl ;
    }
    return 0;
}

当上面的代码被编译和执行时,它会产生下列结果:

Line 1 - a 不等于 b
Line 2 - a 不小于 b
Line 3 - a 大于 b
Line 4 - a 小于或等于 b
Line 5 - b 大于或等于 b

逻辑运算符

下表显示了 C++ 支持的所有关系逻辑运算符。

假设变量 A 的值为 1,变量 B 的值为 0,则:

运算符 描述 实例
&& 称为逻辑与运算符。如果两个操作数都非零,则条件为真。 (A && B) 为假。
|| 称为逻辑或运算符。如果两个操作数中有任意一个非零,则条件为真。 (A || B) 为真。
! 称为逻辑非运算符。用来逆转操作数的逻辑状态。如果条件为真则逻辑非运算符将使其为假。 !(A && B) 为真。

实例

请看下面的实例,了解 C++ 中所有可用的逻辑运算符。

复制并黏贴下面的 C++ 程序到 test.cpp 文件中,编译并运行程序。

#include <iostream>using namespace std;

main()
{   int a = 5;   int b = 20;   int c ;   if ( a && b )
   {      cout << "Line 1 - 条件为真"<< endl ;
    }
    if ( a || b )
    {
        cout << "Line 2 - 条件为真"<< endl ;
    }    /* 改变 a 和 b 的值 */    a = 0;
    b = 10;
    if ( a && b )
    {
        cout << "Line 3 - 条件为真"<< endl ;
    }else{
        cout << "Line 4 - 条件不为真"<< endl ;
    }
    if ( !(a && b) ){
    cout << "Line 5 - 条件为真"<< endl ;
    }
    return 0;
}

当上面的代码被编译和执行时,它会产生下列结果:

Line 1 - 条件为真
Line 2 - 条件为真
Line 3 - 条件不为真
Line 4 - 条件为真

位运算符

位运算符作用于位,并逐位执行操作。&、 | 和 ^ 的真值表如下所示:

p q p & q p | q p ^ q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

假设如果 A = 60,且 B = 13,现在以二进制格式表示,它们如下所示:

A = 0011 1100

B = 0000 1101

-----------------

A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~A  = 1100 0011

下表显示了 C++ 支持的位运算符。假设变量 A 的值为 60,变量 B 的值为 13,则:

运算符 描述 实例
& 如果同时存在于两个操作数中,二进制 AND 运算符复制一位到结果中。 (A & B) 将得到 12,即为 0000 1100
| 如果存在于任一操作数中,二进制 OR 运算符复制一位到结果中。 (A | B) 将得到 61,即为 0011 1101
^ 如果存在于其中一个操作数中但不同时存在于两个操作数中,二进制异或运算符复制一位到结果中。 (A ^ B) 将得到 49,即为 0011 0001
~ 二进制补码运算符是一元运算符,具有"翻转"位效果。 (~A ) 将得到 -61,即为 1100 0011,2 的补码形式,带符号的二进制数。
<< 二进制左移运算符。左操作数的值向左移动右操作数指定的位数。 A << 2 将得到 240,即为 1111 0000
>> 二进制右移运算符。左操作数的值向右移动右操作数指定的位数。 A >> 2 将得到 15,即为 0000 1111

实例

请看下面的实例,了解 C++ 中所有可用的位运算符。

复制并黏贴下面的 C++ 程序到 test.cpp 文件中,编译并运行程序。

#include <iostream>using namespace std;

main()
{   unsigned int a = 60;     // 60 = 0011 1100  
   unsigned int b = 13;    // 13 = 0000 1101
   int c = 0;          

   c = a & b;             // 12 = 0000 1100
   cout << "Line 1 - c 的值是 " << c << endl ;
    c = a | b;             // 61 = 0011 1101    cout << "Line 2 - c 的值是 " << c << endl ;
    c = a ^ b;             // 49 = 0011 0001    cout << "Line 3 - c 的值是 " << c << endl ;
    c = ~a;                // -61 = 1100 0011    cout << "Line 4 - c 的值是 " << c << endl ;
    c = a << 2;            // 240 = 1111 0000    cout << "Line 5 - c 的值是 " << c << endl ;
    c = a >> 2;            // 15 = 0000 1111
    cout << "Line 6 - c 的值是 " << c << endl ;
    return 0; }

当上面的代码被编译和执行时,它会产生下列结果:

Line 1 - c 的值是 12
Line 2 - c 的值是 61
Line 3 - c 的值是 49
Line 4 - c 的值是 -61
Line 5 - c 的值是 240
Line 6 - c 的值是 15

赋值运算符

下表列出了 C++ 支持的赋值运算符:

运算符 描述 实例
= 简单的赋值运算符,把右边操作数的值赋给左边操作数 C = A + B 将把 A + B 的值赋给 C
+= 加且赋值运算符,把右边操作数加上左边操作数的结果赋值给左边操作数 C += A 相当于 C = C + A
-= 减且赋值运算符,把左边操作数减去右边操作数的结果赋值给左边操作数 C -= A 相当于 C = C - A
*= 乘且赋值运算符,把右边操作数乘以左边操作数的结果赋值给左边操作数 C *= A 相当于 C = C * A
/= 除且赋值运算符,把左边操作数除以右边操作数的结果赋值给左边操作数 C /= A 相当于 C = C / A
%= 求模且赋值运算符,求两个操作数的模赋值给左边操作数 C %= A 相当于 C = C % A
<<= 左移且赋值运算符 C <<= 2 等同于 C = C << 2
>>= 右移且赋值运算符 C >>= 2 等同于 C = C >> 2
&= 按位与且赋值运算符 C &= 2 等同于 C = C & 2
^= 按位异或且赋值运算符 C ^= 2 等同于 C = C ^ 2
|= 按位或且赋值运算符 C |= 2 等同于 C = C | 2

实例

请看下面的实例,了解 C++ 中所有可用的赋值运算符。

复制并黏贴下面的 C++ 程序到 test.cpp 文件中,编译并运行程序。

#include <iostream>using namespace std;

main()
{   int a = 21;   int c ;

   c =  a;   cout << "Line 1 - =  运算符实例,c 的值 = : " <<c<< endl ;
    c +=  a;
    cout << "Line 2 - += 运算符实例,c 的值 = : " <<c<< endl ;
    c -=  a;
    cout << "Line 3 - -= 运算符实例,c 的值 = : " <<c<< endl ;
    c *=  a;
    cout << "Line 4 - *= 运算符实例,c 的值 = : " <<c<< endl ;
    c /=  a;
    cout << "Line 5 - /= 运算符实例,c 的值 = : " <<c<< endl ;
    c  = 200;
    c %=  a;
    cout << "Line 6 - %= 运算符实例,c 的值 = : " <<c<< endl ;
    c <<=  2;
    cout << "Line 7 - <<= 运算符实例,c 的值 = : " <<c<< endl ;
    c >>=  2;    cout << "Line 8 - >>= 运算符实例,c 的值 = : " <<c<< endl ;
    c &=  2;
    cout << "Line 9 - &= 运算符实例,c 的值 = : " <<c<< endl ;
    c ^=  2;
    cout << "Line 10 - ^= 运算符实例,c 的值 = : " <<c<< endl ;
    c |=  2;
    cout << "Line 11 - |= 运算符实例,c 的值 = : " <<c<< endl ;
    return 0;
}

当上面的代码被编译和执行时,它会产生下列结果:

Line 1 - =  运算符实例,c 的值 = 21
Line 2 - += 运算符实例,c 的值 = 42
Line 3 - -= 运算符实例,c 的值 = 21
Line 4 - *= 运算符实例,c 的值 = 441
Line 5 - /= 运算符实例,c 的值 = 21
Line 6 - %= 运算符实例,c 的值 = 11
Line 7 - <<= 运算符实例,c 的值 = 44 Line 8 - >>= 运算符实例,c 的值 = 11
Line 9 - &= 运算符实例,c 的值 = 2
Line 10 - ^= 运算符实例,c 的值 = 0
Line 11 - |= 运算符实例,c 的值 = 2

杂项运算符

下表列出了 C++ 支持的其他一些重要的运算符。

运算符 描述
sizeof sizeof 运算符返回变量的大小。例如,sizeof(a) 将返回 4,其中 a 是整数。
Condition ? X : Y 条件运算符。如果 Condition 为真 ? 则值为 X : 否则值为 Y。
, 逗号运算符会顺序执行一系列运算。整个逗号表达式的值是以逗号分隔的列表中的最后一个表达式的值。
.(点)和 ->(箭头) 成员运算符用于引用类、结构和共用体的成员。
Cast 强制转换运算符把一种数据类型转换为另一种数据类型。例如,int(2.2000) 将返回 2。
& 指针运算符 & 返回变量的地址。例如 &a; 将给出变量的实际地址。
* 指针运算符 * 指向一个变量。例如,*var; 将指向变量 var。

C++ 中的运算符优先级

运算符的优先级确定表达式中项的组合。这会影响到一个表达式如何计算。某些运算符比其他运算符有更高的优先级,例如,乘除运算符具有比加减运算符更高的优先级。

例如 x = 7 + 3 * 2,在这里,x 被赋值为 13,而不是 20,因为运算符 * 具有比 + 更高的优先级,所以首先计算乘法 3*2,然后再加上 7。

下表将按运算符优先级从高到低列出各个运算符,具有较高优先级的运算符出现在表格的上面,具有较低优先级的运算符出现在表格的下面。在表达式中,较高优先级的运算符会优先被计算。

类别  运算符  结合性 
后缀  () [] -> . ++ - -   从左到右 
一元  + - ! ~ ++ - - (type)* & sizeof  从右到左 
乘除  * / %  从左到右 
加减  + -  从左到右 
移位  << >>  从左到右 
关系  < <= > >=  从左到右 
相等  == !=  从左到右 
位与 AND  从左到右 
位异或 XOR  从左到右 
位或 OR  从左到右 
逻辑与 AND  &&  从左到右 
逻辑或 OR  ||  从左到右 
条件  ?:  从右到左 
赋值  = += -= *= /= %=>>= <<= &= ^= |=  从右到左 
逗号  从左到右 

实例

请看下面的实例,了解 C++ 中运算符的优先级。

复制并黏贴下面的 C++ 程序到 test.cpp 文件中,编译并运行程序。

对比有括号和没有括号时的区别,这将产生不同的结果。因为 ()、 /、 * 和 + 有不同的优先级,高优先级的操作符将优先计算。

#include <iostream>using namespace std;
 
main()
{   int a = 20;   int b = 10;   int c = 15;   int d = 5;   int e;
 
   e = (a + b) * c / d;      // ( 30 * 15 ) / 5
   cout << "(a + b) * c / d 的值是 " << e << endl ;

   e = ((a + b) * c) / d;    // (30 * 15 ) / 5
   cout << "((a + b) * c) / d 的值是 " << e << endl ;

   e = (a + b) * (c / d);   // (30) * (15/5)
   cout << "(a + b) * (c / d) 的值是 " << e << endl ;

   e = a + (b * c) / d;     //  20 + (150/5)
   cout << "a + (b * c) / d 的值是 " << e << endl ;  
   return 0;
}

当上面的代码被编译和执行时,它会产生下列结果:

(a + b) * c / d 的值是 90
((a + b) * c) / d 的值是 90
(a + b) * (c / d) 的值是 90
a + (b * c) / d 的值是 50

推荐阅读:

嵌入式编程专辑
Linux 学习专辑
C/C++编程专辑
关注微信公众号『技术让梦想更伟大』,后台回复“m”查看更多内容,回复“加群”加入技术交流群。



长按前往图中包含的公众号关注

Guess you like

Origin blog.csdn.net/u012846795/article/details/107948261