typedef enum EM_ValueType
{
VT_INT = 0, //int类型
VT_DOUBLE, //double类型
VT_STRING //std::string类型
} ValueType;
template<typename T>
void ParseReadLine(const std::string& readLine, const std::string& split, ValueType type, std::vector<T>& vecParseResult)
{
std::string::size_type pos1, pos2;
pos2 = readLine.find(split);
pos1 = 0;
while (std::string::npos != pos2)
{
std::string tempStr = readLine.substr(pos1, pos2 - pos1);
switch (type)
{
case VT_INT:
vecParseResult.push_back(atoi(tempStr.c_str()));
break;
case VT_DOUBLE:
vecParseResult.push_back(atof(tempStr.c_str()));
break;
default:
vecParseResult.push_back(atoi(tempStr.c_str()));
break;
}
pos1 = pos2 + split.size();
pos2 = readLine.find(split, pos1);
}
if (pos1 != readLine.length())
{
std::string tempStr = readLine.substr(pos1);
switch (type)
{
case VT_INT:
vecParseResult.push_back(atoi(tempStr.c_str()));
break;
case VT_DOUBLE:
vecParseResult.push_back(atof(tempStr.c_str()));
break;
default:
vecParseResult.push_back(atoi(tempStr.c_str()));
break;
}
}
}
注意: 上述模板函数如果要增加 T=std::string 类型时,不能直接在该函数中增加,否则会引起编译错误。
错误原因: 因为模板函数体内有多种情况增加元素,此时会使编译器不知道初始化模板为哪种类型而报错!!
解决方法: 重载该函数!!如下所示:
void ParseReadLine(const std::string& readLine, const std::string& split, ValueType type, std::vector<std::string>& vecParseResult)
{
std::string::size_type pos1, pos2;
pos2 = readLine.find(split);
pos1 = 0;
while (std::string::npos != pos2)
{
std::string tempStr = readLine.substr(pos1, pos2 - pos1);
switch (type)
{
case VT_STRING:
vecParseResult.push_back(tempStr);
default:
vecParseResult.push_back(tempStr);
break;
}
pos1 = pos2 + split.size();
pos2 = readLine.find(split, pos1);
}
if (pos1 != readLine.length())
{
std::string tempStr = readLine.substr(pos1);
switch (type)
{
case VT_STRING:
vecParseResult.push_back(tempStr);
default:
vecParseResult.push_back(tempStr);
break;
}
}
}
再增加几种函数重载,可以在解析数据的同时,根据传入的处理函数对数据进行处理!!
void ParseReadLine(const std::string& readLine, const std::string& split, std::vector<int>& vecParseResult, std::function<void(int&)> fun)
{
std::string::size_type pos1, pos2;
pos2 = readLine.find(split);
pos1 = 0;
while (std::string::npos != pos2)
{
std::string tempStr = readLine.substr(pos1, pos2 - pos1);
int value = atoi(tempStr.c_str());
fun(value);
vecParseResult.push_back(value);
pos1 = pos2 + split.size();
pos2 = readLine.find(split, pos1);
}
if (pos1 != readLine.length())
{
std::string tempStr = readLine.substr(pos1);
int value = atoi(tempStr.c_str());
fun(value);
vecParseResult.push_back(value);
}
}
void ParseReadLine(const std::string& readLine, const std::string& split, std::vector<double>& vecParseResult, std::function<void(double&)> fun)
{
std::string::size_type pos1, pos2;
pos2 = readLine.find(split);
pos1 = 0;
while (std::string::npos != pos2)
{
std::string tempStr = readLine.substr(pos1, pos2 - pos1);
double value = atof(tempStr.c_str());
fun(value);
vecParseResult.push_back(value);
pos1 = pos2 + split.size();
pos2 = readLine.find(split, pos1);
}
if (pos1 != readLine.length())
{
std::string tempStr = readLine.substr(pos1);
double value = atof(tempStr.c_str());
fun(value);
vecParseResult.push_back(value);
}
}
void ParseReadLine(const std::string& readLine, const std::string& split, std::vector<std::string>& vecParseResult, std::function<void(std::string&)> fun)
{
std::string::size_type pos1, pos2;
pos2 = readLine.find(split);
pos1 = 0;
while (std::string::npos != pos2)
{
std::string tempStr = readLine.substr(pos1, pos2 - pos1);
fun(tempStr);
vecParseResult.push_back(tempStr);
pos1 = pos2 + split.size();
pos2 = readLine.find(split, pos1);
}
if (pos1 != readLine.length())
{
std::string tempStr = readLine.substr(pos1);
fun(tempStr);
vecParseResult.push_back(tempStr);
}
}
//定义一个局部函数
auto b = [](double & a)
{
a /= 2;
};
调用范例如下
std::string str = “66.6,35.8,21.6”;
std::vector vec;
ParseReadLine(str, “,”, vec,b);