返回一个数组? [英] Returning an array?

查看:76
本文介绍了返回一个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信技术上可以返回指向数组的第一个

元素的指针。我可以说服返回的指针像一个

数组,具有一些资格。我对

多维数组特别感兴趣。


通常说数组和指针实际上是相同的。我的

观察结果是我的(gcc)编译器知道T *,T

a1 [9]和a2 [3] [3]之间的差异。


我目前要做的是将指针(或引用)返回到

数组,该数组作为矩阵结果的容器传入

计算将另外两个矩阵作为参数。使用ostream&和/或$;
的概念相同print(& ostream out,const objType& obj){out<< obj.data;

退出; }。我不是绝对/需要/功能,但在某些情况下它会更直观地使用。


AFAIK,没有指定数组返回类型的方法。虽然我可以指定''ostream&'',但我不能指定''T [] [尺寸]''作为返回类型。我
可以使用''T *''作为返回类型,并将返回的数组转换为T *。但是

那么它与传入的类型不同。更不用说它是

相当棘手地玩那个级别的阵列。


我不想使用stl容器有多种原因。我可以

创建我自己的容器。 AAMOF,这就是我在做的事情。我不需要也不需要
甚至想要迭代器。将数组包装在某种类类型中

对象是传递数组的唯一可行方法吗?

-

"如果我们的话假设是关于任何东西而不是关于某一个或多个特定的东西,那么我们的推论构成了数学。因此,数学可能被定义为我们永远不知道我们所讨论的是什么,以及我们所说的是否属实的主题。 - Bertrand

Russell

I believe it is technically possible to return a pointer to the first
element of an array. I can persuade the returned pointer to act like an
array, with some qualifications. I''m specifically interested in
multidimensional arrays.

It is often said that arrays and pointers are virtually identical. My
observations are that my (gcc) compiler knows the difference between T*, T
a1[9], and a2[3][3].

What I''m currently trying to do is to return a pointer (or reference) to an
array which was passed in as a container for the results of matrix
calculations taking two other matrices as arguments. It''s the same concept
as using ostream& print(&ostream out, const objType& obj){ out << obj.data;
return out; }. I don''t absolutely /need/ the functionality, but it would
be more intuitive to work with in some instances.

AFAIK, there is no way to specify an array return type. Whereas I can
specify ''ostream&'', I cannot specify ''T[][dimensions]'' as a return type. I
can use ''T*'' as the return type and cast the returned array to T*. But
then it''s not the same type as was passed in. Not to mention that it is
rather tricky playing with arrays at that level.

There are various reasons I would rather not use the stl containers. I can
create my own containers. AAMOF, that''s what I''m doing. I don''t need nor
even want iterators. Is wrapping the array in some kind of class type
object the only viable approach to passing arrays around?
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell

推荐答案

Steven T. Hatton写道:
Steven T. Hatton wrote:
我相信技术上可以返回指向数组的第一个
元素的指针。


是的,这是可能的。

我可以说服返回的指针像一个
数组,具有一些资格。我对多维数组特别感兴趣。

通常说数组和指针实际上是相同的。我的观察是我的(gcc)编译器知道T *,T
a1 [9]和a2 [3] [3]之间的区别。


他们不是同一类型,如果这是你所暗示的。

我目前正在努力做的是返回一个指针(或引用)到一个
数组,该数组作为矩阵的计算结果传入,并以另外两个矩阵作为参数。这与使用ostream&的概念相同。 print(& ostream out,const objType& obj){out<< obj.data;
退出; }。我不是绝对/需要/功能,但在某些情况下它会更直观。


直观不太客观。但是,如果它对你来说很直观,那么,无论如何都要花费b $ b。

AFAIK,没有办法指定数组返回类型。虽然我可以指定''ostream&'',但我不能指定''T [] [dimension]''作为返回类型。我可以使用''T *''作为返回类型并将返回的数组转换为T *。但是
然后它与传入的类型不同。更不用说在那个级别玩阵列相当棘手了。

