【问题标题】:C++ -- How to convert a string into multiple integers?C++——如何将一个字符串转换为多个整数?
【发布时间】:2019-04-11 08:34:44
【问题描述】:

我正在一个 SVG 文件中绘制形状,该文件是通过使用 .dat 文件提供的输入生成的。

我需要从 .dat 文件中取出一行,从中删除 4 个单独的整数,并将这些整数保存到一个向量中。为此,我尝试创建一个类 square,它包含 4 个整数(它们代表正方形的左上角和右下角坐标)。最好,我可以在类的构造函数中执行此操作,但我不知道该怎么做。

基本上,我知道我会有一个看起来像“1 1 50 50”的字符串,我想把它变成 4 个整数。我遇到的问题是我需要将其设为类对象的 4 个整数,而不仅仅是 4 个整数。

class SQ
{
  public:
    sq() = default;
    static int tl_x;
    static int tl_y; //top left corner
    static int br_x;
    static int br_y; //bottom right corner
};

我已经尝试了下面的代码,但它显然不起作用,因为它只保存遇到的第一个整数。

while (getline(file,s))
  {
    int *f = new int(stoi(s));
    vec.push_back(f);
  }

感谢您的帮助:)

【问题讨论】:

标签: c++ string int


【解决方案1】:

如果您将一行整数(如 "1 1 50 50")读入字符串并需要解析字符串中的整数,则处理转换的标准 C++ 方法是从您的字符串创建一个 stringstream 并使用 iostream从字符串流中提取整数。

例如:

#include <iostream>
#include <sstream>
#include <vector>
#include <string>

int main (void) {

    std::istringstream is { "1 1 50 50\n" };  /* simulated input */
    std::string s;
    std::vector<int> vect;

    while (getline (is, s)) {       /* read string */
        int f;                      /* declare int */
        std::stringstream ss (s);   /* make stringstream from s */
        while ((ss >> f))           /* read ints from ss into f */
            vect.push_back (f);     /* add f to vector */
    }

    for (auto& i : vect)            /* output integers in vector */
        std::cout << i << '\n';
}

(如果您需要单独存储所有行,只需使用std::vector&lt;std::vector&lt;int&gt;&gt; vect;

请注意,最初的 istringstream 只是模拟getline 输入的一种方式。

使用/输出示例

$ ./bin/ssint
1
1
50
50

查看一下,如果您还有其他问题,请告诉我。

【讨论】:

  • 为什么要用双括号:while ((ss &gt;&gt; f))?
  • 没必要,只是习惯而已。
【解决方案2】:

您不必将该行读入std::string,因为您可以一个一个地读取整数并将它们直接存储到一个向量中。试试下面的方法。

#include <fstream>
#include <vector>

int main()
{
  std::ifstream ifile("./yourfile.dat");

  std::vector<int> theIntegers;

  for (int idx = 0, val = 0; idx < 4; ++idx)
  {
    ifile >> val;
    theIntegers.push_back(val);
  }
}

编辑:您可以通过引用构造函数传递std::istream 来在构造函数中读取文件。在下面的示例中,我假设您想准确读取四个整数,同时丢弃该行的可能剩余部分。

#include <istream>
#include <string>
#include <array>

/*
 * This class holds four integers to describe a square
 * On construction, it reads four integers from a given std::istream
 * Note that it does not check the formatting of the input file and
 * that it reads the first four integers and discards the remaining
 * characters until the next newline character.
 */

class Square
{
  std::array<int, 4> d_coordinates;

  public:
    Square() = delete;

    Square(std::istream &iFile)
    {
      for (int idx = 0, val = 0, n =  size(d_coordinates); idx < n; ++idx)
      {
        iFile >> val;
        d_coordinates[idx] = val;
      }

      // Remove the newline character at the end of this line
      // If you for some reason want to store multiple squares
      // on a single line, then just remove the next two lines

      std::string tmp;
      std::getline(iFile, tmp); 
    }
};

int main()
{
  std::ifstream ifile("./yourFile.dat");

  Square x(ifile);
  Square y(ifile);
  Square z(ifile);
}

【讨论】:

  • 虽然这适用于每行 4 个整数,但它非常脆弱。如果您需要处理 3 个整数或每行 5 个整数,则需要更改循环限制。这是字符串流提供好处的地方。您只需从该行创建一个字符串流,然后从字符串流中读取整数,直到用完要读取的内容,无论是 1 个整数还是 1000 个整数。无需更改代码。
  • @DavidC.Rankin 是的,但是,OP 明确要求四个整数,并没有说明在同一行上是否可以有更多的整数应该被忽略。
  • 是的,没有争论。他要 4,而你给了他 4。如果有更多的整数,而只需要 4,则需要读取计数器或使用计数器。
【解决方案3】:

使用stringstreamistream_iteratorback_insertercopy 算法我们可以这样写:

while (getline(file, s))
{
    auto       str_stream       = stringstream(s);
    auto       str_iter         = istream_iterator< int >(str_stream);
    const auto end_of_str       = istream_iterator< int >();
    auto       back_insert_iter = back_inserter(vec);

    copy(str_iter, end_of_str, back_insert_iter);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多