Recommendations on how to record data

I have two functions which I am making available to a lua application:

1
2
register(str name);
record();


register will add a column to a 2d data table. record will capture the value of each registered label and store it in the next line of the table.

val1  val2  val3  val4  val5  (Register new val)
 0     0     0     0     0      
 1     53    13    12    0     
 2     112   14    -5    0   
 3     162   15    15    0      1
 4     210   17    -8    1      2 
 5     265   18    17    1      3   (Record new row)


What's the fastest way to implement these functions in C/C++?

I was thinking of something along the lines of:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
std::vector<std::string> header;
std::vector< std::vector<double> > data;

void register(std::string name) 
{
  header.push_back(name);
}

void record() 
{ 
  std::vector<double> line;
  for (std::vector<std::string>::iterator::it = header.begin(); 
       it!= header.end(); ++it)
  {
    double value = LuaGet(*it); // double LuaGet(string) returns value of the label
    line.push_back(value);
  }
  data.push_back(line);
}


With all of the dynamic memory allocation, I'm guessing that there's a better way to do this (and make it easy to read in the future). Is there a better way? Since I'll be making plots out of each column, a way which makes it easier to read each series might look like the following (but it makes it harder to align with the other data):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
std::map<std::string, std::vector<double> > data;

void register(std::string name) 
{
  data[name] = NULL;
}

void record()
{
  for (std::map<std::string, std::vector<double> >::iterator it = data.begin();
       it != data.end(); ++it)
  {
    double value = LuaGet(it->first);
    it->second.push_back(value);
  }
}



First: better not using a c/c++ keyword (register). Make it more descriptive.

Second: Both code snippets are not compatible. While vector allows a name to appear more than once map doesn't.

generally: vectors (used right) are faster than map. This is a bit more effective:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
std::vector<std::string> header;
std::vector< std::vector<double> > data;

void register(std::string name) 
{
  header.push_back(name);
}

void record()
{
  data.push_back(std::vector<double>());
  std::vector<double> &line = data.back();
  line.resize(header.size());
  
  for (size_t i = 0; i < header.size(); ++i)
  {
    double value = LuaGet(header[i]); // double LuaGet(string) returns value of the label
    line[i] = value;
  }
}
Topic archived. No new replies allowed.