快捷搜索:

C++实现C#的DataTable

简单先容一下DataTable。DataTable主要基于表、行、单元格。行用聚拢包装单元格,表用聚拢包装行,大年夜致便是这样。DataColumn表示单元格,DataColumn中的字段还挺多的,在实际利用中我们可能只想用它来存一个int型的数据;DataColumnCollection顶用一个ArrayList封装DataColumn,表示一些单元格的聚拢;DataRow直接在DataColumnCollection的根基上供给一些措施,成为行记录。而DataRowCollection则用聚拢的要领封装DataRow成为表,但它不因此线性、链表等要领,而是基于红黑树

CTRL+Click to open in new tab." href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://System.Data:4.0.0.0:b77a5c561934e089/System.Data.RBTree%3c%3e">RBTreeDataRow>,主如果查询和删除对照方便,而DataTable则是DataRowCollection的封装,对看了DataTable全部实现的大年夜致代码,我不得吐槽一下:这它妈是把牛刀,我们天天用它来切菜,用着还挺方便。DataTable供给了很强大年夜的功能,然则我们基础不必要,无意偶尔候基础上是获得一个DataTable,啥都不做,直接绑定,最常用的也就Update和Insert,Select和Delete则次之。当然你会说避免应用DataTable,当然还有人说C#......

OK废话到此停止,本日我想做说的是用C++来实现DataTable。大年夜致思路照样按照C#中的来,功能没有C#中的强大年夜,但增编削查的功能照样有的,基础照样小我演习,把代码放出来跟大年夜家评论争论一下。基础上是STL中几个聚拢的运用。在我的实现在并没有应用红黑树,而是用list代替了,主要类的头文件如下:

DataColumn的实今世码如下,主如果对几个字段的封装。源码共享

#pragma once

class DataTable;#include

using namespace std;

templatetypename T>class TDataColumn

{public:

TDataColumn():columnName(string()),caption(string()){

}

TDataColumn(const string& _columnName):columnName(_columnName),caption(string())

{

}

virtual ~TDataColumn(){

}

void Caption(const string& _caption)

{caption=_caption;

}string Caption()

{return caption.size() ?caption:columnName;

}

voidColumnName(const string& _columnName){

columnName=_columnName;}

string ColumnName(){

return columnName;}

void Value(const T& _value)

{value=_value;

}T Value()

{return value;

}

int ObjectId(){

return objectId;}

void Table(DataTable* tb)

{table=tb;

}

DataTable* Table(){

return table;}

string TypeName()

{return typeid(T).name();

}

protected:int objectId;

string columnName;string caption;

T value;DataTable* table;

};

class DataColumn:public TDataColumn

{public:

friend class DataColumnCollection;friend class DataTable;

DataColumn():TDataColumn(){

objectId=++objectCountId;}

DataColumn(const DataColumn& dc)

{*this=dc;

}

DataColumn& operator=(const DataColumn& dc){

objectId=++objectCountId;columnName=dc.columnName;

caption=dc.caption;value=dc.value;

table=dc.table;return *this;

}

DataColumn(const string& _columnName):TDataColumn(_columnName){

objectId=++objectCountId;}

bool operator==(const DataColumn& dc)

{return objectId==dc.objectId;

}

bool operator!=(const DataColumn& dc){

return objectId!=dc.objectId;}

bool operatorconst DataColumn& dc)

{return objectId

}

bool operator>(const DataColumn& dc){

return objectId>dc.objectId;}

void Show()

{cout"columnName:"",caption:"",value:"

}

private:static volatile int objectCountId;

};

其他文件的头文件如下:

#pragma once

#includeusing namespace std;

#include#include

#include#include

#include "DataColumn.h"#include "StringHelper.h"

class DataColumnCollection;

class DataRow;class DataRowCollection;

class DataTable;

