是否可以在函数调用中构造和传递数组/其他容器?
有没有办法在函数调用中构造字符串数组?例如,假设我有一个这样的函数:
void foo( char * [] strings )
{
//...does stuff
}
而不是先声明字符数组然后传递它,有没有办法使用数组或任何其他容器类来调用这样的函数?
foo( { "a", "b", "c", ...} );
基本上,我有一个函数提示用户输入,直到它与我传递的数组中的字符串之一匹配,并且由于这种情况在整个程序中发生变化,因此我将接受的输入传递到提示用户并验证输入的函数中。目前我只是在函数外部构建它并将其传递进去,但想知道它们是否可以以任何方式组合成一行
Is there any way to construct an array of strings inside a function call? For example, say I have a function like this:
void foo( char * [] strings )
{
//...does stuff
}
And instead of declaring the character array first and then passing it, is there any way, with arrays or any other container class, to call the function like this?
foo( { "a", "b", "c", ...} );
Basically I have a function that prompts a user for input until it matches one of the strings in the array I pass, and since that changes throughout the program, I pass the accepted inputs into the function that prompts the user and validates the input. Currently I am just constructing it outside the function and passing it in but was wondering if they could be combined into one line in any way
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这在 C99 中有效:
既然你提到“容器”,你可能使用 C++,所以这个答案的价值有限(除非你使用接受它作为扩展的编译器 - 例如 GCC 的方式) 。
This works in C99:
Since you mention "container" you probably use
C++
so this answer is of limited value (unless you are using a compiler that accepts it as an extension - the way GCC does for example).