I wrote a simple program to convert Roman numbers to Japanese (roomaji).
This program needs to lookup a string using a number (think std::map).
And I'm wondering about the pros/cons of the 3 methods I thought about:
Method 1
I guess the major con here is you need to initialize the data at runtime, keep track of the initialization state, etc.
typedef std::multimap<unsigned long, std::string> UnitMap;
UnitMap Units;
static bool UnitsInitialized = false;
void InitializeUnits()
{
if (!UnitsInitialized)
{
UnitsInitialized = true;
boost::assign::insert(Units)
(0, "zero")
(0, "rei")
//...
}
}
Method 2
This is very C to me.
struct Unit
{
unsigned long Value;
const std::string Roomaji;
};
const Unit Units[] =
{
{0, "zero"},
{0, "rei"}
//...
};
Method 3
This is just like the previous method except a std::pair is used.
I'm thinking the std::pair constructor is called for every unit here which is less than optimal.
This 'feels' more C++ to me though.
typedef std::pair<unsigned long, std::string> Unit;
const Unit Units[] =
{
Unit(0, "zero"),
Unit(0, "rei")
//...
};
Are there any other methods (without an external source)?
And what are the pros/cons here?