如何打印多个参数 [英] How Do I Print Multiple Parameters
本文介绍了如何打印多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include < iostream >
使用 命名空间标准;
int doDateofBirth( int m, int d, int y);
int main(){
cout<< 我的DoB是,<< doDateofBirth( 4 , 14 , 1998 ) << ENDL;
}
int doDateofBirth( int m, int d, int y){
返回 m,d,y;
}
控制台只返回y
解决方案
< blockquote>
Quote:控制台只返回y
这是逗号运算符的正确行为(参见此处 [ ^ ])。
无论如何一个 C / C ++
函数返回单个值。
但是,单一返回值可能是一个对象(您可以为这样的结构或类定义插入运算符,例如
# include < iostream >
使用 命名空间标准;
struct DOB // '出生日期'结构
{
int m,d,y;
};
// DOB对象的插入运算符
ostream& operator << (ostream& os, const DOB& dob)
{
return os<< dob.m<< << dob.d<< << dob.y;
}
DOB doDateofBirth( int m, int d, int y);
int main()
{
cout<< 我的DoB是,<< doDateofBirth( 4 , 14 , 1998 ) << ENDL;
}
DOB doDateofBirth( int m, int d, int y)
{
DOB dob;
dob.m = m; dob.d = d; dob.y = y;
return dob;
}
这是另一种方法。
#include < iostream >
使用 命名空间标准;
void doDateofBirth( int mym, int myd, int myy);
int m,d,y;
void main(){
doDateofBirth( 4 ,< span class =code-digit> 14 , 1998 );
cout<< 我的DoB是,<< m< d<< y<< ENDL;
}
void doDateofBirth( int mym, int myd, int myy){
m = mym;
d = myd;
y = myy;
}< / iostream>
#include <iostream>
using namespace std;
int doDateofBirth(int m , int d , int y);
int main(){
cout << "My DoB is, " << doDateofBirth(4,14,1998) << endl;
}
int doDateofBirth(int m,int d,int y){
return m,d,y;
}
The console is only returning the y
解决方案
Quote:The console is only returning the y
That is the correct behaviour of the comma operator (see here[^]).
Anyway aC/C++
function returns a single value.
However, the single return value could be an object (instance of a class or a struct) and you may define the insertion operator for such a struct or class, e.g.
#include <iostream> using namespace std; struct DOB //'date of birth' struct { int m, d, y; }; // insertion operator for DOB objects ostream & operator << (ostream & os, const DOB & dob) { return os << dob.m << " " << dob.d << " " << dob.y; } DOB doDateofBirth(int m , int d , int y); int main() { cout << "My DoB is, " << doDateofBirth(4,14,1998) << endl; } DOB doDateofBirth(int m,int d,int y) { DOB dob; dob.m = m; dob.d = d; dob.y = y; return dob; }
Here is another way to do it.
#include <iostream> using namespace std; void doDateofBirth(int mym , int myd , int myy); int m, d, y; void main(){ doDateofBirth(4,14,1998); cout << "My DoB is, " << m << d << y << endl; } void doDateofBirth(int mym,int myd,int myy){ m= mym; d= myd; y= myy; }</iostream>
这篇关于如何打印多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文