如何在COUT语句中使用移动运算符
how to use shift operator in cout statement as it is overloaded
我是C 的新手,我知道C 中的Shift Operator被超载。但是,正如我们如何在C中的printf语句中进行换档操作一样,我们可以在Cout语句中进行类似的换档操作。
好吧,尝试一下...
#include <iostream>
int main() {
int k = 1;
std::cout << (k << 1) << std::endl; // Correct shifting - notice the parentheses
std::cout << k << 1 << std::endl; // Wrong
return 0;
}
输出:
2
11
这里重要的是用于<<
操作员的变量的类型。
括号导致它为int << int
,这是位移动。如果没有括号,它将是ostream << int
,它将将int
写入流。
相关文章:
- 自定义先决条件对移动分配运算符有效吗
- 移动赋值运算符;尝试引用已删除的函数.我该如何解决这个问题?
- C++ - 没有自定义交换功能的移动分配运算符?
- 强制复制分配超过移动分配运算符
- 编译器调用复制运算符而不是移动运算符
- 对 r 值使用移动赋值运算符时的异常
- 移动语义和运算符 + 重载
- 运算符+ 的规范实现涉及额外的移动构造函数
- 在之后仍需要使用源对象时调用父移动分配运算符
- 删除复制构造函数是否也会删除默认的复制/移动运算符?
- 如何在COUT语句中使用移动运算符
- 为什么移动运算符不只是析构函数+移动构造函数
- VC2013 移动运算符不递归执行移动
- 使用模板移动运算符
- C++ - 组合复制/移动运算符和构造函数
- 在 C++11 中编写复制/移动/运算符 = 三重奏的"correct"方法是什么?
- 在赋值移动运算符内部使用交换
- 在移动运算符/赋值运算符中,我应该使用std::move还是std::forward
- 我应该在什么条件下考虑实现移动构造函数和移动运算符
- c++移动运算符类成员