有各种原因我宁愿不使用stl容器。我可以创建自己的容器。 AAMOF,这就是我在做的事情。我不需要也不想要迭代器。将数组包装在某种类类型
对象中是传递数组的唯一可行方法吗?
I believe it is technically possible to return a pointer to the first
element of an array.
Yes, it is possible.
I can persuade the returned pointer to act like an
array, with some qualifications. I''m specifically interested in
multidimensional arrays.

It is often said that arrays and pointers are virtually identical. My
observations are that my (gcc) compiler knows the difference between T*, T
a1[9], and a2[3][3].
They are not the same type, if that''s what you''re alluding to.

What I''m currently trying to do is to return a pointer (or reference) to an
array which was passed in as a container for the results of matrix
calculations taking two other matrices as arguments. It''s the same concept
as using ostream& print(&ostream out, const objType& obj){ out << obj.data;
return out; }. I don''t absolutely /need/ the functionality, but it would
be more intuitive to work with in some instances.
"Intuitive" is not really objective. But if it''s intuitive to you,
by all means, do it.
AFAIK, there is no way to specify an array return type. Whereas I can
specify ''ostream&'', I cannot specify ''T[][dimensions]'' as a return type. I
can use ''T*'' as the return type and cast the returned array to T*. But
then it''s not the same type as was passed in. Not to mention that it is
rather tricky playing with arrays at that level.

There are various reasons I would rather not use the stl containers. I can
create my own containers. AAMOF, that''s what I''m doing. I don''t need nor
even want iterators. Is wrapping the array in some kind of class type
object the only viable approach to passing arrays around?




实际上是。这是一种非常常见的方法。将它包装在结构中

是最简单的事情,并且已经为几代C程序员工作过。


当然,您可以尝试使用_references_到数组,但

语法真的很复杂,至少可以这么说。虽然,你可以通过一些typedef获得它周围的
...


typedef double(& dMatrix3x3)[3] [3];


dMatrix3x3 mul(dMatrix3x3 m,双d)

{

m [0] [0] * = d; m [0] [1] * = d; m [0] [2] * = d;

m [1] [0] * = d; m [1] [1] * = d; m [1] [2] * = d;

m [2] [0] * = d; m [2] [1] * = d; m [2] [2] * = d;

返回m;

}


int main()

{

double m [3] [3] = {1,2,3,4,5,6,7,8,9};

mul(mul(m,2),5);


// m现在应该包含{10,20,30 ...

}


V



Yes, actually. That''s a very common approach. Wrapping it in a struct
is the simplest thing, and has worked for generations of C programmers.

You could, of course, try to work with _references_ to arrays, but the
syntax is really convoluted, to say the least. Although, you coudl get
around it with some typedefs...

typedef double (&dMatrix3x3)[3][3];

dMatrix3x3 mul(dMatrix3x3 m, double d)
{
m[0][0] *= d; m[0][1] *= d; m[0][2] *= d;
m[1][0] *= d; m[1][1] *= d; m[1][2] *= d;
m[2][0] *= d; m[2][1] *= d; m[2][2] *= d;
return m;
}

int main()
{
double m[3][3] = { 1,2,3,4,5,6,7,8,9 };
mul(mul(m, 2), 5);

// m should now contain { 10,20,30 ...
}

V




int * Blah()

{

int monkey [12];


返回猴子;

}

int main()< br $>
{

int * const& poo = Blah();


poo [2] = 3; // UB:阵列不再存在

}


-JKop

int* Blah()
{
int monkey[12];

return monkey;
}
int main()
{
int* const &poo = Blah();

poo[2] = 3; //UB: The array no longer exists
}

-JKop


当然,您可以尝试使用_references_来处理数组,但至少可以说
语法真的很复杂。
You could, of course, try to work with _references_ to arrays, but the
syntax is really convoluted, to say the least.



我不同意。这是简单的运算符和操作数优先级:

int& k [5]; // 5个引用整数的数组


int(& k)[5]; //对5个整数数组的引用

-JKop


I disagree. It''s simple operator and operand precedence:
int &k[5]; //an array of 5 references to integers

int (&k)[5]; //a reference to an array of 5 integers
-JKop


这篇关于返回一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