Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::CsvRow Class Reference

A CSV row with header-based field access. More...

#include <parse-csv.H>

Collaboration diagram for Aleph::CsvRow:
[legend]

Public Member Functions

 CsvRow (Array< std::string > fields)
 Construct from fields only (no header access).
 
 CsvRow (Array< std::string > fields, const Array< std::string > &header)
 Construct with header reference for name-based access.
 
const std::string & operator[] (size_t index) const
 Get field by index.
 
const std::string & operator[] (const std::string &column_name) const
 Get field by column name.
 
const std::string & at (size_t index) const
 Get field by index with bounds checking.
 
bool has_column (const std::string &column_name) const
 Check if column exists.
 
template<typename T >
T get (size_t index) const
 Get field as numeric type.
 
template<typename T >
T get (const std::string &column_name) const
 Get field as numeric type by column name.
 
size_t size () const
 Get number of fields.
 
bool empty () const
 Check if row is empty.
 
const Array< std::string > & fields () const
 Get underlying array.
 
const Array< std::string > * header () const
 Get column names (if header set).
 

Private Attributes

Array< std::string > fields_
 
const Array< std::string > * header_
 

Detailed Description

A CSV row with header-based field access.

CsvRow wraps an Array of strings and provides access to fields by column name in addition to index-based access.

// Parse CSV with header
std::istringstream ss("name,age,city\nAlice,30,NYC\nBob,25,LA");
while (ss.good())
{
if (row.size() == 0) break;
std::cout << row["name"] << " is " << row["age"] << " years old\n";
// Or use index: row[0], row[1], row[2]
}
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:138
A CSV row with header-based field access.
Definition parse-csv.H:601
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1319
Array< std::string > csv_read_row(std::istream &in, char delimiter=',')
Read a single CSV row from an input stream.
Definition parse-csv.H:158
DynList< T > maps(const C &c, Op op)
Classic map operation.

Definition at line 600 of file parse-csv.H.

Constructor & Destructor Documentation

◆ CsvRow() [1/2]

Aleph::CsvRow::CsvRow ( Array< std::string >  fields)
inlineexplicit

Construct from fields only (no header access).

Definition at line 607 of file parse-csv.H.

◆ CsvRow() [2/2]

Aleph::CsvRow::CsvRow ( Array< std::string >  fields,
const Array< std::string > &  header 
)
inline

Construct with header reference for name-based access.

Definition at line 611 of file parse-csv.H.

Member Function Documentation

◆ at()

const std::string & Aleph::CsvRow::at ( size_t  index) const
inline

Get field by index with bounds checking.

Definition at line 636 of file parse-csv.H.

References fields_, and Aleph::Array< T >::size().

◆ empty()

bool Aleph::CsvRow::empty ( ) const
inline

Check if row is empty.

Definition at line 671 of file parse-csv.H.

References fields_, and Aleph::Array< T >::size().

◆ fields()

const Array< std::string > & Aleph::CsvRow::fields ( ) const
inline

Get underlying array.

Definition at line 674 of file parse-csv.H.

References fields_.

◆ get() [1/2]

template<typename T >
T Aleph::CsvRow::get ( const std::string &  column_name) const
inline

Get field as numeric type by column name.

Definition at line 662 of file parse-csv.H.

References Aleph::maps().

◆ get() [2/2]

template<typename T >
T Aleph::CsvRow::get ( size_t  index) const
inline

Get field as numeric type.

Definition at line 655 of file parse-csv.H.

References fields_, and Aleph::maps().

◆ has_column()

bool Aleph::CsvRow::has_column ( const std::string &  column_name) const
inline

Check if column exists.

Definition at line 644 of file parse-csv.H.

References fields_, header_, Aleph::maps(), and Aleph::Array< T >::size().

◆ header()

const Array< std::string > * Aleph::CsvRow::header ( ) const
inline

Get column names (if header set).

Definition at line 677 of file parse-csv.H.

References header_.

◆ operator[]() [1/2]

const std::string & Aleph::CsvRow::operator[] ( const std::string &  column_name) const
inline

Get field by column name.

Exceptions
std::runtime_errorIf no header is set.
std::out_of_rangeIf column name not found.

Definition at line 623 of file parse-csv.H.

References fields_, header_, Aleph::maps(), and Aleph::Array< T >::size().

◆ operator[]() [2/2]

const std::string & Aleph::CsvRow::operator[] ( size_t  index) const
inline

Get field by index.

Definition at line 615 of file parse-csv.H.

References fields_.

◆ size()

size_t Aleph::CsvRow::size ( ) const
inline

Get number of fields.

Definition at line 668 of file parse-csv.H.

References fields_, and Aleph::Array< T >::size().

Member Data Documentation

◆ fields_

Array<std::string> Aleph::CsvRow::fields_
private

Definition at line 602 of file parse-csv.H.

Referenced by at(), empty(), fields(), get(), has_column(), operator[](), operator[](), and size().

◆ header_

const Array<std::string>* Aleph::CsvRow::header_
private

Definition at line 603 of file parse-csv.H.

Referenced by has_column(), header(), and operator[]().


The documentation for this class was generated from the following file: