我试图显示调用函数时提供的参数的实际值.`match.call'按照我想要的方式执行某些操作,但它不会对变量进行求值.例如
foo <- function(x) match.call()
foo(2)
Run Code Online (Sandbox Code Playgroud)
版画
foo(x = 2)
Run Code Online (Sandbox Code Playgroud)
我很高兴.然而:
xxx <- 2
foo(xxx)
Run Code Online (Sandbox Code Playgroud)
将打印
foo(x = xxx)
Run Code Online (Sandbox Code Playgroud)
而不是foo(x = 2)我想拥有它.
我曾尝试各种组合substitute,eval和公司,但没有成功的.
我想创建一个std :: vector对象(或任何其他标准或自定义容器类型),其中包含自定义和任意函数的元素,这些函数的签名都是相同的.
它应该是这样的:
// Define the functions and push them into a vector
std::vector<????> MyFunctions;
MyFunctions.push_back(double(int n, float f){ return (double) f / (double) n; });
MyFunctions.push_back(double(int n, float f){ return (double) sqrt((double) f) / (double) n; });
// ...
MyFunctions.push_back(double(int n, float f){ return (double) (f * f) / (double) (n + 1); });
// Create an argument list
std::vector<std::pair<int, float>> ArgumentList;
// ...
// Evaluate the functions with the given arguments
// Suppose that it is guarantied …Run Code Online (Sandbox Code Playgroud) c++ functional-programming function-pointers vector function-call
我正在尝试编写一个以 List 对象作为输出参数的 Java 函数。
boolean myFunction(int x, in y, List myList)
{
/* ...Do things... */
myList=anotherList.subList(fromIndex, toIndex);
return true;
}
Run Code Online (Sandbox Code Playgroud)
在此之前,我调用函数,声明 myList 如下:
List myList=null;
Run Code Online (Sandbox Code Playgroud)
然后我调用该函数
myFunction(x,y,myList)
Run Code Online (Sandbox Code Playgroud)
但是当我尝试操作 myList 时,我发现 myList 仍然为空。
我确信anotherList我的函数代码中的变量不为空,并且我确信该subList函数返回一个非空列表。
原因是什么?如何在 Java 函数中传递 List 作为输出参数?
好吧,因为我用c ++写了很多东西已经有一段时间了,我已经习惯了更现代语言的一些细节.这是一个一直在唠叨我,我相信那里有一个答案.有没有办法在运行时调用用户指定为字符串的函数?无需诉诸某种大规模的开关/ if块?
我所处理的情况可归结为:我已经解决了我在C++中解决的数学相关问题,并指定为"Problem1.cpp/Problem1.h","Problem2.cpp/Problem2 .h"等.每个问题都有一个函数调用problemX()(X问题的数量),这个函数启动了解决方案.在程序开始时,我想问用户"你想解决哪个问题?" 他们指定了一个数字.然后,我想调用适当的problemX()函数,而不必使用大量的硬编码switch语句(或if语句,或函数指针的索引数组等).
我确信这是可能的,但我不记得该如何去做.有任何想法吗?
我写了一篇关于C++编程的考试.有一个问题我和我的教授不同意.问题是,以下功能是否有效:
#include <iostream>
using namespace std;
void f(int=4, long=10, double=3.14);
int main( int argc , char ** argv )
{
f( , ,8);
return EXIT_SUCCESS;
}
void f(int i, long l, double d) {
cout << i << " " << " " << l << " " << d;
}
Run Code Online (Sandbox Code Playgroud)
我说它不会起作用,但我的教授说它肯定会起作用,因为函数声明中的默认参数.我用MSVC尝试过它并没有用.这是编译器特定的吗?我如何说服我的教授在任何编译器中都不起作用,在考试中提高我的分数?
想象一个具有可变数量的输入参数的函数,交替地询问字符串和值.
myfunction('string1',value1,'string2',value2,...)
Run Code Online (Sandbox Code Playgroud)
例如
myfunction('A',5,'B',10)
Run Code Online (Sandbox Code Playgroud)
我想保持调用这个函数的能力,我不想改变varargin函数内部的评估.(除非('string1','string2',...,value1,value2,...)有帮助)
但我也有我的输入字符串和值存储在单元格数组中inputvar <4x1 cell>:
inputvar =
'A' [5] 'B' [10]
Run Code Online (Sandbox Code Playgroud)
该单元阵列也具有可变长度.
我的意图是以某种方式调用我的函数如下:
myfunction( inputvar )
Run Code Online (Sandbox Code Playgroud)
这显然不起作用.我有什么想法可以将我的单元格转换为有效的输入语法?
我已经尝试生成一个类似的字符串
''string1',value1,'string2',value2'
Run Code Online (Sandbox Code Playgroud)
并用于eval在函数调用中使用它.但它没有成功.那么,有没有办法转换字符串代码?
假设有一个带有一些参数的函数,并且我有一个关联数组(或一个具有公共属性的简单对象 - 这几乎是相同的,因为我总是可以使用类型转换(object)$array),其键对应于函数参数名称及其值对应于函数调用参数。我如何调用它并将它们传递到那里?
<?php\nfunction f($b, $a) { echo "$a$b"; }\n// notice that the order of args may differ.\n$args = ['a' => 1, 'b' => 2];\ncall_user_func_array('f', $args); // expected output: 12 ; actual output: 21\nf($args); // expected output: 12 ; actual output: \xe2\x86\x93\n// Fatal error: Uncaught ArgumentCountError:\n// Too few arguments to function f(), 1 passed\nRun Code Online (Sandbox Code Playgroud)\n 我们如何将数组直接传递给 C 中的函数?
例如:
#include <stdio.h>
void function(int arr[]) {};
int main(void) {
int nums[] = {3, -11, 0, 122};
function(nums);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
除了这个,我们可以写一些类似的东西function({3, -11, 0, 122});吗?
我试图理解为什么在 ProcessHacker 代码中使用这种铸造风格。
RtlSetHeapInformation(
PhHeapHandle,
HeapCompatibilityInformation,
&(ULONG){ HEAP_COMPATIBILITY_LFH }, // HEAP_COMPATIBILITY_LFH = 2UL
sizeof(ULONG)
);
Run Code Online (Sandbox Code Playgroud)
使用“{}”进行转换有什么好处?它适用于 C 和 C++ 吗?
&(ULONG){ HEAP_COMPATIBILITY_LFH }, // HEAP_COMPATIBILITY_LFH = 2UL
Run Code Online (Sandbox Code Playgroud) 我无法找出为什么我的函数仅返回用户输入而不是输入的阶乘。
#include <stdio.h>
#include <math.h>
int factorial(int x)
{
//int x;
int sum = 1;
while (x!=0){
sum = sum * x;
x--;
}
return sum;
}
int main(){
int x;
printf("Enter value of x: ");
scanf("%i",&x);
factorial(x);
printf("sum is %i", x);
return 0;
}
Run Code Online (Sandbox Code Playgroud) function-call ×10
c++ ×4
c ×3
arrays ×1
c++11 ×1
cell-array ×1
factorial ×1
java ×1
list ×1
literals ×1
matlab ×1
php ×1
r ×1
return-value ×1
runtime ×1
user-input ×1
vector ×1