class DataColumnCollection{

public:friend class DataTable;

friend class DataRow;friend class DataRowCollection;

DataColumnCollection(DataTable* table);~DataColumnCollection();

void Add(DataColumn* dc);void Add(DataColumn* dc,size_t index);

DataColumn& Add(const string& columnName);

bool Contains(const string& columnName);

void Clear();size_t Count();

int IndexOf(const string& columnName);

void Remove(DataColumn& dc);void Remove(const string& columnName);

void RemoveAt(size_t index);

void CopyTo(DataColumnCollection* arr, size_t index);

DataColumn& operator[](size_t index);DataColumn& operator[](const string& columnName);

void Table(DataTable* tb);

DataTable* Table();private:

void Add(DataColumn* dc,size_t index,DataColumnCollection* collect);void RemoveAt(size_t index,DataColumnCollection* collect);

void InitData();DataColumnCollection();

vector dlist;mapint,DataColumn*> nameList;

DataTable* table;};

class DataRow

{public:

friend class DataTable;friend class DataRowCollection;

friend class DataColumnCollection;~DataRow();

void Table(DataTable* tb);DataTable* Table();

DataColumnCollection& Columns();

DataColumn& operator[](size_t index);DataColumn& operator[](const string& columnName);

bool operator=(const DataRow& dr);private:

DataRow();void Remove(DataColumn& dc);

void RemoveAt(size_t index);DataColumnCollection* columns;

DataTable* table;int rowId;

static volatile int rowIdCount;};

class DataRowCollection

{public:

friend class DataTable;friend class DataColumnCollection;

DataRowCollection(DataTable* table);~DataRowCollection();

void Add(DataRow* dc);void Clear();

size_t Count();void Remove(DataRow* dc);

void RemoveAt(size_t index);DataRow& operator[](size_t index);

void Table(DataTable* tb);DataTable* Table();

private:DataRowCollection();

list drlist;DataTable* table;

};

class DataTable

{public:

friend class DataColumnCollection;friend class DataRowCollection;

DataTable();DataTable(const string& _tableName);

~DataTable();

DataColumnCollection& Columns();DataRowCollection& Rows();

string TableName();

void TableName(const string& _tableName);DataRow& operator[](size_t index);

void Clear();DataRow* NewRow();

vector* Select(const string& columnName,const string& value) ;private:

void InitData();DataColumnCollection* dcCollect;

DataRowCollection* drCollect;string tableName;

};

测试代码如下:

#include

using namespace std;#include

#include "DataColumn.h"#include "DataTable.h"

#include "OperationTimer.h"

int main( ){

DataTable dt;for(size_t i=0;i

{string s("column");

s.push_back(char('0'+i));DataColumn *dc=new DataColumn(s);

dt.Columns().Add(dc);}

for(int j=0;j

{DataRow* dr=dt.NewRow();

for(int i=0;i{

string s("row");s.push_back(char('0'+j));

s.push_back(char('0'+i));(*dr)[i].Value(s);

}dt.Rows().Add(dr);

}

int rowCount=dt.Rows().Count();int columnCount=dt.Columns().Count();

cout"共""行""列"rowCount=rowCount/2;

columnCount=columnCount/2;cout"第""行""列的值为:"

dt.Columns().Add(new DataColumn("column10"));

dt.Columns().Add(new DataColumn("column11"));cout"新增两列后:"

rowCount=dt.Rows().Count();columnCount=dt.Columns().Count();

cout"共""行""列"rowCount=rowCount/2;

columnCount=columnCount/2;cout"第""行""列的值为:"

cout"删除一行一列后:"dt.Columns().RemoveAt(4);

dt.Rows().RemoveAt(2);

rowCount=dt.Rows().Count();columnCount=dt.Columns().Count();

cout"共""行""列"rowCount=rowCount/2;

columnCount=columnCount/2;cout"第""行""列的值为:"

cout"查询列名为column3,值为row43的行,数据如下:"

vector* vect=dt.Select("column3","row43");for(size_t i=0;isize();i++)

{DataColumnCollection& dcCollect= (*vect)[i]->Columns();

for(size_t j=0;j{

dcCollect[j].Show();}

}delete vect;

getchar();

return 0;}

布局截图:

在吐个槽,STL中的map中的key怎么不能是string呢,C#可是啥都可以,难道用map还得自己写个hash算法,在这里我只好自己实现了一个简单的hash算法。

您可能还会对下面的文章感兴趣: