Click here to Skip to main content
11,721,053 members (78,064 online)
Click here to Skip to main content

Tables in C++

, 28 May 2007 43.6K 409 13
Rate this:
Please Sign up or sign in to vote.
To save data of different type in the same datastructure

Introduction

In this Code I have tried to provide a functionality to save different type of data type under same umbrella.

for example map<key,value> here you are bounded with your value type. if suppose your key is always same but not the the value. and you have more than one value at a time. i mean a list of value(uncountable values) so you have to use multi map where you have to insert your
value and keys as many times as you want to insert.

but in case of table you can only key at one time and as many values you want.

typedef pair<const char*,enum DataType> mypair;
map<const char*,enum DataType> obj;
obj.insert(mypair("INT",INT));
obj.insert(mypair("STRING",CHAR_PTR));
obj.insert(mypair("DOUBLE",DOUBLE));

table tableobj(&obj);
tableobj.DescribeTable();

tableobj.InsertValue<int>("INT",12);
tableobj.InsertValue<int>("INT",13);
tableobj.InsertValue<int>("INT",14);

tableobj.InsertValue<char*>("STRING","vikas");
tableobj.InsertValue<char*>("STRING","tyagi");
tableobj.InsertValue<char*>("STRING","Trial");

tableobj.InsertValue<double>("DOUBLE",12.445);
tableobj.InsertValue<double>("DOUBLE",15.445);
tableobj.InsertValue<double>("DOUBLE",19.445);

like you have only limited no of keys and there type and than you can insert as many values as you want.

Using the code

the file name main.cpp contains a test program which is describing all what to do and how to do in this code.

but just to give a brief idea i m explaining here also.
//
// Any source code blocks look like this
//
void main()
{
    typedef pair<const char*,enum DataType> mypair;//define a pair 
    map<const char*,enum DataType> obj;//create an object of Map 
    obj.insert(mypair("INT",INT));//insert you column name and type
    obj.insert(mypair("STRING",CHAR_PTR));//using Enum DataType
    obj.insert(mypair("DOUBLE",DOUBLE));
//here i have defined three columns (INT,STRING,DOUBLE)
    table tableobj(&obj);//this is your table object which accept the object

    tableobj.DescribeTable(); //this function will describe the table

    tableobj.InsertValue<int>("INT",12);//way to insert value under INT
    tableobj.InsertValue<int>("INT",13);//second value
    tableobj.InsertValue<int>("INT",14);
    
    tableobj.InsertValue<char*>("STRING","vikas");// under STRING
    tableobj.InsertValue<char*>("STRING","tyagi");
    tableobj.InsertValue<char*>("STRING","Trial");

    tableobj.InsertValue<double>("DOUBLE",12.445);//under DOUBLE
    tableobj.InsertValue<double>("DOUBLE",15.445);
    tableobj.InsertValue<double>("DOUBLE",19.445);

    cout<<"==========Data Retrival=============="<<endl;
    List*obj1 = tableobj["STRING"];
    char* svalue = obj1->operator[]<char*>(0);//first value
    cout<<svalue<<endl;

    svalue = obj1->operator[]<char*>(1);//second value
    cout<<svalue<<endl;

    svalue = obj1->operator[]<char*>(2);//third value
    cout<<svalue<<endl;

    obj1 = tableobj["INT"];
    int ivalue = obj1->operator[]<int>(0);
    cout<<ivalue<<endl;
    
    ivalue = obj1->operator[]<int>(1);
    cout<<ivalue<<endl;

    ivalue = obj1->operator[]<int>(2);
    cout<<ivalue<<endl;

    obj1 = tableobj["DOUBLE"];
    double dvalue = obj1->operator []<double>(0);
    cout<<dvalue<<endl;

    dvalue = obj1->operator []<double>(1);
    cout<<dvalue<<endl;

    dvalue = obj1->operator []<double>(2);
    cout<<dvalue<<endl;
    
    tableobj.RemoveAt("DOUBLE",1);//remove the second value of DOUBLE column

    tableobj.RemoveValue("STRING","vikas");//remove the value "vikas" from STRING COLUMN
    
} 

Points of Interest

the most interesting thing in this code is that this contains an operator[] which can retrieve any type of data from table. the definition is written. here just pass the index and will return the data at a particular index.

template<class T>
T operator[](int index)
{
list<T>* ptr = GetListInstance<T>();
list<T>::iterator itrlist;
itrlist = ptr->begin();
while(index-- && itrlist != ptr->end())
{
itrlist++;
}
return *itrlist;
}

Plateform

this code can work on any platform. Window/Unix/Linux/Mac OSX

Limitation

unicode not supported

History

verison 1.0 this code

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

vikasktyagi
Software Developer
India India
No Biography provided

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150901.1 | Last Updated 29 May 2007
Article Copyright 2007 by vikasktyagi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid