标签: variadic-functions

初始化Map的类型安全的varargs方法

我想写一个可以用来初始化Map的方法.首裁:

Map map(Object ... o) {for (int i = 0; i < o.length; i+=2){result.put(o[i], o[i+1])}}
Run Code Online (Sandbox Code Playgroud)

简单,但不是类型安全的.使用泛型,可能是这样的:

<TKey, TValue> HashMap<TKey, TValue> map(TKey ... keys, TValue ... values) 
Run Code Online (Sandbox Code Playgroud)

但是不支持该语法.所以最终我来到这个:

public static <TKey, TValue, TMap extends Map<? super TKey, ? super TValue>> TMap map(TMap map, Pair<? extends TKey, ? extends TValue> ... pairs) {
    for (Pair<? extends TKey, ? extends TValue> pair: pairs) {
        map.put(pair.getKey(), pair.getValue());
    }
    return map;
}

public static <TKey, TValue> HashMap<? super TKey, ? super TValue> map(Pair<? extends TKey, …
Run Code Online (Sandbox Code Playgroud)

java generics variadic-functions

1
推荐指数
1
解决办法
1588
查看次数

以编程方式在Scala中设置重复参数

我正打算Futures.awaitAll用可变数量的井来打电话Future.awaitAll被定义为awaitAll(timeout : Long, fts : Future[Any]*).我试过传入a List和a Array但两者都不起作用:

list = future1 :: future2 :: Nil

Futures.awaitAll(1000, list)

found : List[scala.actors.Future[Any]] required: scala.actors.Future[Any]
Run Code Online (Sandbox Code Playgroud)

编辑:我现在要做的是Futures.awaitAll使用可变数量的参数(1到n)以编程方式调用.所以使用Futures.awaitAll(1000, future1, future2)不是一种选择.

Scala编程的第8.8章没有给我任何提示如何解决这个问题,所以欢迎帮助:)

language-features scala variadic-functions

1
推荐指数
1
解决办法
973
查看次数

将va_arg两次传递给相同值的函数结果

我正在尝试使用va_arg在我的GUI库中创建一个通用工厂函数.在同一函数中传递va_arg两次时,它们传递相同的值而不是两个不同的值:

GUIObject* factory(enumGUIType type, GUIObject* parent, ...){
   va_list vl;
   va_start(vl, parent);
   ...
   label->SetPosition(va_arg(vl, int), va_arg(vl, int));
   va_end(vl);
   return finalObjectPointer;
}

factory(LABEL, theParent, 100,200); // Results in position 200:200
Run Code Online (Sandbox Code Playgroud)

是什么导致了这种意外行为

c++ variadic-functions

1
推荐指数
2
解决办法
693
查看次数

C中简单可变字符串连接的内存分配

我有以下测试函数来复制和连接可变数量的字符串参数,自动分配:

char *copycat(char *first, ...) {
    va_list vl;
    va_start(vl, first);
    char *result = (char *) malloc(strlen(first) + 1);
    char *next;
    strcpy(result, first);
    while (next = va_arg(vl, char *)) {
        result = (char *) realloc(result, strlen(result) + strlen(next) + 1);
        strcat(result, next);
    }
    return result;
}
Run Code Online (Sandbox Code Playgroud)

问题是,如果我这样做:

puts(copycat("herp", "derp", "hurr", "durr"));
Run Code Online (Sandbox Code Playgroud)

它应该打印出一个16字节的字符串"herpderphurrdurr".相反,它打印出一个42字节的字符串,这是正确的16字节加上26个字节的垃圾字符.

我还不太清楚为什么呢.有任何想法吗?

c string memory-management variadic-functions

1
推荐指数
2
解决办法
769
查看次数

Java varargs在C++中的等价物

我有一个用Java编写的函数,它接受varargs作为参数.我想将该函数移植到C++.我试图搜索,但我得到的最接近的是使用参数列表的std :: vector.将varargs转换为C++的最佳方法是什么?功能如下.

