0

So I was thinking of simplifying this snippet

#include <bits/stdc++.h>

using namespace std;

int n;
vector<int> A;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n;
    for (int i = 0; i < n; i++) {
        int tmp;
        cin >> tmp;
        A.push_back(tmp);
    }
}

And because I have read about inserters and back_inserters recently I thought of using them right away and this is what I came up with

#include <bits/stdc++.h>

using namespace std;

int n;
vector<int> A;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n;
    for (int i = 0; i < n; i++) cin >> *back_inserter(A);
}

But for some reason the compiler spits a gigantic error message which I can't fit here so here is the first sentence only as I think it is the most relevant.

error: no match for 'operator>>' (operand types are 'std::istream' {aka 'std::basic_istream<char>'} and 'std::back_insert_iterator<std::vector<int> >')

Thanks for your help!

NOTE: before anybody comments on the use of global variables and the using namespace line, this code was intended for use only in competitive programming.

6
  • this code was intended for use only in competitive programming. -- This is not a competitive coding site. If your code is unreadable or sloppy, be prepared to have persons comment on it. Nov 30, 2020 at 20:27
  • 1
  • 1
  • If competitive programming encourages or tolerates sloppy and error-prone practices, I would question the value of competitive programming. Dec 1, 2020 at 17:49
  • To keep it short and simple, we as competitive programmers know the problems associated with global variables, include <bits..., etc. and we code accordingly. I believe this is better than just avoiding them without a proper understanding of why we do in software development. Anyhow, we have a multitude of reasons, here are a few I am aware of: 1- we don't write multi-file programs 2- each person/team are the only people who will maintain the code 3- we will maintain the code for just a few hours (the contest duration) 4- our codes are generally short 5- coding time is critical Dec 2, 2020 at 17:22

2 Answers 2

3

In this case where you are just reading space separated values from the input stream until it ends, you can use std::copy to get the values from the stream. That would look like

std::copy(std::istream_iterator<int>(cin),
          std::istream_iterator<int>(),
          std::back_inserter(vector_name));
3
  • Thanks very much, but for the sake of simplicity I have removed the rest of the code, but more cin statement where present after the snippet above, is there a way to make the above code work? Thanks again for your fast reply! Nov 30, 2020 at 20:39
  • @SalmanElgamal I know of no way to use cin >> something to add to a vector where something is something from the standard library. You'd have to write your own adapter to do that. Nov 30, 2020 at 20:41
  • ok, but I fear I can't do that in the midst of the competition environment as prewritten code is not allowed, anyways thanks a ton for your help. Nov 30, 2020 at 20:46
0

You could read into a temporary variable.

for (int k, i = 0; i < n; i++) cin >> k, back_inserter(A) = k;
# or nicer:
for (int i = 0; i < n; i++) {
     int k;
     cin >> k;
     back_inserter(A) = k;
}

You could provide the missing overload and read into temporary variable in it:

template<typename T>
std::istream& operator>>(std::istream& is, std::back_insert_iterator<std::vector<T>> obj) {
    T tmp;
    is >> tmp;
    obj = tmp;
    return is;
}

int main() {
    for (int i = 0; i < n; i++) cin >> back_inserter(A);
}
4
  • This looks pretty good and concise, thanks a lot for your help, but I really don't understand why this code works but mine doesn't, any idea? Nov 30, 2020 at 20:52
  • ? Because there is no std::istream::operator>>(std::back_insert_operator<std::vector<int>>). There are only that many std::istream::operator>>.
    – KamilCuk
    Nov 30, 2020 at 22:52
  • Yeah I see, but there is one for a void* pointer right, I thought it would treat it the same as a regular pointer, can that be achieved if I cast the back_inserter somehow to a pointer? Dec 1, 2020 at 9:06
  • That makes no sense. No. The operator>>(void*) prints the value of the pointer. And it makes no sense to "cast an object somehow to a pointer".
    – KamilCuk
    Dec 1, 2020 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.