E
eriwik
I use the following structure to store filenames for one or more "sets"
grouped together by a number:
map<int, map<string> > fileSets;
As arguments to the constructor I send a vector<vector<string> > where
each vector<string> contains the filenames of one set. The function
getNumber() calculates a number from the filename. The constructor then
looks like this:
Files::Files(vector<vector<string> > files&)
{
// Add files to the sets
// Loop throught all sets
for (size_t i = 0; i < files.size(); ++i)
{
vector<string>::iterator iter = files.begin();
vector<string>::iterator end = files.end();
// Add a file to fileSets[number][set]
for(; iter != end; ++iter)
{
fileSets[getNumber(*iter)] = *iter;
}
}
}
The reason that I need the first (outer) map is that the number
returned from getNumber() is very large but I don't have that many
files and there can be gaps in the numbers and so on. However I really
don't need the second (inner) map, a vector would work just as well
since I know the number of sets when the constructor is called
(files.size()).
So my question is: Is it possible to somehow "initialize" the vector in
the map to have a certain number of elements? That is if I do
fileSets[######] and that element does not already exist then a new
vector<string> will be created with a specified number of elements
allocated, so that I can then index into the vector and assigns to the
specified element?
Something like this:
Files::Files(vector<vector<string> > files&) :
fileSets(map<vector<string>(files.size()) >)
or something like that?
grouped together by a number:
map<int, map<string> > fileSets;
As arguments to the constructor I send a vector<vector<string> > where
each vector<string> contains the filenames of one set. The function
getNumber() calculates a number from the filename. The constructor then
looks like this:
Files::Files(vector<vector<string> > files&)
{
// Add files to the sets
// Loop throught all sets
for (size_t i = 0; i < files.size(); ++i)
{
vector<string>::iterator iter = files.begin();
vector<string>::iterator end = files.end();
// Add a file to fileSets[number][set]
for(; iter != end; ++iter)
{
fileSets[getNumber(*iter)] = *iter;
}
}
}
The reason that I need the first (outer) map is that the number
returned from getNumber() is very large but I don't have that many
files and there can be gaps in the numbers and so on. However I really
don't need the second (inner) map, a vector would work just as well
since I know the number of sets when the constructor is called
(files.size()).
So my question is: Is it possible to somehow "initialize" the vector in
the map to have a certain number of elements? That is if I do
fileSets[######] and that element does not already exist then a new
vector<string> will be created with a specified number of elements
allocated, so that I can then index into the vector and assigns to the
specified element?
Something like this:
Files::Files(vector<vector<string> > files&) :
fileSets(map<vector<string>(files.size()) >)
or something like that?