【发布时间】:2015-05-16 07:44:59
【问题描述】:
我正在尝试使用分隔符从文件中拆分数据,但之后我想再次使用空格拆分某些字符串,但我一直遇到编译器错误。这段代码如下...
PS:我没有共享所有代码,所以如果它看起来不完整,这就是原因,我只是在寻找一种重新拆分所选字符串的方法。
bool loadProperties(char* residential, char* commercial,
PropertyContainer* thePropertyContainer) {
ifstream propertyFile;
int datapos = 6;
Property* propertyType = NULL;
propertyFile.open(residential, ios::in);
if (!propertyFile.is_open()) {
cout << "Cant open file: " << residential << endl;
return false;
}
while (propertyFile.good()) {
getline(propertyFile, buffer);
typedef tokenizer<char_separator<char> > tokenizer;
char_separator<char> sep("\n\t\0,*");
tokenizer tokens(buffer, sep);
for (tokenizer::iterator pos = tokens.begin();
pos != tokens.end(); ++pos) {
if (!validate(datapos, *pos)) {
return false;
}
switch(datapos) {
case 6:
vector < string > splitstring; <------------------------ (LINE 127)
boost::split(splitstring, *pos, boost::is_any_of(" ")); <------this is where I am trying to split via white space for a second time.
if (splitstring[0] == "RS") {
propertyType = new ResSales;
} else if (splitstring[0] == "RS") {
propertyType = new ResRentals;
} else {
return false;
}
break;
case 7:
propertyType->setOwner(*pos);
break;
case 8:
propertyType->setAddress(*pos);
break;
case 9:
propertyType->setSuburb(*pos);
break;
case 10:
propertyType->setPostcode(changeString<int>(*pos));
break;
case 11:
dynamic_cast<Residential*>(propertyType)->setBedrooms
(changeString<int>(*pos));
break;
case 12:
if (propertyType->getType() == "ResSales") {
dynamic_cast<Sales*>(propertyType)->setAuctionDate(*pos);
} else if (propertyType->getType() == "ResRentals") {
dynamic_cast<Rentals*>(propertyType)->setBond
(changeString<double>(*pos));
}
break;
case 13:
if (propertyType->getType() == "ResSales") {
dynamic_cast<Sales*>(propertyType)->setPrice
(changeString<double>(*pos));
} else if (propertyType->getType() == "ResRentals") {
dynamic_cast<Rentals*>(propertyType)->setMonthlyRent
(changeString<double>(*pos));
}
break;
}
if (datapos >= 14) {
thePropertyContainer->addProperty(propertyType);
datapos = 6;
} else {
++datapos;
}
}
}
propertyFile.close();
return true;
}
基本上,如果 case 是 6,我想再次用空格分割令牌,我不知道如何实现这一点,我正在尝试使用 boost,如你所知。但我得到了错误....
非常感谢任何帮助,谢谢。
更新:在 cmets 与一些人讨论后续问题后,似乎问题出在 switch 语句范围内,现在只需要阻止代码部分并像对待一样工作,谢谢大家。
【问题讨论】:
-
您传递的是 vector
类型定义,而不是实际对象。您需要实际创建一个拆分字符串变量并将其传递给拆分。 -
我该怎么做?拆分字符串=空;在它初始化它之后?
-
输入格式是什么?你混合了很多样式(更不用说命名空间了),我认为这项工作可以做得更干净。如果您发布更纯粹的问题(没有X/Y problem 层),您可以获得更多质量建议。让它成为 SSCCE
-
输入格式非常愚蠢,但它是由讲师发送的,其中前两个输入变量是空格分割,但后面的变量是带空格的字符串。
-
@RoryThoman 遗憾的是,“输入格式非常愚蠢”对我来说还不够准确,无法实现。 (我没有让你把你的烂摊子归咎于某个外部团体:)我建议专注于这项任务,这样我就可以帮助你清理它。)
标签: c++ string boost split token