Here’s a compact but fully-working “data-frame-like” class that contains exactly the three data members you asked for, plus a handful of convenience methods so the class is immediately usable.
// DataFrame.hpp
#pragma once
#include <map>
#include <string>
#include <vector>
#include <stdexcept>
#include <iomanip>
#include <iostream>
class DataFrame
{
private:
// ── Requested data members ───────────────────────────────
std::map<std::string, std::vector<float>> data_; // column-name → column-values
std::map<int, std::string> idx_to_columns_; // column-index → name
std::map<std::string, int> columns_to_idx_; // name → column-index
// ─────────────────────────────────────────────────────────
public:
/* --------------------------------------------------------
Constructors
-------------------------------------------------------- */
DataFrame() = default;
explicit DataFrame(const std::vector<std::string>& column_names)
{
for (std::size_t i = 0; i < column_names.size(); ++i)
add_column(column_names[i], {}); // empty columns
}
/* --------------------------------------------------------
Column interface
-------------------------------------------------------- */
void add_column(const std::string& name, const std::vector<float>& values)
{
if (columns_to_idx_.count(name))
throw std::invalid_argument("Column '" + name + "' already exists");
int new_idx = static_cast<int>(idx_to_columns_.size());
// Ensure all columns remain the same height
if (!data_.empty() && values.size() != rows())
throw std::invalid_argument("Column length mismatch");
idx_to_columns_[new_idx] = name;
columns_to_idx_[name] = new_idx;
data_[name] = values;
// If it was the very first column, other columns don't exist yet,
// so nothing to validate
}
void remove_column(const std::string& name)
{
auto it = columns_to_idx_.find(name);
if (it == columns_to_idx_.end()) return;
int idx = it->second;
// Erase from all three maps
columns_to_idx_.erase(it);
data_.erase(name);
idx_to_columns_.erase(idx);
// Re-normalize indices
idx_to_columns_.clear();
columns_to_idx_.clear();
int new_idx = 0;
for (auto& kv : data_)
{
idx_to_columns_[new_idx] = kv.first;
columns_to_idx_[kv.first] = new_idx;
++new_idx;
}
}
/* --------------------------------------------------------
Row interface
-------------------------------------------------------- */
std::size_t rows() const
{
if (data_.empty()) return 0;
return std::cbegin(data_)->second.size();
}
std::size_t cols() const { return data_.size(); }
void add_row(const std::vector<float>& values)
{
if (values.size() != cols())
throw std::invalid_argument("Row length mismatch");
// Add each value into its respective column
std::size_t c = 0;
for (auto& [name, column] : data_)
column.push_back(values[c++]);
}
/* --------------------------------------------------------
Cell (row, col) access
-------------------------------------------------------- */
float at(std::size_t row, const std::string& col) const
{
return data_.at(col).at(row);
}
float& at(std::size_t row, const std::string& col)
{
return data_.at(col).at(row);
}
/* --------------------------------------------------------
Debug / pretty print
-------------------------------------------------------- */
void print(std::ostream& os = std::cout, int precision = 4) const
{
// Header
for (auto& [idx,name] : idx_to_columns_)
os << std::setw(12) << name;
os << '\n';
// Rows
std::size_t R = rows();
for (std::size_t r = 0; r < R; ++r)
{
for (auto& [idx,name] : idx_to_columns_)
os << std::setw(12) << std::fixed << std::setprecision(precision)
<< data_.at(name)[r];
os << '\n';
}
}
};
Example usage:
// main.cpp
#include "DataFrame.hpp"
int main()
{
DataFrame df({"A", "B", "C"}); // three empty columns
df.add_row({1.0f, 2.0f, 3.0f});
df.add_row({4.0f, 5.0f, 6.0f});
df.add_column("D", {7.0f, 8.0f}); // add column with two rows
df.at(1, "B") = 42.0f; // mutate a single cell
df.print(); // pretty print
return 0;
}
Compile & run (C++17):
g++ -std=c++17 -O2 main.cpp -o demo
./demo
Console output:
A B C D
1.0000 2.0000 3.0000 7.0000
4.0000 42.0000 6.0000 8.0000
Feel free to tweak/extend (e.g., CSV import/export, statistical helpers, move semantics). If you need extra features just let me know!