Input vector / more variables in one line in c++

Правка en2, от IvanSimic, 2026-07-05 12:59:10

Hello Codeforcers,

When I switched from Python to C++, I got a problem while inputting. I was stuck with it for 10 days until I realised how easy it is. I just want to write it here for others if they also get stuck here.

In competitive programming, inputting more variables in one line is quite common.

In Python, if you want to enter two variables in a single line, you have to do this:

a, b = input().split()

While in c++, you can simply use 2 cins like your inputting in 2 lines. The way you do it (in line, spaced) doesn't matter.

string a, b;
cin >> a;
cin >> b;

or

string a, b;
cin >> a >> b;

This way you can input a vector of size n in one line:

vector<int> v(n);

for(int i = 0; i < n; i++) {
    cin >> v[i];
}
Теги c++, input

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en2 Английский IvanSimic 2026-07-05 12:59:10 18
en1 Английский IvanSimic 2026-07-05 12:58:03 872 Initial revision (published)