英文:
Can we create an array while passing it as an argument to a function in C++?
问题
我想在调用函数时创建一个数组,就像在Java或Python中可以做的那样。
例如:
class HelloWorld {
public static void main(String[] args) {
example(new int[]{1,2,3}); // 就像这样
}
static void example(int[] a){
System.out.print(a[0]);
}
}
或者在Python中
def fun(x):
print(x[0])
fun((1, 2, 3)) #就像这样
当我尝试在C++中做类似的事情时,我会得到一个错误:
void example(int a[]){
cout << a[0] << endl;
}
int main() {
// 在这里编写C++代码
cout << "Hello world!" << endl;
example(new int(3){0, 1, 2});
return 0;
}
这会产生错误:
错误:在'{'标记之前期望')'
或者
void example(int a[]){
cout << a[0] << endl;
}
int main() {
// 在这里编写C++代码
cout << "Hello world!" << endl;
example({0, 1, 2});
return 0;
}
在这里,编译器将数组{0, 1, 2}
视为初始化列表。
错误:无法将'<brace-enclosed初始化列表>'转换为'int*'
我希望能够实现类似于第二次尝试的函数调用。
function({1, 2, 3, 4}); //任意大小的数组
我尝试搜索,但未能找到符合要求的解决方案。
非常感谢所有人的帮助,提前感谢大家。
英文:
I want to create an array while passing it to a function, like we can do in Java or Python.
For example:
class HelloWorld {
public static void main(String[] args) {
example(new int[]{1,2,3}); // Like this
}
static void example(int[] a){
System.out.print(a[0]);
}
}
or in python
def fun(x):
print(x[0])
fun((1, 2, 3)) #Like this
When I try to do something like this in C++ I get an error
void example(int a[]){
cout<<a[0]<<endl;
}
int main() {
// Write C++ code here
cout << "Hello world!"<<endl;
example(new int(3){0, 1, 2});
return 0;
}
This gives the error
> error: expected ')' before '{' token
or
void example(int a[]){
cout<<a[0]<<endl;
}
int main() {
// Write C++ code here
cout << "Hello world!"<<endl;
example({0, 1, 2});
return 0;
}
Here the compiler takes the array {0, 1, 2}
as an initializer list.
> error: cannot convert '<brace-enclosed initializer list>' to 'int*'
I'd like if there is some way of achieving a function call similar to the 2nd attempt.
function({1, 2, 3, 4}); //An array of any size
I tried searching for it but wasn't able to find a solution that fits the bill.
Any and all help is really appreciated and I thank everyone in advance.
答案1
得分: 2
以下是代码部分的中文翻译:
#include <iostream>
#include <vector>
void example(const std::vector<int>& a) {
std::cout << a[0] << std::endl;
}
int main() {
// 使用预定义变量
std::vector<int> list { 0, 1, 2 };
example(list);
// 使用内联变量
example(std::vector<int> { 3, 4 });
// 让编译器来判断
example({ 5, 6 });
return 0;
}
请注意,new[]
不会自动释放内存,如果将其作为参数传递给不负责释放内存的函数,将会导致内存泄漏。
这种情况不会发生在std::vector
或其他标准容器上,它们会在超出作用域时始终释放内存。
Python在许多方面填补了C++不愿填补的空白,但即使Python也可以在参数方面变得更加像C++一样严格。
英文:
Here's a reworked version that uses more modern C++ conventions:
#include <iostream>
#include <vector>
void example(const std::vector<int>& a) {
std::cout << a[0] << std::endl;
}
int main() {
// Using a pre-defined variable
std::vector<int> list { 0, 1, 2 };
example(list);
// Using an inline variable
example(std::vector<int> { 3, 4 });
// Letting the compiler figure it out
example({ 5, 6 });
return 0;
}
Keep in mind new[]
is not automatically released, and if you pass it in as an argument to a function that does not assume responsibility for releasing it, you have a memory leak.
This does not happen with std::vector
or other standard containers, these will always release memory when they fall out of scope.
Python will fill in the blanks in many ways that C++ refuses to, but even Python can be made stricter about arguments where it will act a lot more like C++.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论