How to read n integers from standard input in C++?

对着背影说爱祢 提交于 2019-11-30 00:03:09

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;
}

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);
  }
}
Jerry Coffin

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;
}

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;

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 ...

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);

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;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!