vector by name, recordset/field setup

R

Richard

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;
}
 
V

Victor Bazarov

Richard said:
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;

You should probably look at the possibility to use

std::map<std::string, field> recordset;

instead of separate 'names' and 'm_RecordCount' objects. BTW, you
can always request the count from the vector, can't you? Just call
recordset.size().

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top