0

For solving problems on Leetcode, Kickstart or other competitive competitions, we need to take input of multiple integers in a single line and store them in an array or vector, like

Input : 5 9 2 5 1 0

int arr[6];
for (int i = 0; i < 6; i++)
    cin >> arr[i];

or

vector<int> input_vec;
int x;

for (int i = 0; i < 6; i++) {
     cin >> x;
     input_vec.push_back(x);
}

This works, but also contributes to the execution time drastically, sometimes 50% of the execution time goes into taking input, in Python3 it's a one-line code.

input_list = list(int(x) for x in (input().split()))

But, couldn't find a solution in C++.

Is there a better way to do it in c++?

Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
Shashank Prasad
  • 474
  • 8
  • 11
  • 2
    Side note: Just because it is one line in python does not mean there is not a similar amount of machine code generated for each. – 001 Jul 24 '20 at 18:28
  • 3
    There are many better ways to do this in C++. The best way to learn about them is to [read a good C++ textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), instead of these pointless online competition sites that offer little of value in terms of learning valuable C++ skills. All they seem to succeed in is teaching bad programming practices, which you don't want to have. – Sam Varshavchik Jul 24 '20 at 18:29
  • Unrelated: `int arr[6]; for(int& v : arr) cin >> v;` – Ted Lyngmo Jul 24 '20 at 18:31
  • Regarding the execution time - read about [vector](https://en.cppreference.com/w/cpp/container/vector), if you know the amount of items you need to populate you can pre-allocate it, using `reserve` . – joepol Jul 24 '20 at 18:34
  • “50% of the execution time” is a meaningless measure. The important thing is whether the program as a whole is fast enough. – molbdnilo Jul 24 '20 at 18:34
  • Suspicion: Did you compile your program using the optimizing options to get the highest performance? – Ted Lyngmo Jul 24 '20 at 18:34
  • 1
    *Python3 it's a one-line code.* But how long does it take to execute that one line? Don't fall into the trap of thinking less code is always faster. – user4581301 Jul 24 '20 at 18:59
  • 1
    Another good way to speed up a program is realizing you don't always have to store data before processing it. Often you can fire an input directly into a computation in progress. – user4581301 Jul 24 '20 at 19:01
  • Another fun approach is to construct a `vector` out of [`std::istream_iterator`](https://en.cppreference.com/w/cpp/iterator/istream_iterator)s. – user4581301 Jul 24 '20 at 19:18
  • I've done my share of the `Leetcode` problems while preparing for the interviews; I doubt that reading an input data would measurably contribute to the execution time. – Vlad Feinstein Jul 24 '20 at 23:06
  • @VladFeinstein, in Kickstart it did, a logic using c++ showed Time limit exceeded, I tried the same logic with python and used the .split() method for input, it was expected – Shashank Prasad Jul 25 '20 at 17:33

2 Answers2

2

Take the help of std::istringstream:

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

int main(void) {
    std::string line;
    std::vector<int> numbers;
    int temp;

    std::cout << "Enter some numbers: ";
    std::getline(std::cin, line);

    std::istringstream ss(line);

    while (ss >> temp)
        numbers.push_back(temp);
    
    for (size_t i = 0, len = numbers.size(); i < len; i++)
        std::cout << numbers[i] << ' ';

    return 0;
}
Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
-1

How to take multiple integers in the same line as an input and store them in an array or vector in c++?

Like this:

int arr[6]; for(int i=0;i<6;i++){ cin>>arr[i]; }
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 1
    It takes the same thing, just the code looks smaller. – Shashank Prasad Jul 24 '20 at 19:00
  • 1
    @ShashankPrasad I believe that's exactly the point eerorika is trying to make. Burried deep inside the guts of `input_list = list(int(x) for x in (input().split()))` Is going to be allocation of a container, a loop, and in that loop an read into the container. This is unavoidable. Python simply expresses the behaviour in a slightly smaller amount of characters. – user4581301 Jul 24 '20 at 19:06
  • 1
    @ShashankPrasad It's in the same line. It's what you asked for. – eerorika Jul 24 '20 at 19:08