Input vector / more charactervariables in one line in c++
Difference between en1 and en2, changed 18 character(s)
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];↵
}↵
~~~~~↵






History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English IvanSimic 2026-07-05 12:59:10 18
en1 English IvanSimic 2026-07-05 12:58:03 872 Initial revision (published)