Is there a similar way to read integer pairs from stdin in vector <pair <int, int >> in C ++

I’m wondering if there’s a way like "

copy(istream_iterator<int>(cin), istream_iterator<int>(),back_inserter(v));

to copy pairs intin vector<pair<int,int> >, when input is given in pairs in the order they appear?

Thank.

+5
source share
2 answers

You can do this, but first you need to write your own operator >>for the pair class. This statement is the whole secret of the aforementioned call. Its actual implementation depends on the format of your int pairs.

+3
source

boost::zip_iterator.

copy(boost::make_zip_iterator(
         boost::make_tuple(istream_iterator<int>(cin),
                           istream_iterator<int>(cin)),
     boost::make_zip_iterator(
         boost::make_tuple(istream_iterator<int>(),
                           istream_iterator<int>()),
     back_inserter(v));
+4
source

All Articles