【发布时间】:2012-03-08 23:29:41
【问题描述】:
我知道我可以在 C++ 中做到这一点:
string s[] = {"hi", "there"};
但是有没有办法在不删除 string s[] 的情况下以这种方式删除数组?
例如
void foo(string[] strArray){
// some code
}
string s[] = {"hi", "there"}; // Works
foo(s); // Works
foo(new string[]{"hi", "there"}); // Doesn't work
【问题讨论】:
-
为什么要使用动态分配的 c 样式数组?我建议您改用
std::vector。使用 c++11 时,您可以使用初始化列表来构造它们,例如void foo(vector<string>); foo({"hi", "there"});应该在 c++11 中工作
标签: c++ arrays string initialization