开发者

vector of char array

I have the following code:

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

It's a hex representation of a gif file. I have 500 gifs that I need to store like that so I want to use a vector to make it easier for access.

Something开发者_如何学编程 like:

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

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

I cannot find the right code for that. Any help would be greatly appreciated.

Petry


You cant do that, because vectors store constant sized structures, and youre's are variable sized. What you can do however, is store a vector of vector :)

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] );
}

Another idea, if they are indeed stored as static variables, is not to store the data twice:

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] );
}

Disclaimer : I probably forgot some &'s, feel free to correct me.


Looks like you are storing all 500 GIF files in a row. You cannot detect size of each without parsing its header. If your function MakeImage could parse GIF header you could return pointer to the next image from it.

Then the loop will look like:

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


I believe that the best solution is to generate a C/CPP file that declares a vector of images. All the rest means writing code, which is not generally recommended for a lot of initialization (my opinion).

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

The code for generating this can be easily written in a scripting language (bash, perl, python, etc). It should be something like this:

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

Is this a solution to your question?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