在C ++函数中返回字符串数组

时间:2011-09-23 10:08:29

标签: c++ arrays string function

我是C ++的新手。 对于学校项目,我需要创建一个能够返回字符串数组的函数。

目前我的标题中有这个:

的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;}

显然这不起作用,但这就是我想要做的事情的想法。 在Java中,这将是实现它的方法。我试过谷歌搜索我的问题,但我没有遇到任何明确的答案。

4 个答案:

答案 0 :(得分:25)

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

string* getNames() {
 string* names = new string[3];
 names[0] = "Simon";
 names[1] = "Peter";
 names[2] = "Dave"; 

 return names;
}

在这种情况下,您将返回堆中位置的指针。堆中的所有内存都必须手动释放。所以现在你的工作是删除内存,如果你不再需要它了:

delete[] names;

答案 1 :(得分:12)

在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也可以动态增长,因此在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;
}

动态分配std::string数组在技术上是可行的,但在C ++中却是一个糟糕的想法(例如C ++不提供Java所拥有的垃圾收集器)。

如果你想用C ++进行编程,那么grab a good C++ book并阅读它首先介绍...用C ++编写Java代码是灾难的一个方法,因为尽管表面支持相似,语言非常非常在许多基本方面都有所不同。

答案 2 :(得分:7)

使用std::vector<std::string>。处理比C - 样式数组更容易。

#include <string>
#include <vector>

...

std::vector<std::string> Config::getVehicles()
{
  std::vector<std::string> data;
  data.push_back("hello");
  ...
  return data;
}

查看标准库中的其他容器,它们几乎总是比C ++中的普通数组更好的选择。

如果您的getVehicles方法未更改Config对象的状态,请考虑将其设为const

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

答案 3 :(得分:0)

试试这个

#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';
        }
    }

例如,在我的电脑中,结果是

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

获取地址的地址和内容可以帮助您理解c ++中的数组真的很简陋:它不提供任何方法,您可以在不分配内存的情况下访问索引(循环中的值为6)。 你的第一个例子展示了一个本地数组的直接分配(测试),所以你不能返回它(本地数组死掉),在这个例子中,局部变量也死了,但总有一个变量可以访问这个部分分配内存,函数,然后是接收函数结果的变量,所以变量测试在调用函数后死了,但仍然分配了内存。问候。

相关问题