将行拆分为整数

发布于 2024-08-15 20:19:12 字数 711 浏览 1 评论 0原文

我有一个我读取的文件,它包含一堆行,每行都有不同数量的整数,我无法将其拆分为整数向量的向量。

这是我当前的代码。

std::vector<int> read_line()
{
    std::vector<int> ints;
    int extract_int;
    while((const char*)std::cin.peek() != "\n" && std::cin.peek() != -1)
    {
        std::cin >> extract_int;
        ints.push_back(extract_int);
    }
    return ints;
}
std::vector<std::vector<int> > read_lines()
{
    freopen("D:\\test.txt", "r", stdin);
    freopen("D:\\test2.txt", "w", stdout);
    std::vector<std::vector<int> > lines;
    while(!std::cin.eof())
    {
        lines.push_back(read_line());
    }
    return lines;
}

问题是所有的整数都被作为一行读取。

我做错了什么?

I have a file that I read from, it contains a bunch of lines each with a different number of integers, I'm having trouble splitting it up into a vector of a vector of ints.

This is my current code.

std::vector<int> read_line()
{
    std::vector<int> ints;
    int extract_int;
    while((const char*)std::cin.peek() != "\n" && std::cin.peek() != -1)
    {
        std::cin >> extract_int;
        ints.push_back(extract_int);
    }
    return ints;
}
std::vector<std::vector<int> > read_lines()
{
    freopen("D:\\test.txt", "r", stdin);
    freopen("D:\\test2.txt", "w", stdout);
    std::vector<std::vector<int> > lines;
    while(!std::cin.eof())
    {
        lines.push_back(read_line());
    }
    return lines;
}

The problem is that all of the ints are being read as a single line.

What am I doing wrong?

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

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

发布评论

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

评论(3

我为君王 2024-08-22 20:19:12

您可能需要使用 getline() 来阅读在一行中,然后是该行中的 字符串流 而不是尝试处理整个文件作为单个流。

You may want to use getline() to read in a line, and then a string stream out of that line instead of trying to treat the entire file as a single stream.

十级心震 2024-08-22 20:19:12

问题是你的 (const char *)std::cin.peek() != "\n" 转换。 强制转换是邪恶的;尽量避免使用它们。以下代码有效:

std::vector<int> read_line()
{
    std::vector<int> ints;
    int extract_int;
    while(std::cin.peek() != '\n' && std::cin.peek() != -1)
    {
        std::cin >> extract_int;
        ints.push_back(extract_int);
    }

    std::cin.ignore(); // You need this to discard the \n

    return ints;
}

The problem is your (const char *)std::cin.peek() != "\n" cast. casts are evil; try to avoid using them. The following code works:

std::vector<int> read_line()
{
    std::vector<int> ints;
    int extract_int;
    while(std::cin.peek() != '\n' && std::cin.peek() != -1)
    {
        std::cin >> extract_int;
        ints.push_back(extract_int);
    }

    std::cin.ignore(); // You need this to discard the \n

    return ints;
}
我的黑色迷你裙 2024-08-22 20:19:12

将单行读入向量,

std::vector<int> read_line()
{
  std::vector<int> ints;

  std::string line;
  std::getline(std::cin, line);

  int i;
  std::stringstream ss(line);
  while (ss >> i)
    ints.push_back(i);

  return ints;
}

然后将所有向量读入,

std::vector< std::vector<int> > read_lines()
{
  std::vector< std::vector<int> > lines;

  while (std::cin) {
    std::vector<int> line = read_line();

    if (std::cin)
      lines.push_back(line);
  }

  return lines;
}

然后打印结果,

template<class T>
struct print : public std::unary_function<T,void>
{
  print(std::ostream &out) : os(out) {}
  void operator() (T x) { os << '[' << x << ']'; }
  std::ostream &os;
};

void print_vector(const std::vector<int> &v)
{
  std::for_each(v.begin(), v.end(), print<int>(std::cout));
  std::cout << '\n';
}

int main()
{
  std::vector< std::vector<int> > lines = read_lines();

  std::for_each(lines.begin(), lines.end(), print_vector);

  return 0;
}

例如:

$ cat input
1 2 3 4
5 6
7 8 9
10

$ ./try.exe <input
[1][2][3][4]
[5][6]
[7][8][9]
[10]

Read a single line into a vector with

std::vector<int> read_line()
{
  std::vector<int> ints;

  std::string line;
  std::getline(std::cin, line);

  int i;
  std::stringstream ss(line);
  while (ss >> i)
    ints.push_back(i);

  return ints;
}

and then all vectors with

std::vector< std::vector<int> > read_lines()
{
  std::vector< std::vector<int> > lines;

  while (std::cin) {
    std::vector<int> line = read_line();

    if (std::cin)
      lines.push_back(line);
  }

  return lines;
}

Then print the result with

template<class T>
struct print : public std::unary_function<T,void>
{
  print(std::ostream &out) : os(out) {}
  void operator() (T x) { os << '[' << x << ']'; }
  std::ostream &os;
};

void print_vector(const std::vector<int> &v)
{
  std::for_each(v.begin(), v.end(), print<int>(std::cout));
  std::cout << '\n';
}

int main()
{
  std::vector< std::vector<int> > lines = read_lines();

  std::for_each(lines.begin(), lines.end(), print_vector);

  return 0;
}

For example:

$ cat input
1 2 3 4
5 6
7 8 9
10

$ ./try.exe <input
[1][2][3][4]
[5][6]
[7][8][9]
[10]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文