c++allocatoristream-iterator

What's with allocator in vector?


vector<int> data(istream_iterator<int>(cin),
istream_iterator<int>{});   cout<<"Size is : " << data.size() << endl; //compile success

vector<int> data1(istream_iterator<int>(cin),
std::allocator<int>{});   cout<<"Size is : " << data1.size() << endl; //compile failure

 error: no matching function for call to ‘std::vector<int>::vector(std::istream_iterator<int>, std::allocator<int>)’    vector<int> data1(istream_iterator<int>(cin), std::allocator<int>{});

Why is the first statement fine but second? Doesn't the vector take int type allocator in this case? I am experimenting with allocators.


Solution

  • Why is the first statement fine

    Because vector has a constructor that accepts two iterators (and an allocator with default argument). Those iterators represent beginning and end of an input range.

    but second [is not]?

    Because vector doesn't have a constructor that accepts a single iterator and an allocator.