C++ 使用可变模板和boost将表映射到map
我试图在映射中复制一个数据库表,其中它的主键是map的键,其余的列是boost:vector的实例。我对boost和可变模板都是新手。我曾尝试编写包装器,但它只适用于固定数量的列。下面是代码C++ 使用可变模板和boost将表映射到map,c++,boost,variadic-templates,variadic-functions,C++,Boost,Variadic Templates,Variadic Functions,我试图在映射中复制一个数据库表,其中它的主键是map的键,其余的列是boost:vector的实例。我对boost和可变模板都是新手。我曾尝试编写包装器,但它只适用于固定数量的列。下面是代码 #include <boost/container/vector.hpp> #include <iostream> #include <string> #include <map> #include <type_traits> typedef b
#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);
}
};
#包括
#包括
#包括
#包括
#包括
typedef boost::container::向量映射;
typedef std::映射表;
typedef std::map::迭代器行;
typedef std::string str;
模板
类映射表
{
私人:
表映射表;
映射_COLS COLS;
国际热核反应堆排;
std::字符串scTableName;
内部流动;
int iCols;
公众:
MappedTable(){iCols=3;}
MappedTable(int iNumCols){iCols=iNumCols;}
~MappedTable(){}
模板
void fnRowCols()//终止版本
{
}
模板
无效fnCols(常量str&scCol2,常量str&scCol3,…)
{
if(cols.size()>=iCols)
{
cols.erase(cols.begin(),cols.begin()+iCols);
}
cols.向后推(scCol2);
fnCols(scCol3,…);
}
模板
无效fnMapRow(int iCol1、const str和scCol2等)
{
fnCols(scCol2,…);
映射表[iCol1]=映射的列(列);
}
映射列和fnGetRow(int iFindKey)
{
row=mapTable.find(iFindKey);
if(row!=mapTable.end())
返回(行->秒);
}
};
以下是上述包装器的main(),如果我在包装器中不使用可变模板,它可以正常工作:-
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;
}
intmain()
{
可映射表(3);
std::string vid[]={“11”、“21”、“51”、“41”};
标准::字符串票价[]={“100”、“400”、“200”、“4000”};
std::字符串车辆[]={“公共汽车”、“汽车”、“火车”、“飞机”};
int i=0;
对于(i=0;i变量模板函数语法看起来不正确。它应该是这样的:
template <typename... VarArgs>
void fnCols(const str& scCol2, const str& scCol3, const VarArgs&... args)
{
// Non-relevant code skipped
fnCols(scCol3, args...); // Recursive call with expanding arguments pack
}
模板
无效fnCols(常量str和scCol2、常量str和scCol3、常量VarArgs和…args)
{
//跳过了不相关的代码
fnCols(scCol3,args…)//带扩展参数的递归调用包
}
fnMapRow
也存在类似问题。
另外,在模板成员函数定义之前,不需要使用模板。我找到了问题的答案。如果将来有人需要,下面是工作代码
void fnCols() //termination version
{
}
template <typename... VarArgs>
void fnCols(const str& scCol2, const VarArgs&...args)
{
if(cols.size()>=iCols)
{
cols.erase (cols.begin(),cols.begin()+iCols);
}
cols.push_back(scCol2);
fnCols(args...);
}
template <typename... VarArgs>
void fnMapRow(int iCol1, const VarArgs&... args)
{
static const int iNumArgs = sizeof...(VarArgs);
if(iNumArgs==iCols)
{
fnCols(args...);
mapTable[iCol1]=MAPPED_COLS(cols);
}
}
void fnCols()//终止版本
{
}
模板
无效fnCols(常量str和scCol2,常量VarArgs和…args)
{
if(cols.size()>=iCols)
{
cols.erase(cols.begin(),cols.begin()+iCols);
}
cols.向后推(scCol2);
fnCols(args…);
}
模板
void fnMapRow(int-iCol1,const-VarArgs&…args)
{
静态常数int iNumArgs=sizeof…(VarArgs);
如果(iNumArgs==iCols)
{
fnCols(args…);
映射表[iCol1]=映射的列(列);
}
}
非常感谢Rost,您的回答对我帮助很大。但是,请将语法更正为模板
。此外,我暂时不接受您的回答,因为我仍然保留问题,以寻求更好的方法和建议。等待您的建议。。