Cod*_*n42 1 c++ iostream bit-manipulation operators
例如:
int age;
cin >> age;
cout << "You are " << age << " years old!" << endl;
Run Code Online (Sandbox Code Playgroud)
为什么我们在这里使用"<<"和">>"运算符?他们在做什么?我有点理解位移,但我不知道这是如何工作的.
它们被称为流插入运算符(<<)和流提取运算符(>>).
这些运算符与左右位移运算符相同(即使它们具有不同的名称).位移运算符过载,因此当左侧是流时,它们读取或写入该流.
它们就像任何函数调用一样 - 它的工作方式如下:
leftShift(leftShift(leftShift(leftShift(cout, "You are "), age), " years old!"), endl);
Run Code Online (Sandbox Code Playgroud)
除了调用函数operator<<而不是leftShift.
严格来说,没有理由调用一个函数leftShift必须进行左移,同样没有理由调用一个函数operator<<必须进行左移.
| 归档时间: |
|
| 查看次数: |
1284 次 |
| 最近记录: |