tags:

views:

90

answers:

2

I am working on a program that "encodes" a file based on a supplied bookfile. The resulting file has each letter replaced with a number. This number corresponds to the offset of that letters appearence in the bookfile. So if we had "hello" it would pick an 'h' from the bookfile, find its location number and replace it in the output.

Mine works correctly but i am looking for a way to optimize it. As of right now, everytime it brings in a new letter it creates a vector of offset numbers to choose from. I would like to be able to "save" this vector and use it again if i find the same number again.

I am at a loss as to how i would program this however. For example, if i read in an 'h' i would like to save it as vector<int> hLocations;

Is there anyway of doing this or am i just insane? i was thinking of making a function that does this, but the part that confuses me is <int>Locations; Is there a way of using variables inside C++ code? i think that is what i am really asking.

+3  A: 

You could use a std::map<unsigned int, std::vector<unsigned int> >, so that the character of interest is the key to the vector of offsets. That way, you don't have to code up N different vectors for each possible character in the file.

Bill Perkins
Or a vector<vector<unsigned int> > would work just as well, since it would have at most 256 entries.
j_random_hacker
I like the map, since you don't have to reserve alot of space for any possible character. But, map is funkier to use, since you have to deal with find and iterator pairs...
Bill Perkins
Yes either would work, it's a tradeoff. I reckon (256 - number_of_used_chars) * sizeof (empty vector) is not a great deal of space to waste mind you. That second factor is probably 12 or 16 bytes, implying 4Kb total.
j_random_hacker
Last I looked, vector had three pointers, start, end and capacity. I don't know what you'd use a fourth pointer for. It could be 24 bytes wasted per empty vector on 64-bit.
Zan Lynx
@Zan: Sure, but maps also use twice the space on 64-bit. Re the 4th pointer: don't vectors need to store their allocator objects somewhere? Maybe a quality library implementation could somehow optimise this away for the default of allocator<vector<T> >, which is near-ubiquitous in my experience.
j_random_hacker
Depending upon the specifics of this problem, using a map, while having more initial allocation, might end up being a push if you don't have all 256 possible ASCII characters. I feel that with Unicode, map would end up cheaper overall.
Bill Perkins
Instead of using a pair of int and char vectors, i might use multimap and have the key as the char. Then i could find the "range" of a's and pick one randomly, then delete it from the multimap.
Deinumite
@Bill: Yes, I'd definitely use map for Unicode characters. @Deinumite: If a multimap would solve your problem then I guess I'm a bit confused about what you're trying to do, but it sounds like you know what you want at least :)
j_random_hacker
A: 

This question is pretty old but what I was asking was really "Does C++ have variable variable names?"

PHP has this...

$foo = 'bar';
$bar = 'foobar';
echo $$foo;    //This outputs foobar

I was looking for something simliar in C++.

Deinumite