我如何从函数返回一个数组? [英] How do I return an array from a function?
问题描述
我没有得到一个错误信息,当我编译code,但我不能正确的结果。
的#include<&iostream的GT;
使用命名空间std;结构坐标{
INT R;
INT℃;
};
结构CoordwValue {
坐标℃;
char值;
};CoordwValue * getNeighbors();诠释主(){
CoordwValue * K = getNeighbors();
的for(int i = 0;我4;;我++)
COUT&所述;&下;(K [I]。价值);
}
CoordwValue * getNeighbors(){
CoordwValue邻居[4];
Neighbors->值='X';
Neighbors-> C.R = 0;
Neighbors-> C.C = 1;
(邻居+ 1) - >值='0';
(邻居+ 1) - > C.R = 1;
(邻居+ 1) - > C.C = 2;
(邻居+ 2) - >值='1';
(邻居+ 2) - GT; C.R = 2;
(邻居+ 2) - GT; C.C = 1;
(邻居+ 3) - >值='X';
(邻居+ 3) - > C.R = 1;
(邻居+ 3) - > C.C = 0;
//的for(int i = 0;我4;;我++)
// COUT<<邻居[I] .value的;
返回邻居;
}
在code打印X01X的这部分
的for(int i = 0;我4;;我++)
COUT<<邻居[I] .value的;
但我不能得到相同的结果。
的for(int i = 0;我4;;我++)
COUT&所述;&下;(K [I]。价值);
这是什么问题?
编辑:
这在code版本工作正常。
的#include<&iostream的GT;
使用命名空间std;
字符* getNeighbors();诠释主(){
字符* K = getNeighbors();
的for(int i = 0;我4;;我++)
COUT&所述;≤(*(K + I));
}
字符* getNeighbors(坐标C,INT R){
炭邻居[4];
*邻居='X';
*(邻居+ 1)='0';
*(邻居+ 2)='1';
*(邻居+ 3)=X
返回邻居;
}
如果您想返回四个对象的数组,你不一定需要使用动态分配或的std ::矢量
。你只需要包装在阵列中的一类,这样就可以返回。例如:
结构GetNeighborsResult
{
CoordwValue值[4];
};GetNeighborsResult getNeighbors();
升压,TR1和C ++ 0x中都有一个类似于容器的阵列
,你可以很容易地用于此用途类模板:
的std ::阵列< CoordwValue,4为H. getNeighbors();
使用阵列
的好处是,你不必写每一个类型和数量,你有,你可以只使用类模板一个单独的类。
如果你选择返回一个指向一个动态分配的数组,使用智能指针以管理内存。没有任何理由不使用智能指针。
I don't get a error message when I compile the code but I cant a proper result.
#include <iostream>
using namespace std;
struct Coord{
int r;
int c;
};
struct CoordwValue{
Coord C;
char Value;
};
CoordwValue* getNeighbors();
int main (){
CoordwValue *k= getNeighbors();
for (int i=0;i<4;i++)
cout<<(k[i].Value);
}
CoordwValue *getNeighbors(){
CoordwValue Neighbors[4];
Neighbors->Value='X';
Neighbors->C.r= 0;
Neighbors->C.c= 1;
(Neighbors+1)->Value='0';
(Neighbors+1)->C.r= 1;
(Neighbors+1)->C.c= 2;
(Neighbors+2)->Value='1';
(Neighbors+2)->C.r= 2;
(Neighbors+2)->C.c= 1;
(Neighbors+3)->Value='X';
(Neighbors+3)->C.r= 1;
(Neighbors+3)->C.c= 0;
//for (int i=0;i<4;i++)
// cout<<Neighbors[i].Value;
return Neighbors;
}
This part of the code prints X01X
for (int i=0;i<4;i++)
cout<<Neighbors[i].Value;
But I can't get the same result from
for (int i=0;i<4;i++)
cout<<(k[i].Value);
What is the problem?
Edit:
This version of the code works fine.
#include <iostream>
using namespace std;
char* getNeighbors();
int main (){
char *k= getNeighbors();
for (int i=0;i<4;i++)
cout<<(*(k+i));
}
char *getNeighbors(Coord C, int r){
char Neighbors[4];
*Neighbors='X';
*(Neighbors+1)='0';
*(Neighbors+2)='1';
*(Neighbors+3)='X'
return Neighbors;
}
If you want to return an array of four objects, you don't necessarily need to use dynamic allocation or std::vector
. You just need to wrap the array in a class so that you can return it. For example:
struct GetNeighborsResult
{
CoordwValue Value[4];
};
GetNeighborsResult getNeighbors();
Boost, TR1, and C++0x all have a container-like array
class template that you can easily use for this purpose:
std::array<CoordwValue, 4> getNeighbors();
The advantage of using array
is that you don't have to write a separate class for every type and number that you have, you can just use the class template.
If you do choose to return a pointer to a dynamically allocated array, use a smart pointer to manage the memory. There is no reason whatsoever to not use a smart pointer.
这篇关于我如何从函数返回一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!