English 中文(简体)
采用C++方法的双向阵列 [复制]
原标题:pass two dimensional array to a method C++ [duplicate]
  • 时间:2011-11-15 16:13:31
  •  标签:
  • c++
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
How do I use arrays in C++?

我不清楚在C++中如何做到这一点:

main()
{
   float array[5][4];   

   transpose(array); 
}

void transpose(float** array);

=> I got some FCCC错误:vector<KeypointMatch, :allotor<KeypointMatch> >

我如何把两个层面阵列的变量变成C++的一种方法?

提前感谢。

最佳回答

如果你阅读或修改汇总表中的位置,那将是有问题的,但你将修改矩阵本身。 例如,你需要创建另一个矩阵。

EDITED:我转而使用独一无二的交换机,因为有意见(尽管我并不认为,OP确实准备就绪/对此感兴趣)。

EDITED 我知道,你可以将矩阵“内部”。 然而,这并不是被占领土上的问题。 感谢所有评论家。

<代码>std:unique_ptr<> 是你管理点的舱位,确保点名器在使用后自动删除。 一种选择是auto_ptr。 不幸的是,它不支持阵列的病媒。 www.un.org/Depts/DGACM/index_spanish.htm 虽然这是新的标准C++-0x的一部分,但确实支持它们。

因此,你需要修改转口的正式参数,以便:

void transpose(std::unique_ptr<float**> &array, int rows, int columns)
{
    std::unique_ptr<float **> aux = array;
    array.reset( new float[columns][rows] );

    // now transpose aux into array
}

这也意味着你不能像你那样创建原有阵列:

float array[5][4];

这是一种 st子的矩阵,你不能期望对点人本身加以修改,这是没有意义的。 更好:

int main()
{
    std::unique_ptr<float **>array( new float[5][4] );
    transpose( array, 5, 4 );
    // ... use array ...
}

尽管如此,另一种解决办法是稍微改变转盘程序的签名,使之发挥作用。

std::unique_ptr<float **> transpose(float** array, int rows, int columns)
{
    std::unique_ptr<float **> toret( new float[columns][rows] );

    // now transpose array into toret

    // end
    return toret;
}

你们不需要改变你们的方案。

int main()
{
    float array[5][4];
    std::unique_ptr<float **> transposed( transpose( array, 5, 4 ) );

    // ... more things ...
}
问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签