gpt4 book ai didi

c++ - 自己的 vector 分配实现

转载 作者:行者123 更新时间:2023-12-02 09:53:57 25 4
gpt4 key购买 nike

我正在通过编写所有默认函数来实现类似于vector的STL。还有一个问题,我不明白为什么它为简单类型调用ragn版本的assign,并且没有默认值。
这是实现代码:
vector

void assign(size_t count, const T& value){ // Default version
void assign(size_t count, const T& value){
if(this->_size < count){
this->allocator.deallocate(this->arr, this->_capacity);
this->arr = this->allocator.allocate(count);

this->_capacity = count;
}
for(size_t i = 0; i < count; ++i)
this->arr[i] = value;

this->_size = count;
}

template<class InputIt>
void assign(InputIt first, InputIt last){ // Range version
size_t count = std::distance(first,last);
if(this->_size < count){
this->allocator.deallocate(this->arr, this->_capacity);
this->arr = this->allocator.allocate(count);

this->_capacity = count;
}
for(size_t i = 0; first != last; i++)
this->arr[i] = *first++;

this->_size = count;
}

主要代码:
 Vector<int> vec;
vec.assign(5,10);


输出:

/MyVector/MyVector.h: In instantiation of ‘void Vector<T, Allocator>::assign(InputIt, InputIt) [with InputIt = int; T = int; Allocator = std::allocator]’:
../MyVector/main.cpp:52:24: required from here
../MyVector/MyVector.h:99:45: error: no matching function for call to ‘distance(int&, int&)’
size_t count = std::distance(first,last);
~~~~~~~~~~~~~^~~~~~~~~~~~
In file included from /usr/include/c++/7/bits/stl_algobase.h:66:0,
from /usr/include/c++/7/bits/char_traits.h:39,
from /usr/include/c++/7/ios:40,
from /usr/include/c++/7/ostream:38,
from /usr/include/c++/7/iostream:39,
from ../MyVector/main.cpp:1:
/usr/include/c++/7/bits/stl_iterator_base_funcs.h:138:5: note: candidate: template<class _InputIterator> constexpr typename std::iterator_traits<_Iterator>::difference_type std::distance(_InputIterator, _InputIterator)
distance(_InputIterator __first, _InputIterator __last)
^~~~~~~~
/usr/include/c++/7/bits/stl_iterator_base_funcs.h:138:5: note: template argument deduction/substitution failed:
/usr/include/c++/7/bits/stl_iterator_base_funcs.h: In substitution of ‘template<class _InputIterator> constexpr typename std::iterator_traits<_Iterator>::difference_type std::distance(_InputIterator, _InputIterator) [with _InputIterator = int]’:
../MyVector/MyVector.h:99:45: required from ‘void Vector<T, Allocator>::assign(InputIt, InputIt) [with InputIt = int; T = int; Allocator = std::allocator]’
../MyVector/main.cpp:52:24: required from here
/usr/include/c++/7/bits/stl_iterator_base_funcs.h:138:5: error: no type named ‘difference_type’ in ‘struct std::iterator_traits<int>’
In file included from ../MyVector/main.cpp:2:0:
../MyVector/MyVector.h: In instantiation of ‘void Vector<T, Allocator>::assign(InputIt, InputIt) [with InputIt = int; T = int; Allocator = std::allocator]’:
../MyVector/main.cpp:52:24: required from here
../MyVector/MyVector.h:107:36: error: invalid type argument of unary ‘*’ (have ‘int’)
this->arr[i] = *first++;
^~~~~~~~
Makefile:725: recipe for target 'main.o' failed
make: *** [main.o] Error 1

我正在使用C++ 17

最佳答案

范围版本更适合vec.assign(5, 10);InputIt = int。您应该以某种方式禁用不代表输入迭代器的模板参数的重载。

让我们看看stdlibc++ implementation:

template<typename InputIt, typename = std::RequireInputIter<InputIt>>
void assign(InputIt first, InputIt last) {
M_assign_dispatch(first, last);
}

其中 RequireInputIter is
template<typename InputIt>
using RequireInputIter = typename enable_if<is_convertible<typename
iterator_traits<InputIt>::iterator_category, input_iterator_tag>::value>::type;

换句话说,对于推导类型 InputItiterator_traits<InputIt>::iterator_category类型应可转换为 input_iterator_tag。否则,由于 SFINAE,该 assign重载被静默排除在重载解决方案集中。

在C++ 17中,可以使用 RequireInputIter_t帮助器简化 _v:
template<typename InputIt>
using RequireInputIter = enable_if_t<is_convertible_v<typename
iterator_traits<InputIt>::iterator_category, input_iterator_tag>>;

还要注意,输入迭代器可用于遍历范围 only once。调用 std::distance(first, last)之后,所有后续遍历范围的尝试都是未定义的行为,除非 InputIt至少是一个正向迭代器。对于输入迭代器,您无法确定要预分配多少空间。

这就是 assign在内部使用标签分发技术的原因。经过一些简化后,它看起来像这样:
template<typename InputIt, typename = std::RequireInputIter<InputIt>>
void assign(InputIt first, InputIt last) {
M_assign_aux(first, last,
typename iterator_traits<InputIt>::iterator_category{});
}

有两个 M_assign_aux重载
template<typename InputIt>
void M_assign_aux(InputIt first, InputIt last, std::input_iterator_tag);

template<typename ForwardIt>
void M_assign_aux(ForwardIt first, ForwardIt last, std::forward_iterator_tag);

做作业。第一个仅用于输入迭代器,第二个-用于正向迭代器以及从其派生的迭代器,即双向和随机访问迭代器。

关于c++ - 自己的 vector 分配实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61889111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com