括号之间的两个字符串在C ++中用逗号分隔 [英] Two strings between brackets separated by a comma in C++

查看:461
本文介绍了括号之间的两个字符串在C ++中用逗号分隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

C ++逗号运算符

我碰巧遇到了(至少对我来说)C ++

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