, 1 , , , . , , addRow . . , . , row1
3 table1
, , , types
. ID
, types
. - .
template<class ID, class Type>
class TableRow {
private:
ID id_;
std::vector<Type> values_;
public:
template<class... Params>
TableRow( const ID& id, Params&&... valuePack ) :
id_( id ),
values_ { std::forward<Params>( valuePack )... }
{}
ID getID() const {
return id_;
}
std::vector<Type> getValues() const {
return values_;
}
std::size_t getSize() const {
return values_.size();
}
};
template<class ID, class Type>
class Table {
private:
std::size_t rowSize_;
std::vector<TableRow<ID, Type>> table_;
public:
explicit Table( TableRow<ID, Type> row ) {
table_.push_back( row );
rowSize_ = row.getSize();
}
void addRow( TableRow<ID, Type> row ) {
if ( row.getSize() == rowSize_ ) {
table_.push_back( row );
} else {
std::ostringstream strStream;
strStream << __FUNCTION__ << " row passed in does not match size of this table row size." << std::endl;
throw std::exception( strStream.str().c_str() );
}
}
};
...
int main() {
try {
std::string id = std::string( "John" );
std::string val1 = std::string( "a" ), val2 = std::string( "b" );
std::string val3 = std::string( "c" ), val4 = std::string( "d" );
TableRow<std::string, std::string> tr1 { id, val1, val2, val3, val4 };
Table<std::string, std::string> table( tr1 );
TableRow<std::string, std::string> tr2( "Mike", "e", "f", "g", "h" );
table.addRow( tr2 );
TableRow<std::string, std::string> tr3( "Susan", "a", "b", "c" );
TableRow<unsigned, float> trf1( 0, 2.3f, 4.5f );
TableRow<unsigned, float> trf2( 1, 4.5f, 7.8f );
Table<unsigned, float> table2( trf1 );
table2.addRow( trf2 );
TableRow<unsigned, float> trf3( 2, 3.5f, 8.7f, 9.2f, 4.8f );
} catch ( std::exception e ) {
std::cout << e.what() << std::endl;
std::cout << "\nPress any key and enter to quit." << std::endl;
char q;
std::cin >> q;
return -1;
}
std::cout << "\nPress any key and enter to quit." << std::endl;
char q;
std::cin >> q;
return 0;
}
, , , , , .
, , ; .
. ; . , .
... ...
IDs | val1, val1, val3, val4
1 | a b c d
2 | e f g h
3 | a b c d
, , (a,b,c,d)
. 2
. , ID
, X|Y
, , vectors
, , set
rows
row - ids
, , , , , , , , , 1
.
:
, . , , . ( ). .
, , , , .
, Table
template<class ID, class Type>
ID Table<ID, Type>::find( const std::vector<Type>& data ) {
for ( auto& row : table_ ) {
if ( data == row.getValues() ) {
return row.getId();
} else {
std::ostringstream strStream;
strStream << __FUNCTION__ << " could not find matching set." << std::endl;
throw std::exception( strStream.str().c_str() );
}
}
}
, , , .
addItem
addElement
TableRow
, Table
addColumn
, addColumn
. -, variadic
TableRow
, . , row's
, .
Another thing to consider is that if each item in the list of data belonging to the identifier does not have the same type, this class should be slightly modified, instead of using it std::vector<T>
you can use std::tuple<...>
and compare two tuples from there.