有没有办法将'std :: vector 2d'作为指针传递给'2d c array'到一个功能。有没有办法将'std :: vector 2d'作为指向'2d c array'的指针传递给函数
我知道你可以将std :: vector 1d作为指向c数组的指针传递给某个函数。 例如, 功能:
void foo(char* str); //requires the size of str to be 100 chars
std::vector<char> str_;
str_.resize(100);
foo(&str_[0]); //works
我不知道是否是可能的2D矢量太像 功能
void foo(char** arr_2d);
和矢量
std::vector<std::vector<char>> vector_2d;
我尝试以下代码,但我得到一些与堆损坏相关的错误。
std::vector<std::vector<unsigned char>> vector_2d;
//assuming function expects the size of the vector to be 10x10
vector_2d.resize(10);
for(int i=0;i<10;i++)
{
vector_2d[i].resize(10);
}
foo(&vector_2d[0]);//error here
好吧,现在终于* *所有这些修改后您的问题有一定的道理 – 2012-07-19 21:54:54
顺便说一句,'char **'与数组数组一起使用,而不是2-D数组 – 2012-07-19 22:00:08
不能直接做,对不起。你必须从你的矢量建立一个指针数组。 – 2012-07-19 22:09:56