在c++函数中返回字符串数组。

时间:2022-03-09 17:26:52

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

Config.h

string[] getVehicles(void);

Config.cpp

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中,这是一种方法。我曾尝试在谷歌上搜索我的问题,但我没有找到任何明确的答案。

4 个解决方案

#1


22  

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:

也许在这种情况下使用向量更好,但这不是问题的正确答案。它不起作用的原因是变量测试只存在于函数的范围内。所以你必须自己管理内存。这是一个例子:

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;

#2


9  

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

向量也可以动态增长,所以在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++ book and 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++书,把它从头到尾读一遍……在c++中编写Java代码是一种灾难的方法,因为尽管表面的括号相似,但语言在许多基本方面都是非常不同的。

#3


6  

Use a std::vector<std::string>. It's much easier to deal with than C-style arrays.

使用std::向量< std::string >。它比c型数组更容易处理。

#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方法没有更改配置对象的状态,请考虑使用const:

std::vector<std::string> Config::getVehicles() const { ... }

#4


0  

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)。你第一个例子显示当地的直接分配数组(测试),所以你不能返回它(本地数组死了),在本例中,局部变量也死去,但总是一个变量访问分配的内存的一部分,这个函数,然后接收函数的结果的变量,变量测试是死后调用的函数,但仍然是分配的内存。的问候。

#1


22  

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:

也许在这种情况下使用向量更好,但这不是问题的正确答案。它不起作用的原因是变量测试只存在于函数的范围内。所以你必须自己管理内存。这是一个例子:

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;

#2


9  

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

向量也可以动态增长,所以在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++ book and 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++书,把它从头到尾读一遍……在c++中编写Java代码是一种灾难的方法,因为尽管表面的括号相似,但语言在许多基本方面都是非常不同的。

#3


6  

Use a std::vector<std::string>. It's much easier to deal with than C-style arrays.

使用std::向量< std::string >。它比c型数组更容易处理。

#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方法没有更改配置对象的状态,请考虑使用const:

std::vector<std::string> Config::getVehicles() const { ... }

#4


0  

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)。你第一个例子显示当地的直接分配数组(测试),所以你不能返回它(本地数组死了),在本例中,局部变量也死去,但总是一个变量访问分配的内存的一部分,这个函数,然后接收函数的结果的变量,变量测试是死后调用的函数,但仍然是分配的内存。的问候。