c# - C++如何实现list的数组

标签 c# c++

我正在将一部分代码从 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/

相关文章:

c++ - 从友元函数访问静态变量

c++ - 由于非类型模板参数 : how to prevent warning?,具有零大小数组的类模板

c++ - EXC_BREAKPOINT 崩溃的原因

c# - (C#) 什么是实体?

c# - XML 反序列化内部异常

c# - unity SQLite 本地数据库

c++ - 在汇编程序中访问 std::string (c++/asm/visual studio 2015)

c++ - 如何将 tstringstream 的内容传递给接受 LPTSTR 的方法?

c# - NHibernate - 使用 QueryOver 选择多列的最简洁的方法

c# - 从 backgroundworker 获取错误值的进度