I need to dynamically add (but not remove) vectors of doubles based on
keys/names. Please see my code below. Am I reinventing the wheel or doing
anything inefficiently? I get the feeling that I am, just not sure:
#include "stdafx.h"
#include <vector>
using namespace std;
typedef vector<double> field;
vector<field> recordset;
vector<const char*> names;
int m_RecordCount;
// Clears the recordset and "names" array
// then sets the recordset size (used in AddField).
void ResetRecordset(int RecordCount)
{
recordset.clear();
names.clear();
m_RecordCount = RecordCount;
}
// Adds a new field to the recordset
void AddField( char* Name)
{
field f;
f.resize(m_RecordCount);
recordset.push_back(f);
names.push_back(Name); // The associated key or name for this field
}
// Returns a field by name lookup. I suppose a map would be faster but I
only have around 10 fields.
const field GetField(const char* Name)
{
field ret;
for(int n = 0; n != names.size(); ++n)
if(strcmp(names[n],Name) == 0)
return recordset[n];
return ret;
}
int main(int argc, char* argv[])
{
ResetRecordset(100); // Size for all fields.
AddField("Test");
field f = GetField("Test");
if(f.size() > 1)
f[0] = 123456.789;
return 0;
}