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.
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.