有沒有辦法將'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