向量中的分配器有什么用?

InQ*_*ive -1 c++ istream-iterator allocator

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
Run Code Online (Sandbox Code Playgroud)
 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>{});
Run Code Online (Sandbox Code Playgroud)

为什么第一个语句很好,但第二个呢?int在这种情况下,向量不采用类型分配器吗?我正在试验allocators。

eer*_*ika 5

为什么第一个语句没问题

因为 vector 有一个构造函数,它接受两个迭代器(和一个带有默认参数的分配器)。这些迭代器代表输入范围的开始和结束。

但第二[不是]?

因为 vector 没有接受单个迭代器和分配器的构造函数。

  • @InQusitive 因为单个迭代器不能表示一个范围。构造函数如何知道要从迭代器中读取多少个元素? (2认同)