C++对and、or等运算符的支持

// test.c
// gcc test.c  --- compile error
#include <stdio.h>
int main()
{
  int a = (0) or (0);
  printf("%d", a);
  return a;
}

上面这段c代码能编译通过吗?答案是不能。gcc test.c


增加一个头文件,换成这样呢:

// test.c
// gcc test.c  --- compile success
#include <stdio.h>
#include <iso646.h>
int main()
{
  int a = (0) or (0);
  printf("%d", a);
  return a;
}

 答案是能! 

对于C++,不增加头文件,依然能编译过下面的test.cpp文件。

// test.cpp
// g++ test.cpp     ---- compile success<pre name="code" class="cpp">#include <stdio.h>
#include <stdio.h>
int main()
{
  int a = (0) or (0);
  printf("%d", a);
  return a;
}

 

Oh shit, g++什么时候自动支持这么”牛逼“的运算符了,这不是php和python的专利吗?不光是and or,ISO646标准一口气支持了一堆运算符:

定义为
and&&
and_eq&=
bitand&
bitor|
compl~
not!
not_eq!=
or||
or_eq|=
xor^
xor_eq^=

为什么这么做:http://zh.wikipedia.org/wiki/Iso646.h

做到了什么程度:http://zh.wikipedia.org/wiki/ISO/IEC_646

