在 C++ 函数中返回字符串数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7527356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Return string array in C++ function
提问by sn0ep
I am new to C++. For a school project I need to make a function which will be able to return a string array.
我是 C++ 的新手。对于学校项目,我需要创建一个能够返回字符串数组的函数。
Currently I have this in my header:
目前我的标题中有这个:
Config.h
配置文件
string[] getVehicles(void);
Config.cpp
配置文件
string[] Config::getVehicles(){
string test[5];
test[0] = "test0";
test[1] = "test1";
test[2] = "test2";
test[3] = "test3";
test[4] = "test4";
return test;}
Obviously this does not work but that's the idea of what I am trying to do. In Java this would be the way to do it. I've tried googling my problem but I didn't come across any answers that were clear to be honest.
显然这行不通,但这就是我想要做的事情的想法。在 Java 中,这将是这样做的方式。我试过在谷歌上搜索我的问题,但老实说我没有找到任何明确的答案。
回答by GreenPepper
Maybe it is better to use a vector in this case, but this is not a correct answer for the question. The reason why it doesn't work is that the variable test just exists in the scope of your function. So you have to manage the memory on your own. Here is an example:
也许在这种情况下最好使用向量,但这不是问题的正确答案。它不起作用的原因是变量 test 仅存在于您的函数范围内。所以你必须自己管理内存。下面是一个例子:
string* getNames() {
string* names = new string[3];
names[0] = "Simon";
names[1] = "Peter";
names[2] = "Dave";
return names;
}
In this case you return a pointer of the position in the heap. All the memory in the heap has to free manually. So it is now your work to delete the memory, if you don't need it anymore:
在这种情况下,您返回堆中位置的指针。堆中的所有内存都必须手动释放。所以现在你的工作是删除内存,如果你不再需要它:
delete[] names;
回答by 6502
In C++ you don't use an array, but a std::vector
instance. Arrays in C++ must have a compile-time fixed length while std::vector
instances can change their length at runtime.
在 C++ 中,您不使用数组,而是使用std::vector
实例。C++ 中的数组必须具有编译时固定长度,而std::vector
实例可以在运行时更改其长度。
std::vector<std::string> Config::getVehicles()
{
std::vector<std::string> test(5);
test[0] = "test0";
test[1] = "test1";
test[2] = "test2";
test[3] = "test3";
test[4] = "test4";
return test;
}
std::vector
can also grow dynamically, so in a C++ program you will find more often something like
std::vector
也可以动态增长,所以在 C++ 程序中你会发现更多类似的东西
std::vector<std::string> Config::getVehicles()
{
std::vector<std::string> test; // Empty on creation
test.push_back("test0"); // Adds an element
test.push_back("test1");
test.push_back("test2");
test.push_back("test3");
test.push_back("test4");
return test;
}
Allocating dynamically an array of std::string
is technically possible but a terrible idea in C++ (for example C++ doesn't provide the garbage collector that Java has).
动态分配一个数组在std::string
技术上是可行的,但在 C++中是一个糟糕的想法(例如 C++ 不提供 Java 所具有的垃圾收集器)。
If you want to program in C++ then grab a good C++ bookand read it cover to cover first... writing Java code in C++ is a recipe for a disaster because the languages, despite the superficial braces similarity, are very very different in many fundamental ways.
如果你想用 C++ 编程,那么拿起一本好的 C++ 书,然后从头到尾阅读......根本途径。
回答by Mat
Use a std::vector<std::string>
. It's much easier to deal with than C
-style arrays.
使用一个std::vector<std::string>
. 它比C
-style 数组更容易处理。
#include <string>
#include <vector>
...
std::vector<std::string> Config::getVehicles()
{
std::vector<std::string> data;
data.push_back("hello");
...
return data;
}
Check out the other containers in the standard library, they are almost always a better choice than plain arrays in C++.
查看标准库中的其他容器,它们几乎总是比 C++ 中的普通数组更好的选择。
If your getVehicles
method doesn't change the Config
object's state, consider making it const
:
如果您的getVehicles
方法不会更改Config
对象的状态,请考虑使其 const
:
std::vector<std::string> Config::getVehicles() const { ... }
回答by Alain Isbaquipof
Try this
尝试这个
#include <iostream>
#include <string>
using namespace std;
string * essai()
{
string * test = new string[6];
test[0] = "test0";
test[1] = "test1";
test[2] = "test2";
test[3] = "test3";
test[4] = "test4";
cout<<"test et *test\t"<<&test<<' '<<*(&test)<<'\n';
return test;
}
main()
{
string * toto;
cout<<"toto et *toto\t"<<&toto<<' '<<*(&toto)<<'\n';
toto = essai();
cout<<"**toto\t"<<*(*(&toto))<<'\n';
cout<<"toto\t"<<&toto<<' '<<*(&toto)<<'\n';
for(int i=0; i<6 ; i++)
{
cout<<toto[i]<<' '<<&toto[i]<<'\n';
}
}
For example, in my computer, the result is
例如,在我的电脑中,结果是
toto et *toto 0x7ffe3a3a31b0 0x55dec837ae20
test et *test 0x7ffe3a3a3178 0x55dec9ddd038
**toto test0
toto 0x7ffe3a3a31b0 0x55dec9ddd038
test0 0x55dec9ddd038
test1 0x55dec9ddd058
test2 0x55dec9ddd078
test3 0x55dec9ddd098
test4 0x55dec9ddd0b8
0x55dec9ddd0d8
Getting addresses and contents of addresses could help you to understand that an array in c++ is really rudimentary : it offers no methods and you could access an index without allocating memory (the value 6 in the loop). Your first example show a direct allocation of a local array (test), so you can't return it (the local array dies), in this example, the local variable dies also but there is always a variable that access at this part of allocated memory, the function, and then the variable that receive the result of the function, so the variable test is dead after the calling of the function but the memory is still allocated. Regards.
获取地址和地址的内容可以帮助您理解 c++ 中的数组实际上是基本的:它不提供任何方法,您可以在不分配内存的情况下访问索引(循环中的值 6)。您的第一个示例显示了本地数组的直接分配(测试),因此您无法返回它(本地数组死亡),在此示例中,局部变量也死亡,但始终有一个变量可以访问该部分分配的内存,函数,然后是接收函数结果的变量,所以在调用函数后变量测试已经死了,但内存仍然被分配。问候。