在 c++ 中如何将字符串数组转换为浮点数数组?

发布于 2024-12-14 20:42:35 字数 500 浏览 1 评论 0原文

基本上,我对 C++ 几乎一无所知,只用 Visual Basic 进行过简单的编程。

我希望将 csv 文件中的一堆数字存储为 float 数组。这是一些代码:

string stropenprice[702];   
float openprice[702];
int x=0;
ifstream myfile ("open.csv");
if (myfile.is_open())
{
  while ( myfile.good() )
  {
    x=x+1;
    getline (myfile,stropenprice[x]);
    openprice[x] = atof(stropenprice[x]);
    ...
  }
  ...
}

无论如何,它说:

错误 C2664:“atof”:无法将参数 1 从“std::string”转换为“const char *”

Basically, I know virtually nothing about C++ and have only programmed briefly in Visual Basic.

I want a bunch of numbers from a csv file to be stored as a float array. Here is some code:

string stropenprice[702];   
float openprice[702];
int x=0;
ifstream myfile ("open.csv");
if (myfile.is_open())
{
  while ( myfile.good() )
  {
    x=x+1;
    getline (myfile,stropenprice[x]);
    openprice[x] = atof(stropenprice[x]);
    ...
  }
  ...
}

Anyways it says:

error C2664: 'atof' : cannot convert parameter 1 from 'std::string' to 'const char *'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

留蓝 2024-12-21 20:42:35

好吧,你必须说 atof(stropenprice[x].c_str()),因为 atof() 只对 C 风格的字符串进行操作,而不是 std::string 对象,但这还不够。您仍然需要将该行标记为以逗号分隔的片段。 find()substr() 可能是一个好的开始(例如 参见此处),尽管也许更通用的标记化函数会更优雅。

这是我很久以前从某个地方偷来的标记器函数,我不记得了,所以对抄袭表示歉意:

std::vector<std::string> tokenize(const std::string & str, const std::string & delimiters)
{
  std::vector<std::string> tokens;

  // Skip delimiters at beginning.
  std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
  // Find first "non-delimiter".
  std::string::size_type pos     = str.find_first_of(delimiters, lastPos);

  while (std::string::npos != pos || std::string::npos != lastPos)
  {
    // Found a token, add it to the vector.
    tokens.push_back(str.substr(lastPos, pos - lastPos));
    // Skip delimiters.  Note the "not_of"
    lastPos = str.find_first_not_of(delimiters, pos);
    // Find next "non-delimiter"
    pos = str.find_first_of(delimiters, lastPos);
  }

  return tokens;
}

用法: std::vector; v = tokenize(line, ","); 现在对每个字符串使用 std::atof() (或 std::strtod())向量。


这里有一个建议,只是为了让您了解通常如何用 C++ 编写此类代码:

#include <string>
#include <fstream>
#include <vector>
#include <cstdlib>

// ...

std::vector<double> v;

std::ifstream infile("thefile.txt");
std::string line;

while (std::getline(infile, line))
{
  v.push_back(std::strtod(line.c_str(), NULL));  // or std::atof(line.c_str())
}

// we ended up reading v.size() lines

Well, you'd have to say atof(stropenprice[x].c_str()), because atof() only operates on C-style strings, not std::string objects, but that's not enough. You still have to tokenize the line into comma-separated pieces. find() and substr() may be a good start (e.g. see here), though perhaps a more general tokenization function would be more elegant.

Here's a tokenizer function that I stole from somewhere so long ago I can't remember, so apologies for the plagiarism:

std::vector<std::string> tokenize(const std::string & str, const std::string & delimiters)
{
  std::vector<std::string> tokens;

  // Skip delimiters at beginning.
  std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
  // Find first "non-delimiter".
  std::string::size_type pos     = str.find_first_of(delimiters, lastPos);

  while (std::string::npos != pos || std::string::npos != lastPos)
  {
    // Found a token, add it to the vector.
    tokens.push_back(str.substr(lastPos, pos - lastPos));
    // Skip delimiters.  Note the "not_of"
    lastPos = str.find_first_not_of(delimiters, pos);
    // Find next "non-delimiter"
    pos = str.find_first_of(delimiters, lastPos);
  }

  return tokens;
}

Usage: std::vector<std::string> v = tokenize(line, ","); Now use std::atof() (or std::strtod()) on each string in the vector.


Here's a suggestion, just to give you some idea how one typically writes such code in C++:

#include <string>
#include <fstream>
#include <vector>
#include <cstdlib>

// ...

std::vector<double> v;

std::ifstream infile("thefile.txt");
std::string line;

while (std::getline(infile, line))
{
  v.push_back(std::strtod(line.c_str(), NULL));  // or std::atof(line.c_str())
}

// we ended up reading v.size() lines
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文