如何从C++中的标准输入读取n个整数?

发布于 2024-12-10 04:11:10 字数 194 浏览 0 评论 0原文

我需要阅读类似的内容:

5 60 35 42
2 38 6
5 8
300 1500 900

然后将第一行保存在数组中。调用其他函数后,对下一行执行相同的操作,依此类推。

我尝试使用 gets() ,然后使用 sscanf() 扫描字符串中的整数,但我不知道如何从字符串中读取 n 个数字。

I need to read something like:

5 60 35 42
2 38 6
5 8
300 1500 900

And then save the first line in an array. After calling other functions do the same with the next line, and so on.

I try with gets() and then use sscanf() to scan the integers from the string, but I don't know how to read n numbers from a string.

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

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

发布评论

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

评论(7

好多鱼好多余 2024-12-17 04:11:10

C++

如果您有未知数量的条目分布在未知数量的行上,并以 EOF 结尾:

int n;
while(cin >> n)
  vector_of_int.push_back(n);

如果您有已知数量的条目分布在未知数量的行上:

int n;
int number_of_entries = 20; // 20 for example, I don't know how many you have.
for(int i ; i < number_of_entries; ++i)
  if(cin >> n)
    vector_of_int.push_back(n);

如果您在单行上有未知数量的条目:

std::string str;
std::getline(std::cin, str);
std::istringstream sstr(str);
int n;
while(sstr >> n)
  vector_of_int.push_back(n);

如果未知数量的条目分布在已知数量的行中:

for(int i = 0; i < number_of_lines; ++i) {
  std::string str;
  if(std::getline(std::cin, str)) {
    std::istringstream sstr(str);
    int n;
    while(sstr >> n)
      vector_of_int.push_back(n);
  }
}
  

C++

If you have an unknown number of entries spread across an unknown number of lines, ending at EOF:

int n;
while(cin >> n)
  vector_of_int.push_back(n);

If you have a known number of entries spread across an unknown number of lines:

int n;
int number_of_entries = 20; // 20 for example, I don't know how many you have.
for(int i ; i < number_of_entries; ++i)
  if(cin >> n)
    vector_of_int.push_back(n);

If you have an uknown number of entries on a single line:

std::string str;
std::getline(std::cin, str);
std::istringstream sstr(str);
int n;
while(sstr >> n)
  vector_of_int.push_back(n);

If you have a unknown number of entries spread across a known number of lines:

for(int i = 0; i < number_of_lines; ++i) {
  std::string str;
  if(std::getline(std::cin, str)) {
    std::istringstream sstr(str);
    int n;
    while(sstr >> n)
      vector_of_int.push_back(n);
  }
}
  
二智少女 2024-12-17 04:11:10

我以前见过这样的比赛输入文件。如果速度比错误检测更重要,您可以使用自定义例程。这是与我使用的类似的:

void readintline(unsigned int* array, int* size) {
    char buffer[101];
    size=0;
    char* in=buffer;
    unsigned int* out=array;
    fgets(buffer, 100, stdin);
    do {
        *out=0;
        while(*in>='0') {
            *out= *out* 10 + *in-'0';
            ++in;
        }
        if (*in)
            ++in; //skip whitespace
        ++out;
    } while(*in);
    size = out-array;
}

如果一行上的字符超过 100 个,或者数字超过数组可以容纳的数量,它将破坏您的内存,但您不会获得更快的例程来读取无符号整数行。

另一方面,如果你想要简单:

int main() {
    std::string tmp;
    while(std::getline(std::cin, tmp)) {
        std::vector<int> nums;
        std::stringstream ss(tmp);
        int ti;
        while(ss >> ti) 
            nums.push_back(ti);
        //do stuff with nums
    }
    return 0;
}

I've seen input files like this for competitions before. If speed is more of an issue than error detection, you could use a custom routine. Here's one similar to that I use:

void readintline(unsigned int* array, int* size) {
    char buffer[101];
    size=0;
    char* in=buffer;
    unsigned int* out=array;
    fgets(buffer, 100, stdin);
    do {
        *out=0;
        while(*in>='0') {
            *out= *out* 10 + *in-'0';
            ++in;
        }
        if (*in)
            ++in; //skip whitespace
        ++out;
    } while(*in);
    size = out-array;
}

It will destroy your memory if there's more than 100 characters on a line, or more numbers than array can hold, but you won't get a faster routine to read in lines of unsigned ints.

On the other hand, if you want simple:

int main() {
    std::string tmp;
    while(std::getline(std::cin, tmp)) {
        std::vector<int> nums;
        std::stringstream ss(tmp);
        int ti;
        while(ss >> ti) 
            nums.push_back(ti);
        //do stuff with nums
    }
    return 0;
}
心头的小情儿 2024-12-17 04:11:10

我可能会写这样的代码:

// Warning: untested code.
std::vector<int> read_line_ints(std::istream &is) { 
    std::string temp;
    std::getline(is, temp);

    std::istringstream buffer(temp);
    int num;
    std::vector<int> ret;

    while (buffer>>num)
        ret.push_back(num);
    return ret;
}

I'd probably write the code something like this:

// Warning: untested code.
std::vector<int> read_line_ints(std::istream &is) { 
    std::string temp;
    std::getline(is, temp);

    std::istringstream buffer(temp);
    int num;
    std::vector<int> ret;

    while (buffer>>num)
        ret.push_back(num);
    return ret;
}
猫弦 2024-12-17 04:11:10

在 C++ 中,您可以使用 std::istringstream

std::string nums = "1 20 300 4000";
std::istringstream stream(nums);
int a, b, c, d;
stream >> a >> b >> c >> d;
assert(a == 1 && b == 20 && c == 300 && d == 4000);

如果您想从标准输入获取它,请执行相同的操作,但只需使用 std::cin

std::cin >> a >> b >> c >> d;

In C++, you can use std::istringstream.

std::string nums = "1 20 300 4000";
std::istringstream stream(nums);
int a, b, c, d;
stream >> a >> b >> c >> d;
assert(a == 1 && b == 20 && c == 300 && d == 4000);

If you want to get it from the standard input, then do the same, but just use std::cin

std::cin >> a >> b >> c >> d;
谜泪 2024-12-17 04:11:10

快速的解决方案是使用 scanf()< 阅读它们/a>

int array[1000];
int index = 0;

while ((index < 1000) && (scanf("%d", &tmp) == 1)) {
    array[index++] = tmp;
}

这仍然需要更多验证......

The quick solution is to read them with scanf()

int array[1000];
int index = 0;

while ((index < 1000) && (scanf("%d", &tmp) == 1)) {
    array[index++] = tmp;
}

This still needs a bit more validation ...

无需解释 2024-12-17 04:11:10

C++:

vector<int> ints;
while( !cin.eof() )
{
   int t;
   cin >> t;
   if ( !cin.eof() )
      ints.push_back(t);
}

替代方案(感谢 Shahbaz)

int t;
vector<int> ints;
while(cin >> t)
   ints.push_back(t);

C++:

vector<int> ints;
while( !cin.eof() )
{
   int t;
   cin >> t;
   if ( !cin.eof() )
      ints.push_back(t);
}

Alternative (thx to Shahbaz)

int t;
vector<int> ints;
while(cin >> t)
   ints.push_back(t);
风苍溪 2024-12-17 04:11:10

在 C++ 中,通过 stdin 读取由空格分隔的 N 个整数非常简单:

#include <iostream>

using namespace std;

const unsigned N = 5;

int main(void)
{
   int nums[N];

   for (unsigned i = 0; i < N; ++i)
      cin >> nums[i];

   cout << "Your numbers were:\n";

   for (unsigned i = 0; i < N; ++i)
      cout << nums[i] << " ";

   cout << "\n";

   return 0;
}

In C++ it's extremely simple to read N integers separated by whitespace via stdin:

#include <iostream>

using namespace std;

const unsigned N = 5;

int main(void)
{
   int nums[N];

   for (unsigned i = 0; i < N; ++i)
      cin >> nums[i];

   cout << "Your numbers were:\n";

   for (unsigned i = 0; i < N; ++i)
      cout << nums[i] << " ";

   cout << "\n";

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