I don't think a vector of vectors is the way to go.
if we assume each row is the information about a planet. Then each row is structured data. So we should use a structure to represent the data:
struct PlanetData
{
double periodOrbit;
double logintudeAtEpoch;
double longitudeOfPerihelion;
double eccentricityOfOrbit;
double semiMajorAxisOrbit;
double orbitalInclination;
double longitudeOfAscendingNode;
double angularDiameterAt1AU;
double visualMagnitudeAt1AU;
};
So now we could have a vector of this structured data:
static constexpr int CountOfPlanets = 8;
std::vector<PlanetData> elementsPlanetaryOrbits(CountOfPlanets);
Alternatively since you don't look like you need to resize the number of planets (or you only need to do that after re-compiling (its not dynamic)) you could use a std::array rather than std::vector.
std::array<PlanetData, CountOfPlanets> elementsPlanetaryOrbits;
I like the idea of being able to index the elementsPlanetaryOrbits
by a specific name Mercury
as that seems important to make sure we don't have accidents and you have done that using your constants.
static constexpr int Mercury = 0;
static constexpr int Venus = 1;
static constexpr int Earth = 2;
static constexpr int Mars = 3;
static constexpr int Jupiter = 4;
static constexpr int Saturn = 5;
static constexpr int Uranus = 6;
static constexpr int Neptune = 7;
// Notice the use of `constexpr` rather than `const`.
But here you can index into the elementsPlanetaryOrbits
by an integer and that seems like you can have some accidents. You want to try and design your code so it can not be used incorrectly.
So I like the idea of using a map (or unordered map) and specifying an enum as the key. This will prevent you from accidentally using an integer.
enum class Planets {Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune};
std::map<Planets, PlanetData> elementsPlanetaryOrbits;
OK. Now I have to point out that there are some disadvantages to maps (locality) is not good access time is always going to be slower than a vector. But you should weigh the use case of correctness against these factors (only you have enough information to make the correct choice). Personally I am always going to go for correctness and preventing mistakes first then optimize for speed when I prove to myself it needs that extra oomf (though that is a controversial opinion in the C++ world). :-)
I would start with the std::map
as above, but if I need to improve the speed I would change to a std::vector but wrap it in structure to give accesses via the enum.
// Version 1
using PlanerHolder = std::map<Planets, PlanetData>;
// Now in the future I discover that map is not suffecient.
// Version 2
class PlanerHolder
{
std::vector<PlanetData> data;
public:
PlanerHolder()
: data(CountOfPlanets)
{}
PlanetData& operator[](Planets index)
{
return data[static_cast<int>(index)];
}
// any other members you need.
};
Note: I have implemented PlanetData as a simple property bag (a structure with some unconnected data). You may want to upgrade this to a class with proper constructor and appropriate methods that support your use case (but please avoid get/set methods in preference of methods that do meaningful tasks).
Actually the first thing I would do is add a constructor so that the members are at least correctly initialized to zero. Though if you only use them in standard containers this is not required as the containers will do this for you.
// Version 1 Initialization:
#include <map>
#include <iostream>
struct PlanetData
{
double periodOrbit;
double logintudeAtEpoch;
double longitudeOfPerihelion;
double eccentricityOfOrbit;
double semiMajorAxisOrbit;
double orbitalInclination;
double longitudeOfAscendingNode;
double angularDiameterAt1AU;
double visualMagnitudeAt1AU;
};
enum class Planet {Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune};
using PlanerHolder = std::map<Planet, PlanetData const>;
PlanerHolder ElementsPlanetaryOrbits{
{Planet::Mercury, { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 }},
{Planet::Venus, { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 }},
{Planet::Earth, { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 }},
{Planet::Mars, { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 }},
{Planet::Jupiter, { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 }},
{Planet::Saturn, { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 }},
{Planet::Uranus, { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 }},
{Planet::Neptune, { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 }}
};
int main()
{
std::cout << ElementsPlanetaryOrbits[Planet::Earth].orbitalInclination << "\n";
}
struct PlanetData { double orbit; double longetudeAtEpoch; /*..*/};
? \$\endgroup\$