我有以下代码:
template <class T>
bool loadCSV (const QString &filename, map<T,int> &mapping){
QFile boxfile;
boxfile.setFileName(filename);
QString line;
QStringList list;
if (!boxfile.open(QIODevice::ReadOnly)){
cout << "Could not open box data file." << endl;
return false;
}
QTextStream stream2( &boxfile );
while (!stream2.atEnd()){
line = stream2.readLine();
list = line.split(',');
mapping[list.front().toInt()]=list.back().toInt();
}
return true;
}
它需要一个 CSV 文件并将其粘贴到:
map<int, int> mapping
结构体。现在这是作为模板完成的,因此我也可以使用将数据粘贴到
map<string, int> mapping
结构体。现在要做到这一点,需要更改 while 循环中的最后一行,我不确定实现这一点的最佳方法是什么。
我可以想到几个方法:
一些如何检测类型并在那里有某种条件行(我实际上不确定这是否可行,如果可以,如何做到这一点。
向 QStringList 添加一个类函数来执行此操作。
我尝试了选项 2 并这样做了:
void QStringList::cInsert(map<int,int> &mapping){
mapping[this->front().toInt()]=this->back().toInt();
}
void QStringList::cInsert(map<string,int> &mapping){
mapping[(this->front()).toAscii()]=this->back().toInt();
}
这不起作用,因为我还需要在 QStringList 类中定义这些函数,所以这会有点混乱。反而。我尝试从 QStringList 继承:
class myQStringList: public QStringList{
public:
void cInsert(map<int,int> &mapping);
void cInsert(map<string,int> &mapping);
};
void myQStringList::cInsert(map<int,int> &mapping){
mapping[this->front().toInt()]=this->back().toInt();
}
void myQStringList::cInsert(map<string,int> &mapping){
mapping[(this->front()).toAscii()]=this->back().toInt();
}
然后更改代码:
template <class T>
bool loadCSV (const QString &filename, map<T,int> &mapping){
QFile boxfile;
boxfile.setFileName(filename);
QString line;
myQStringList list;
if (!boxfile.open(QIODevice::ReadOnly)){
cout << "Could not open box data file." << endl;
return false;
}
QTextStream stream2( &boxfile );
while (!stream2.atEnd()){
line = stream2.readLine();
list = line.split(',');
list.cInsert(mapping);
}
return true;}
但。我收到与 line.split/list 分配有关的错误:
main.cpp:123: error: no match for 'operator=' in 'list = QString::split(const QChar&, QString::SplitBehavior, Qt::CaseSensitivity) const(((const QChar&)(&QChar(44))), KeepEmptyParts, CaseSensitive)'
我不确定这个错误是什么,也不确定它是否与未继承的分配/复制运算符有关?
而关于实际的新类,我得到这个错误:
main.cpp:104: error: no match for 'operator[]' in 'mapping[QString::toAscii() const()]'
c:/qt/mingw/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/stl_map.h:332: note: candidates are: _Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = std::string, _Tp = int, _Compare = std::less<std::string>, _Alloc = std::allocator<std::pair<const std::string, int> >]
main.cpp: In function `bool loadCSV(const QString&, std::map<T, int, std::less<_Key>, std::allocator<std::pair<const T,int> > >&) [with T = int]':
我根本不明白。谁能解释一下?
另外,我不确定我的处理方式是否正确,并且也希望得到与此相关的建议。谢谢。