从c_str()到float的转换

Conversion from c_str() to float

本文关键字:float 转换 str      更新时间:2023-10-16

我下载了一个代码,实现了一个BoW (Bag of Words),我在这一行得到一个错误:

float label=atof(entryPath.filename().c_str());

它说:类型"const boost::filesystem::path::value_type *"的参数与类型"const char *"的参数不兼容

我已经搜索了一点,我已经看到有一个问题,这种类型的字符串到字符之间的转换,但我找不到任何相关的浮点转换。

我也看到了stringc_str之间的差异,我认为错误是在atof中,因为我认为只能转换string而不能转换c_str。这是可能的错误吗?

另外,我是boost库的新手,我不知道如何处理这种情况。

非常感谢,对不起我的英语

如果你使用boost::filesystem为什么不使用boost::lexical_cast ?如果你在Windows上工作,那么atof将不能与path.c_str()一起工作,因为value_typewchar_t

float label = boost::lexical_cast<float>(entryPath.filename().string());

可以很好地工作。

或者直接使用

float label = atof(entryPath.filename().string().c_str());