我想知道有没有办法识别模板参数?例如,假设我想基于模板参数初始化变量.原型示例如下
template<class T>
void initialise(T a)
{
if(T==int)a=0;
else if(T=double)a=0.0;
else if(T=complex<double>)a=T(0.,0);
else print("unknown type");
}
Run Code Online (Sandbox Code Playgroud)
我的问题是如何识别模板参数"T"?或者,我是否需要获得预处理器指令的帮助?这可能是一个重复的问题,但我找不到它的答案.任何建议将不胜感激.
我已经program.exe
在我的Windows机器上创建了它.由于某种原因,我无法正确传递命令行参数.
int main(int argc, char *argv[]) {
///////testing
cout << "\n\n(int)ARGV[1]: " << (int)argv[1] << "\n\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在终端我运行:
program.exe 4
Run Code Online (Sandbox Code Playgroud)
我看到(int)ARGV[1]: 15333464
打印到我的控制台.
知道为什么会这样或者我如何修改代码?我应该打印出4号.谢谢.
我只是在玩弄
int main(int argc, int *argv[void])
Run Code Online (Sandbox Code Playgroud)
函数,我试图使一个程序读取数字参数的数量.
理论上(在我自己疯狂的妄想心中),这应该工作:
#include <stdio.h>
int main(int argc, char *argv[])
{
int count;
printf("%d\n", sizeof(int));
}
Run Code Online (Sandbox Code Playgroud)
但无论我在命令行中作为参数放置什么,我总是得到4(一个字4个字节?)
如何调整此代码,以便在我输入时
./program 9 8 2 7 4 3 1
Run Code Online (Sandbox Code Playgroud)
我得到:
7
Run Code Online (Sandbox Code Playgroud)
非常感激!
我最近开始学习C++,我有点困惑argv
和argc
.我试图通过检查大小来确定参数是否传递给程序argc
,但不管有多少参数(包括没有)我传递给程序,它的大小总是如此4
.
简单的例子:
int main(int argc, char** argv)
{
std::cout << sizeof(argc); //outputs 4 with 0 or any num of arguments
std::cout << sizeof(argv); //outputs 8 with 0 or any num of arguments
}
Run Code Online (Sandbox Code Playgroud)
我之前发现了同样的问题,我为重复它而道歉,但我发现的那些页面上的答案与这里发生的事情相矛盾.
那么,为什么argc
总是4
有没有其他方法来检查参数是否传递给main()
?
如果它是相关的我正在使用g ++进行编译.
我在Python中有以下函数(在Trinket上看到它):
def foo(a=1, b=2):
print(a);
print(b)
foo(,4)
Run Code Online (Sandbox Code Playgroud)
当我运行时foo(,4)
,我希望它在未提供第一个参数时使用默认值,并使用提供的值(如果可用).即它应该打印:
1
4
Run Code Online (Sandbox Code Playgroud)
这在Python中可行吗?如果是这样,我该怎么做?
def test():
x = 99
def nested(x):
print (x)
return nested
a = test()
a()
Run Code Online (Sandbox Code Playgroud)
TypeError:nested()缺少1个必需的位置参数:'x'
当我调用nested
在闭包函数中分配的print参数x时test
,TypeError会提示我将位置参数传递给嵌套,但为什么在测试函数中分配的'x'没有传递给嵌套?
在 C++ 中有没有办法像在 python 中那样按名称传递参数?例如我有一个功能:
void foo(int a, int b = 1, int c = 3, int d = 5);
Run Code Online (Sandbox Code Playgroud)
我可以以某种方式称它为:
foo(5 /* a */, c = 5, d = 8);
Run Code Online (Sandbox Code Playgroud)
或者
foo(5, /* a */, d = 1);
Run Code Online (Sandbox Code Playgroud) C++ 有没有办法定义一个函数,这样我就可以做这样的事情:
foo( "Error on line " << iLineNumber );
foo( "name " << strName << " is Invalid" );
Run Code Online (Sandbox Code Playgroud)
就像现在一样,我必须在调用 foo 函数之前声明一个 ostringstream 对象,如下所示:
std::ostringstream strStream1;
ostringstream1 << "Error on line " << iLineNumber;
foo( ostringstream1 );
std::ostringstream strStream2;
ostringstream2 << "name " << strName << " is Invalid";
foo( ostringstream2 );
Run Code Online (Sandbox Code Playgroud) 当我运行以下代码时:
xdata2 = [1 3 4 5];
ydata2 = [3 9 76 73];
params = [0.1 0.5 0.0 -0.5 0.2 0.8];
y = svenssontest2(xdata2,ydata2,params,0.636,1.9632);
Run Code Online (Sandbox Code Playgroud)
我收到错误消息"输入参数太多",但输入参数的数量是正确的.这是函数的代码svenssontest2
:
function [alpha L1 L2] = svenssontest2(tau,Y,params,L1,L2)
tau=tau.';
Y=Y.';
nObs=length(Y);
%z=1;
%for(j =1:50)
%L2=j/200+0.01;
%for(k=1:50)
% L1=k/200+0.01;
Lev1= [params(1)*ones(nObs,1) params(2)*(1-exp(-params(5).*tau))./(params(5).*tau) params(3)*((1-exp(-params(5).*tau))./(params(5).*tau)-exp(-params(5).*tau)) params(4)*((1-exp(-params(6).*tau))./(params(6).*tau)-exp(-params(6).*tau))];
Y=Y-Lev1;
G= [ones(nObs,1) (1-exp(-L1.*tau))./(L1.*tau) (1-exp(-L1.*tau))./(L1.*tau)-exp(-L1.*tau) (1-exp(-L2.*tau))./(L2.*tau)-exp(-L2.*tau)];
alpha =G\Y;
u=Y-G*alpha
stderr=sqrt(diag((u'*u)/(length(Y)-4)*pinv(G'*G)));
Sum_u2 = sum(u.^2);
Res(1,:) = [Sum_u2 alpha' L1 L2];
% z=z+1;
% end
Run Code Online (Sandbox Code Playgroud) 在功能
int foo(int i, int j)
{
return i + j;
}
Run Code Online (Sandbox Code Playgroud)
既i
和j
未初始化,所以这个函数可以用,只用两个参数来调用:
foo(5, 6); // 11
Run Code Online (Sandbox Code Playgroud)
好的,但是C++有默认参数,并且可能的定义如下:
int foo(int i = 1, int j = 2)
{
return i + j;
}
Run Code Online (Sandbox Code Playgroud)
这里既有i
和j
具有默认参数,然后调用:
foo(5, 6); // 11
foo(5); // 7
foo(); // 3
Run Code Online (Sandbox Code Playgroud)
是可能的.那么,为什么这个定义:
int foo(int i += 1, int j += 2)
{
return i + j;
}
Run Code Online (Sandbox Code Playgroud)
这些电话:
foo(5, 6); // 14
foo(5); // 8
foo(); // …
Run Code Online (Sandbox Code Playgroud)