将输入读取到动态大小的数组中

发布于 2024-11-06 11:21:33 字数 753 浏览 2 评论 0原文

我一直在尝试做的是从标准输入读取一行并使用空格作为分隔符来分割它。

假设我有这个作为输入:

2
1 2
3 4

第一行给出了我想要读取的行数,它们都是由未知数量的空格分隔的整数行(即可以是 1 个空格,但也可以是 10 个空格)。
我一直在尝试做的事情是将这些行读入动态大小的整数数组中。

这在 Python 中非常简单:

foo = raw_input()
array = foo.split()

甚至更短:

foo = raw_input().split()

但是,由于环境的原因,我必须学习 C++ 的优点。 所以我尝试创建类似于上面的Python代码的东西:

#include <iostream>

using namespace std;

int lines;
int *array;

int main() {
    cin >> lines;
    for (int line = 0; line < lines; line++) {
        // Something.
    }
}

我似乎不知道如何分割输入行。我知道 std::cin 会读取直到到达空格。然而,我似乎想不出什么办法来计算线路上的数字数量...

稍微推动一下正确的方向将不胜感激,谢谢。

What I've been trying to do, is read a line from stdin and split it, by using whitespace as seperators.

Let's say I have this as input:

2
1 2
3 4

The first line gives me the amount of lines I'd like to read, they're all lines with integers seperated by an unknown amount of whitespace (i.e. could be 1 space, but it could also be 10 spaces).
The thing I've been trying to do is reading those lines into dynamically sized arrays of integers.

This was extremely easy in Python:

foo = raw_input()
array = foo.split()

or even shorter:

foo = raw_input().split()

However, because of the circumstances, I have to learn the beauty of C++.
So I tried to create something akin to the above Python code:

#include <iostream>

using namespace std;

int lines;
int *array;

int main() {
    cin >> lines;
    for (int line = 0; line < lines; line++) {
        // Something.
    }
}

I don't seem to know a way to split the line of input. I know that std::cin reads until it reaches a whitespace. However, I can't seem to think of something to count the amount of numbers on the line...

A little nudge into the right direction would be appreciated, thanks.

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

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

发布评论

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

评论(2

ぶ宁プ宁ぶ 2024-11-13 11:21:33

因此,鉴于您想要的只是轻轻一推,这里有一些提示..

std: :getline() - 允许您从流读取到 std::string 中。

然后,您可以使用刚刚读入的这个字符串构造一个 std::istringstream 。然后使用这个流来读取您的整数,

例如:

std::string line;
if(std::getline(std::cin, line))
{
  std::istringstream str(line);
  int lc;

  if (str >> lc) // now you have the line count..
  {
    // now use the same technique above
  }
}

哦,对于您的“动态大小的数组”,您需要查看 std::vector

so given all you wanted is a nudge, here are a couple of hints..

std::getline() - allows you to read from a stream into a std::string.

You can then construct a std::istringstream using this string which you've just read in. Then use this stream to read your ints

for example:

std::string line;
if(std::getline(std::cin, line))
{
  std::istringstream str(line);
  int lc;

  if (str >> lc) // now you have the line count..
  {
    // now use the same technique above
  }
}

oh and for your "dynamically sized array", you need to look at std::vector<>

梦行七里 2024-11-13 11:21:33

在 C++ 中,您可以使用 [] 访问字符串中的字符,就像该字符串是一个数组一样。我建议您将 cin 中的一行读入字符串中,使用 for 循环迭代字符串并检查每个字符以查看是否为空格。每当您找到非空白字符时,请将其存储在数组中。

In C++ you can access characters in a string with [], just as if that string were an array. I suggest you read a line from cin into a string, iterate over the string with a for loop and check each character to see whether it is whitespace. Whenever you find a non-whitespace character, store it in your array.

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