我正在将一部分代码从 C# 翻译成 C++。
这是我的观点:
Class Point
{
public int X;
public int Y;
}
const int MAX = 256;
public void ComputePoints(Byte[] image,
int width,
int height,
out List<Point>[] listPixels)
{
listPixels = new List<Point>[MAX];
//etc..
}
(我简化了这段代码,只显示有趣的部分)。
我的问题涉及 out List<Point>[] listPixels
.我尝试通过以下方式翻译:
public void ComputePoints(unsigned char[] image,
int width,
int height,
std::vector<Point> *listPixels[])
{
*listPixels = new std::vector<Point>[MAX];
//etc..
}
但是我有错误
Segmentation fault.
我怎样才能写出最简单的 out List<Point>[] listPixels
等价物?在 C++ 中?
最佳答案
自 List<Point>[]
是一个列表数组,您可以使用嵌套 vector ( vector 的 vector )来获得所需的行为:
std::vector<std::vector<Point> >
请注意,在两个 >
之间添加一个空格可能很重要的。如果没有,一些编译器将无法编译。
现在您可以像这样将 vector 作为引用传递
void ComputePoints(... , std::vector<std::vector<Point> > &listPixels)
{
...
关于c# - C++如何实现list的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46791606/