I've got two vectors, and I want to fill both of them from the cin
. My normal approach here is to create two for loops, but today I've tried to do it like this:
vector<long long> a(n);
vector<long long> b(n);
for(auto& vec : {a, b}) {
for(int i = 0; i < n; ++i) {
cin >> vec[i];
}
}
With this solution I get a compilation error, telling me that my vec[i]
is a vector, while I expect it to be a long long int
:
error: no match for ‘operator>>’ (operand types are ‘std::istream’ {aka ‘std::basic_istream<char>’} and ‘std::vector<long long int>’)
I thought that it is some magic which I can't understand and tried to dereference vec
, but that didn't help either:
error: no match for ‘operator*’ (operand type is ‘const std::vector<long long int>’)
17 | cin >> (*vec)[i];
| ^~~~
What am I doing wrong here? Why isn't auto&
giving me a pointer, as I expect? How do I make this code work?