I have read many similar questions with answers, but I am still unable to sort this problem out. While my actual development is actually much more complex, the following short code block distills the problem to what appears to me to be a simple question.
Below I demonstrate how columns of a table are entered as vectors. This is similar to the way a dataframe can be passed into Rcpp code. Since these columns are all of the same type I can build a single row vector. I really want such a row to be encapsulated in a Slice type that I can pass around as a pointer. I can do this for a single row, but attempt to create a vector of such rows fails.
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
class Slice {
public:
vector<double> row;
Slice(const std::vector<double> &row);
};
Slice::Slice(const std::vector<double> &_row) {
row = {_row[0], _row[1], _row[2]};
}
int main() {
vector<double> col0={1,2,3,4};
vector<double> col1={10,20,30,40};
vector<double> col2={100,200,300,400};
int num_rows = (int) col0.size();
vector<vector<double>> columns{col0, col1, col2};
vector<double> row0;
for(int i=0; i<(int) columns.size() ; i++) {
row0.push_back((double) columns[i][0]);
}
shared_ptr<Slice> slice0(new Slice(row0));
for (int x : slice0->row) {
cout << x << " ";
}
/*
// g++ -std=c++11 compile fails at slices.push_back
vector<shared_ptr<Slice>> slices;
for(int i=0; i<num_rows; i++) {
vector<double> row;
for(int j=0; j<(int) columns.size(); j++) {
row.push_back((double) columns[j][i]);
}
slices.push_back(new Slice(row));
}
*/
return 0;
}
Aucun commentaire:
Enregistrer un commentaire