I am trying to copy a database table in a map where its the primary key will be the key of map and the rest of the columns are instances of boost:vector. I am new to boost as well as variadic templates. I have tried to write a wrapper, but it works fine only for a fixed number of columns. Following is the code
#include <boost/container/vector.hpp>
#include <iostream>
#include <string>
#include <map>
#include <type_traits>
typedef boost::container::vector<std::string> MAPPED_COLS;
typedef std::map <int, MAPPED_COLS > TABLE ;
typedef std::map <int, MAPPED_COLS > ::iterator ROW_ITER;
typedef std::string str;
template <typename str>
class MappedTable
{
private:
TABLE mapTable;
MAPPED_COLS cols;
ROW_ITER row;
std::string scTableName;
int iRows;
int iCols;
public:
MappedTable() { iCols=3; }
MappedTable(int iNumCols) { iCols=iNumCols;}
~MappedTable() { }
template <str>
void fnRowCols() //termination version
{
}
template <str>
void fnCols(const str& scCol2, const str& scCol3,...)
{
if(cols.size()>=iCols)
{
cols.erase (cols.begin(),cols.begin()+iCols);
}
cols.push_back(scCol2);
fnCols(scCol3,...);
}
template <str>
void fnMapRow(int iCol1,const str& scCol2,...)
{
fnCols(scCol2,...);
mapTable[iCol1]=MAPPED_COLS(cols);
}
MAPPED_COLS& fnGetRow(int iFindKey)
{
row=mapTable.find(iFindKey);
if(row!=mapTable.end())
return (row->second);
}
};
Following is the main() for the above wrapper which works fine if i am not using variadic templates in my wrapper:-
int main()
{
MappedTable Table(3) ;
std::string vid[]={"11", "21", "51", "41"};
std::string fare[]={"100", "400", "200", "4000"};
std::string vehicle[]={"bus", "car", "train", "aeroplane"};
int i=0;
for(i=0;i<4;i++)
{
Table.fnMapRow(i,vid[i],fare[i],vehicle[i]);
}
for(i=0;i<4;i++)
{
MAPPED_COLS mpCol=Table.fnGetRow(i);
std::cout<<"\n "<<i<<" "<<mpCol[0]<<" "<<mpCol[1]<<" "<<mpCol[2];
}
std::cout<<"\n";
return 0;
}
The code was compiled with Boost 1.51.0 and gcc 4.4 with std=c++0x option
Can anyone suggest me what is that i am missing?
I am open to better ideas and as well as keen to know how this particular example would work even if not efficient enough.
The working code snippet is available in my answer below (Thanks to Rost).
It would be great if anyone can suggest some better and more methods to store an entire table into a map.
Thanks !!
Your variadic template function syntax doesn’t look correct. It shall be like this:
Similar problem with
fnMapRow.Also
template <str>is not needed before template member function definitions.