括号中的两个字符串由C ++中的逗号分隔 [英] Two strings between brackets seperated by a comma in C++
问题描述
可能重复:
C ++ Comma Operator
我遇到了意想不到的问题今天的行为,如下面的片断所示:
I came across unexpected (to me at least) C++ behavior today, shown by the following snippit:
#include <iostream>
int main()
{
std::cout << ("1", "2") << std::endl;
return 0;
}
输出:
2
这适用于括号之间的任何数量的字符串。测试在visual studio 2010编译器以及在键盘上。
This works with any number of strings between the parentheses. Tested on the visual studio 2010 compiler as well as on codepad.
我想知道为什么这个编译在第一个地方,这个功能的用途是什么?
I'm wondering why this compiles in the first place, what is the use of this 'feature'?
推荐答案
啊,这是逗号运算符。当使用逗号和两个(或更多)表达式时,会发生的是,所有表达式都被执行,结果作为一个整体是最后一个表达式的结果。这就是为什么你得到2作为这样的结果。有关更多解释,请参见此处。
Ahh, this is the comma operator. When you use a comma and two (or more) expressions, what happens is that all expressions are executed, and the result as a whole is the result of the last expression. That is why you get "2" as a result of this. See here for a bigger explanation.
这篇关于括号中的两个字符串由C ++中的逗号分隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!