public EventHandlerQueue<T> get (final EventHandler<T> ... handlers)
{
     // Do something with handlers
     return new EventHandlerQueue<T>(handlers)
}  
Run Code Online (Sandbox Code Playgroud)

c++ variadic-functions

1
推荐指数
2
解决办法
605
查看次数

调用va_arg的次数少于传递的参数数量

如果我调用va_arg的次数少于在可变参数函数中传递的参数数量,那么它是否是未定义的行为?

例如:

#include <stdarg.h>
void foo(unsigned n, ...) {
    va_list ap;
    int bar = 0;

    va_start(ap, n);
    if (n) bar = va_arg(ap,int);
    // Do something with bar
}

int main() {
    foo(2, 3, 4);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

该程序会导致未定义的行为吗?

c variadic-functions undefined-behavior

1
推荐指数
1
解决办法
146
查看次数

在C中使用varargs

是一个例子varargs.

我有点困惑.

[x]问题1:如何获得第n 论点?

va_list ap;
va_arg (ap + (n-1), int); // **INCORRECT USAGE**
Run Code Online (Sandbox Code Playgroud)

这是对的吗?如果没有,如何获得第n 参数?

[x]问题2:什么时候会ap增加?

它说 ap将增加到下一个论点,但如果我使用va_arg(ap + (n-1), int), n>=2,会ap增加吗?

问题3:(BTW /可能是偏离主题的)什么是缩写?

有时,我们声明Cnt缩写为CountCounter.

什么ap缩写?

问题4:哪种类型ap

我知道它的va_list类型,但是什么va_list?结构?诠释?或者是其他东西?

谢谢.

c variadic-functions

1
推荐指数
1
解决办法
268
查看次数

编组变量参数 - __arglist或替代

描述我试图解决的问题的最好方法是用代码说话.我在这个论坛上看到了很多__arglist问题,但没有很多有用的答案.我知道应该避免使用_arglist,所以我对其他方法持开放态度

在一个C++模块中,我有类似以下内容

void SomeFunction(LPCWSTR pszFormat, va_args args)
{
    // this function is not exported...
    // it is designed to take a format specifier and a list of variable 
    // arguments and "printf" it into a buffer. This code 
    // allocates buffer and uses _vsnwprintf_s to format the 
    // string. 
    // I really do not have much flexibility to rewrite this function
    // so please steer away from scrutinizing this. it is what is 
    // and I need to call it …
Run Code Online (Sandbox Code Playgroud)

c# c++ pinvoke marshalling variadic-functions

1
推荐指数
1
解决办法
2668
查看次数

带有%s和std :: string的sprintf给出了乱码

在以下代码中:

#include <string>
using namespace std;

int main(){
    char command[300];
    string stringz = "mystringy";
    sprintf(command,"echo \"something with a string %s\" ", stringz);
    system(command);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

为什么是输出

something with a string 8?
Run Code Online (Sandbox Code Playgroud)

而不是预期的

something with a string mystringy
Run Code Online (Sandbox Code Playgroud)

一个愚蠢的问题,但我无法找到答案.

c++ string printf variadic-functions stdstring

1
推荐指数
2
解决办法
2207
查看次数

Kotlin函数重载(varargs vs单参数)

我有两个函数检查String/Strings是否为空.

fun isBlank(s: String?) : Boolean {
            return s.isNullOrBlank()
        }

fun isBlank(vararg strings: String) : Boolean {
            return strings.isEmpty() ||
                    strings.any { isBlank(it) }
        }
Run Code Online (Sandbox Code Playgroud)

所以我尝试从第二个函数调用第一个函数,但似乎它试图调用自己.例如,它在java中很好用:

public static boolean isBlank(final String string) {
        return string == null || string.trim().isEmpty();
}

public static boolean isBlank(final String... strings) {
        return strings.length == 0
                || Arrays.stream(strings).anyMatch(StringUtil::isBlank);
}
Run Code Online (Sandbox Code Playgroud)

如何在kotlin处理这种情况?

overloading variadic-functions kotlin

1
推荐指数
1
解决办法
394
查看次数