在C 中的push_back之前,将字符串转换为浮点
Converting a string to a float before push_back in C++
我有大量的CSV线,我要分解逐条线,通过逗号分裂,将这些令牌放在向量中。
这是我处理该句子中最后两个项目的小代码段:
//token for parser
string token;
//turn into a string stream
istringstream ss(line);
//ye olde vector to put things in
std::vector<float> lineContainer;
while(getline(ss, token, ','))
{
lineContainer.push_back(::atof(token));
}
当我尝试编译时,我会得到以下内容:
Error: cannot convert 'std::string' to "const char*' for argument '1' to 'double atof(const char*)'.
换句话说,将字符串转换为浮子是不可以的,至少我是如何做的。
如何将此数据类型转换为浮点?我认为这是直接的,但是我对C 的经验不足(我保证在C#中更好),并且还没有抓住所有以其他语言为理所当然的事情的细微差别。
token
是类型std::string
。atof
将C-string或const char*
指针指向nul终止的字符阵列。
lineContainer.push_back(::atof(token.c_str()));
http://www.cplusplus.com/reference/string/string/c_str/
相关文章:
- 如何将包含epoch时间的十六进制字符串转换为time_t
- 当字符串存储在变量中时,如何将字符串转换为wchar_t
- 如何将这个std::字符串转换为std::基本字符串
- 如何正确将字符串转换为标准::时间::system_clock::time_point?
- 如何在C++中将十六进制字符串转换为文本数据
- 需要帮助在 c++ 中将字符串转换为字符 ----错误 "const char *" 类型的值不能用于初始化 "char" 类型的实体
- 如何从字符串转换为双精度*
- C++:如何将 unix 时间的字符串转换为 *tm?(使用时间错误:"cannot convert 'String' to 'tm*' ")
- 如何在C++中将 Python 字符串转换为其转义版本?
- 如何在 C++11 中将标准::字符串转换为标准::u32字符串?
- 如何将整数字符串转换为整数的二维向量?
- 使用 "stringstream " 将字符串转换为整数
- 将字符串转换为浮点数或整数,而无需使用内置函数(如 atoi 或 atof)
- 将文件复制到自定义位置,存在字符串转换问题
- 将ANSI C字符串转换为UNICODE
- 如何将节点 V8 字符串转换为 C++ 字符串
- 在C++中将字符串转换为双精度,而无需科学记数法
- 使用从文件(stod、strtod、atof)中提取的数据C++从字符串转换为双精度.csv
- 如何在C++中将字符串转换为双精度?
- 将字符串转换为无符号字符数组/字节数组