Consider the following code piece:
...
int N,var;
vector<int> nums;
cin >> N;
while (N--)
{
cin >> var;
nums.push_back(var);
}
...
Is it possible to do this without using an auxillary variable, in this case var?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Assuming you have already read the initial
N, there is a nice trick usingistream_iterator:The
back_inserterobject turns itself into an iterator that adds elements to the vector at the end. Iterator streams can be parameterized by the type of the elements read, and, if no parameter given, signals the end of input.