哪个 C++您使用逻辑运算符:and、or、not 以及类似或 C 风格的运算符吗?为什么?

发布于 2024-08-10 13:10:31 字数 188 浏览 6 评论 0原文

标题中暗示的休闲/好奇心问题。

在我看来,我个人更喜欢新的运算符,因为它们可以使代码更具可读性。

哪些是自己用的?您选择其中一种而不是另一种的原因是什么?

Emacs 还以不同的方式突出显示这些运算符,因此我在查看屏幕时可以获得更多视觉反馈。我知道旧的运算符也可以突出显示,但默认情况下 ISO646 会突出显示

leisure/curiosity question as implied in the title.

I personally prefer the new operators as to make code more readable in my opinion.

Which ones do use yourself? What is your reason for choosing one over the other one?

also Emacs highlights those operators differently so I get more visual feedback when looking at the screen. I know the old operators can be highlighted as well but the ISO646 highlighted by default

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

送君千里 2024-08-17 13:10:31

我认为我从未见过实际使用字母运算符的 C/C++ 源代码。对我来说,这看起来很奇怪;非常不C-ish。

I don't think I have ever even seen C/C++ source code that actually uses the alphabetic operators. To me, it just looks strange; very un-C-ish.

童话里做英雄 2024-08-17 13:10:31

我更喜欢使用 not 而不是 !,因为它更具可读性:

if (not condition()) {
}

if (!condition()) {
}

! 那种迷失在那里的情况相比。当然,在编写可移植的代码时必须避免(或#define)这种情况。

然而,andor对我来说没有多大作用,所以我不使用它们。

I prefer to use not instead of ! because it's a lot more readable:

if (not condition()) {
}

versus

if (!condition()) {
}

The ! kind of gets lost in there. Of course, this must be avoided (or #defined) when writing code intended to be portable.

However, and and or don't do much for me, so I don't use them.

花之痕靓丽 2024-08-17 13:10:31

我不会使用替代运算符,因为在我看来,它们会造成更多混乱,而不是清晰。
如果我看到一个字母名称,我期望一个命名空间、类、变量、函数或函数样式运算符 - 常见运算符直观地将其划分为序列。对我来说,替代风格不适合 C/C++ 世界。

另外,虽然 Greg 对 C 或 C++ 新手有一定的看法,但你很快就会习惯它 - 我在任何地方都可以毫无问题地发现 !

I won't use the alternative operators as they cause more confusion then clearity in my opinion.
If i see an alphabetical name i expect a namespace, class, variable, function or a function style operator - the common operators divide this intuitively into sequences for me. The alternative style just doesn't fit into the C/C++ world for me.

Also, while Greg has a point regarding people new to C or C++, you get used to it pretty soon - i have no problems spotting ! anywhere.

谁的新欢旧爱 2024-08-17 13:10:31

您是否指的是源自 iso646.h?

它们来自 C,而且很少见,因此大多数人的可读性可能较差。我确信如果我们是用这些关键词“长大”的,它们会更常见,因为它们读起来更像英语。

话又说回来,&& 让非英语使用者用他们的母语说“and”。 (这并不是说这首先是关键词中需要考虑的问题。)

Are you referring to the alternative tokens originating from iso646.h?

They are from C and it's very uncommon to see, therefore likely to be less readable by most. I'm sure if we had been "raised" with these keywords they'd be more common, since they read like English better.

Then again, && lets non-English speakers say "and" in their native tongue. (Not that this was a concern in the keywords in the first place.)

夏尔 2024-08-17 13:10:31

我用旧的。

原因:我用 Perl 编码了一段时间,运算符具有不同的优先级,具体取决于它们是否是拼写出来的变体。因此,我遇到了一些错误,从那时起,我总是在拼写出来的内容上使用 &&||!任何具有它们的语言的变体。

I use the old ones.

Reason: I coded in Perl for a while and the operators have different precedence depending on if they're the spelled-out variant or not. I ran into a couple of bugs because of this and since then I always use &&, || and ! over the spelled out variant for any language that has them.

憧憬巴黎街头的黎明 2024-08-17 13:10:31

逻辑(不仅仅是逻辑)运算符的替代标记,如 andornot 等与 C++ 没有任何关系。它们几乎从一开始就出现在 C 语言中。如果您喜欢在代码中使用它们,您可以自由地这样做,但如果人们认为这种做法相当非正统,请不要感到惊讶。通常人们在 C 和 C++ 代码中都使用传统的指示符来表示这些运算符(例如 &&|| 等)。

Alternative tokens for logical (and not only logical) operators, like and, or, not etc. have nothing to do with C++ specifically. They were present in C as well, virtually from the very beginning. If you like to use them in your code, you are free to do so, but don't be surprised if people see this practice as rather unorthodox. Normally people use traditional designators for these operators (e.g. &&, || and so on) in both C and C++ code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文