矢量的char数组

时间:2010-06-04 11:06:09

标签: c++ arrays vector char

我有以下代码:

static unsigned char S0_gif[] = {
0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x0f, 0x00, 0x0f, 0x00, 0x91, 0x02,
..
};

它是gif文件的十六进制表示。我有500个GIF,我需要存储,所以我想使用矢量,使访问更容易。

类似的东西:

vector<char[]> gifs; 
gif.push_back( {0x47, 0x49,..} );

Then in the loop:
{
MakeImage(gif[i], sizeof gif[i] );
}

我无法找到合适的代码。任何帮助将不胜感激。

佩特里

3 个答案:

答案 0 :(得分:2)

你不能这样做,因为矢量存储恒定大小的结构,而你的大小是可变的。然而,你可以做的是存储矢量的矢量:)

vector<vector<char> > gifs; // note the neccessary space between > >
gif.push_back( vector<char>( S0_gif, S0_gif + sizeof(S0_gif) )  );

Then in the loop:
{
MakeImage( gifs[i] );
}

另一个想法,如果它们确实存储为静态变量,则不会将数据存储两次:

vector< unsigned char * > gifs; 
vector< size_t > gifsizes; 
gifs.push_back( S0_gif );
gifsizes.push_back( sizeof(S0_gif) );

Then in the loop:
{
MakeImage( gifs[i], gifsizes[i] );
}

免责声明:我可能忘了一些&,请随时纠正我。

答案 1 :(得分:0)

看起来你要连续存储所有500个GIF文件。如果不解析其标题,则无法检测每个的大小。如果你的函数MakeImage可以解析GIF标题,你可以返回指向它的下一个图像的指针。

然后循环看起来像:

char* img_ptr = S0_gif;
while ( img_ptr ) img_ptr = MakeImage( img_ptr );

答案 2 :(得分:0)

我认为最好的解决方案是生成一个声明图像矢量的C / CPP文件。所有其余的都意味着编写代码,这通常不建议用于大量的初始化(我的意见)。

unsigned char *Array[]={
S0_gif,
S1_gif,
S2_gif,
S3_gif,
...
};

生成它的代码可以用脚本语言(bash,perl,python等)轻松编写。它应该是这样的:

print "char *Array[]={"
for i in range(0,500)
   print "S"+i+"_gif"
print "};"

这是您问题的解决方案吗?

相关问题