C++ 没有Stod()我怎么能重写
如果不在C++中使用stod()或strotd(),如何重写我的readDetails函数? 我将使用的编译器没有启用c++11,因此 在此范围错误中未声明“stod”C++ 没有Stod()我怎么能重写,c++,function,compiler-errors,C++,Function,Compiler Errors,如果不在C++中使用stod()或strotd(),如何重写我的readDetails函数? 我将使用的编译器没有启用c++11,因此 在此范围错误中未声明“stod” int readDetails(SmallRestaurant sr[]) { //Declaration ifstream inf; //Open file inf.open("pop_density.txt"); //Check condition if (!inf) { //Display cout &
int readDetails(SmallRestaurant sr[])
{
//Declaration
ifstream inf;
//Open file
inf.open("pop_density.txt");
//Check condition
if (!inf)
{
//Display
cout << "Input file is not found!" << endl;
//Pause
system("pause");
//Exit on failure
exit(EXIT_FAILURE);
}
//Declarations and initializations
string fLine;
int counter = 0;
int loc = -1;
//Read
getline(inf, fLine);
//Loop
while (inf)
{
//File read
loc = fLine.find('|');
sr[counter].nameInFile = fLine.substr(0, loc);
fLine = fLine.substr(loc + 1);
loc = fLine.find('|');
sr[counter].areaInFile = stod(fLine.substr(0, loc)); //line using stod
fLine = fLine.substr(loc + 1);
loc = fLine.find('|');
sr[counter].popInFile = stoi(fLine.substr(0, loc));
fLine = fLine.substr(loc + 1);
sr[counter].densityInFile = stod(fLine); //line using stod
counter++;
getline(inf, fLine);
}
//Return
return counter;
}
int-readDetails(SmallRestaurant sr[])
{
//声明
ifstream-inf;
//打开文件
inf.open(“pop_density.txt”);
//检查条件
如果(!inf)
{
//展示
不能使用std::istringstream
std::string number_as_text("123");
int value;
std::istringstream number_stream(number_as_text);
number_stream >> value;
编辑2:对于那些迂腐的人:
下面的示例读取一个double。与上面读取的整数非常相似
std::string number_as_text("3.14159");
double pi;
std::istringstream number_stream(number_as_text);
number_stream >> pi;
您还可以使用sprintf
的变体
编辑1:另一种解析方法
你可以试试:
std::string tract;
std::string county;
std::string state;
double value1;
char separator;
//...
std::ifstream input("myfile.txt");
//...
std::getline(input, tract, ',');
std::getline(input, county, ',');
std::getline(input, state, '|');
input >> value1;
input >> separator;
//...
input.ignore(100000, '\n'); // Ignore any remaining characters on the line
上面的内容不需要单独的字符串到数字的转换。拜托,这甚至不是他们要求的转换。而且,这显然有些重复。@BaummitAugen:他们要求将字符串转换为十进制(数字),这就是std::istringstream
所做的。@BaummitAugen:关于如何读取文件的帖子太多了,但它们都略有不同。这篇文章有“|”和“,”作为分隔符。他们问如何替换stod
,你回答如何将一些字符串解析为int
。我知道FGitW很重要对于这个repz,但至少要先阅读标题中的问题。:/So不是使用stod
转换“9.84473419420788”
,而是应该将其读入int
?