我正在尝试用C++98实现std::vector。
我参考了https://www.cplusplus.com/reference/vector/vector/vector/。
所以,在构造函数中,我按照以下方式编写了vector。
explicit vector (size_type n, const value_type& val = value_type(),
const allocator_type& alloc = allocator_type())
{
...
}
template <class InputIterator>
vector (InputIterator first, InputIterator last,
const allocator_type& alloc = allocator_type())
{
...
}
然而,当我在主函数中测试该向量时,它并没有按照我所期望的工作。
int main(void)
{
vector<int> vec(3, 100);
}
我想调用 explicit vector (size_type n, const value_type& val = value_type(), const allocator_type& alloc = allocator_type())
,但却调用了带有迭代器的构造函数。
所以,我的问题是:
为什么调用了带有迭代器的构造函数?
这是因为“explicit”吗?在主程序中应该使用 'size_t' 来调用带有 'val' 的构造函数吗?
还是有一种方法可以检查迭代器?
很抱歉打扰您,但我真的不知道为什么会发生这种情况...