之所以发本文,是发现OceanBase代码中我写了一个or,被千拂同学review的时候发现。不过,居然没有被编译器发现。最近php写多了是么?


  • 20
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
·条款一:指针与引用的区别  ·条款二:尽量使用C++风格的类型转换  ·条款三:不要使用多态性数组  ·条款四:避免无用的缺省构造函数  ·条款五:谨慎定义类型转换函数  ·条款六:自增(increment)、自减(decrement)操作符前缀形式与后缀形式的区别  ·条款七:不要重载&&,||, or ,  ·条款八:理解各种不同含义的new和delete  ·条款九:使用析构函数防止资源泄漏  ·条款十:在构造函数中防止资源泄漏  ·条款十一:禁止异常信息(exceptions)传递到析构函数外  ·条款十二:理解“抛出一个异常”与“传递一个参数”或“调用一个虚函数”间的差异  ·条款十三:通过引用(reference)捕获异常  ·条款十四:审慎使用异常规格(exception specifications)  ·条款十五:了解异常处理的系统开销  ·条款十六:牢记80-20准则(80-20 rule)  ·条款十七:考虑使用lazy evaluation(懒惰计算法)  ·条款十八:分期摊还期望的计算  ·条款十九:理解临时对象的来源  ·条款二十:协助完成返回值优化  ·条款二十一:通过重载避免隐式类型转换  ·条款二十二:考虑用运算符的赋值形式(op=)取代其单独形式(op)  ·条款二十三:考虑变更程序库  ·条款二十四:理解虚拟函数、多继承、虚基类和RTTI所需的代价  ·条款二十五:将构造函数和非成员函数虚拟化  ·条款二十六:限制某个类所能产生的对象数量  ·条款二十七:要求或禁止在堆中产生对象  ·条款二十八:灵巧(smart)指针
Product Description The author uses practical, concise code examples to illuminate a useful programming stratagem or warn against a dangerous practice. Readers will come away with a better understanding of how C++ is used in the real world. From the Inside Flap In the hands of an expert, C++ helps designers and programmers build systems that are modular, maintainable, and fast. To the novice, however, the size of the language can be intimidating. There are a lot of features in C++ and it takes some experience to learn which ones are appropriate for any situation. This book is intended to enhance and expedite that learning process. Most successful C++ programmers cannot recite chapter and verse from the language rules; instead, they have acquired a set of idioms and techniques that have worked well for them. Our goal is to help the C++ novice learn those idioms that have been most useful in practice. We also point out some of the most common pitfalls. We do not try to cover the entire language and we leave the ultra-precise definitions of language semantics to the reference manuals. Instead, we concentrate on helping the reader build programs that can be understood by someone who is not a C++ language lawyer. We not only discuss techniques for making programs elegant and fast; we also show how to make them easier to understand and maintain. Acknowledgements Almost none of the ideas and programming idioms in this book are my invention. My goal has been to present, in a way that allows novice C++ programmers to learn them quickly, what I consider to be the most important strategies and tactics I have learned from others in the eight years I have been using C++. Some of these lessons were learned by studying actual development projects as they moved from C to C++; others came from discussions with talented individuals. Many of the best ideas on templates and library design, including the ideas behind many of the container classes in this book, came from classes in the USL Standard Components that were originally designed by Martin Carroll, Andrew Koenig, and Jonathan Shopiro. I claim exclusive ownership of any errors in my versions. Andrew Koenig was a valuable resource as the local C++ language lawyer. The participants in the "C++ Strategies and Tactics" seminars I presented at several conferences helped inspire this book and refine its ideas. Other important ideas came from Tom Cargill, John Carolan, Jim Coplien, Mark Linton, Gerald Schwarz, and of course Bjarne Stroustrup, who also invented the C++ programming language that made the book possible in the first place. Brian Kernighan read several drafts of this book, and his excellent feedback has been a lot of help. I would also like to thank David Annatone, Steve Buroff, Tom Cargill, Bill Hopkins, Cay Horstman, Lorraine Juhl, Peter Juhl, Stan Lippman, Dennis Mancl, Scott Meyers, Barbara Moo, Lorraine Weisbrot Murray, Bjarne Stroustrup, Clovis Tondo, Steve Vinoski, and Christopher Van Wyk for their comments on early drafts of this book. Lorraine Weisbrot Murray also contributed the encouragement, understanding, support, and love that helped make the entire effort feasible. Rob Murray
好的,对于这个问题,需要使用C++中的位运算和位图处理库来完成。 首先,需要用位图处理库读取两个位图文件,并确保它们具有相同的大小和位深度。可以使用OpenCV库来完成这个步骤。下面的代码演示了如何读取位图文件并检查它们是否具有相同的大小和位深度: ```c++ #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace std; int main() { // 读取第一个位图文件 Mat first = imread("first1.bmp", IMREAD_UNCHANGED); if (first.empty()) { cout << "Failed to read first1.bmp" << endl; return -1; } // 读取第二个位图文件 Mat second = imread("second1.bmp", IMREAD_UNCHANGED); if (second.empty()) { cout << "Failed to read second1.bmp" << endl; return -1; } // 检查两个位图是否具有相同的大小和位深度 if (first.size() != second.size() || first.type() != second.type()) { cout << "The two bitmaps must have the same size and type" << endl; return -1; } // 现在可以开始进行叠置运算了 // ... } ``` 接下来,需要将两个位图的每个像素进行叠置运算,这里使用按位或(|)运算符。按位或运算符将两个二进制数的每一位进行或运算,结果中的每一位要么为1,要么为0,具体规则如下: | a | b | a \| b | |:-:|:-:|:-------:| | 0 | 0 | 0 | | 0 | 1 | 1 | | 1 | 0 | 1 | | 1 | 1 | 1 | 按位或运算符可以用来将两个位图的每个像素进行叠置运算,即将它们的对应像素的每个通道进行按位或运算。下面的代码演示了如何进行叠置运算并将结果输出到一个新的位图文件中: ```c++ #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace std; int main() { // 读取第一个位图文件 Mat first = imread("first1.bmp", IMREAD_UNCHANGED); if (first.empty()) { cout << "Failed to read first1.bmp" << endl; return -1; } // 读取第二个位图文件 Mat second = imread("second1.bmp", IMREAD_UNCHANGED); if (second.empty()) { cout << "Failed to read second1.bmp" << endl; return -1; } // 检查两个位图是否具有相同的大小和位深度 if (first.size() != second.size() || first.type() != second.type()) { cout << "The two bitmaps must have the same size and type" << endl; return -1; } // 叠置运算 Mat result; bitwise_or(first, second, result); // 将结果保存到新的位图文件中 imwrite("result.bmp", result); // 输出完成信息 cout << "The result has been saved to result.bmp" << endl; return 0; } ``` 通过以上的代码,我们就可以使用C++对两个位图进行叠置运算并将结果保存到一个新的位图文件中了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

maray

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

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

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

打赏作者

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

抵扣说明:

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

余额充值