I'm still relatively new to C++ and I've written a program that could do with being a bit shorter and faster.
I use this same set of for loops many times to cycle through the whole tree of values. I'll use the JSON format to show you a much shortened version of the tree (in reality the tree has another layer of depth with many more values):
{
"WorldOne": {
"Earth": {
"Japan": "Tokyo",
"America": "New York"
},
"Moon": {
"Dark Side": "Crater One"
}
}
}
I then parse this and fill a large vector with these values. The format of the vectors can be seen in the loop below.
My program then, depending on the parameters I set, cycles through to grab the data contained within.
This is the loop I am using over and over again:
for ( auto cWorld : fWorldVec ) //vector of world pointers
{
for ( auto cPlanet : ( cWorld )->fPlanetVector )
{
for ( auto cCity : cPlanet.fCityVector )
{
for (auto cValue : cCity.fValueVector)
{
// Then I do stuff
// e.g:
ConstructSomething( ( cWorld )->getWorldId(), cPlanet.getPlanetId(), cCity.getCityId(), cValue.getValueId() );
// I know the above JSON didn't contain values,
// but it does in my code for reasons that aren't worth mentioning
// This loop is used so many times throughout my code
// It would be wonderful to simply be able to grab the data much faster
}
}
}
}
I apologise if this question is too vague, but I really need a speed boost or even to make this loop shorter. I'd like any improvements on the format of my tree or how to condense these loops.
I was thinking along the lines of making some kind of address table with pointers - but I can't quite think of a way to do it.
// Then I do stuff
. We will also review that "Stuff", though ;) If you have other questions, feel free to visit Code Review Meta. \$\endgroup\$ConstructSomething
doesn't look like real code \$\endgroup\$