Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 自向量分配实现_C++_Vector_Iterator_C++17_Assign - Fatal编程技术网

C++ 自向量分配实现

C++ 自向量分配实现,c++,vector,iterator,c++17,assign,C++,Vector,Iterator,C++17,Assign,我正在用编写所有默认函数来实现类似stl的向量。还有一个问题,我不明白为什么它为简单类型调用ragne版本的assign,而不是默认值。 以下是实现代码: 向量h 主要代码: Vector<int> vec; vec.assign(5,10); 输出: /MyVector/MyVector.h: In instantiation of ‘void Vector<T, Allocator>::assign(InputIt, InputIt) [with Input

我正在用编写所有默认函数来实现类似stl的向量。还有一个问题,我不明白为什么它为简单类型调用ragne版本的assign,而不是默认值。 以下是实现代码: 向量h

主要代码:

 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,您可以使用cast调用特定函数。比如:
分配大小5,常数int&10

您可以使用cast调用特定函数。比如:
分配大小5,常数int&10

范围版本更适合vec.5,10;对于不表示输入迭代器的模板参数,应该以某种方式禁用该重载

让我们来看看:

换句话说,对于导出的类型InputIt,迭代器特征::迭代器类别类型应该转换为输入迭代器标记。否则,该赋值重载将从重载解析集中自动排除,原因是

在C++17中,可以使用_t和_v助手简化RequireInputer:

有两个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);

去做作业。第一个仅用于输入迭代器,第二个用于前向迭代器和从中派生的迭代器,即双向和随机访问迭代器。

范围版本更适合于vec.assign5,10;对于不表示输入迭代器的模板参数,应该以某种方式禁用该重载

让我们来看看:

换句话说,对于导出的类型InputIt,迭代器特征::迭代器类别类型应该转换为输入迭代器标记。否则,该赋值重载将从重载解析集中自动排除,原因是

在C++17中,可以使用_t和_v助手简化RequireInputer:

有两个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);

去做作业。第一个仅用于输入迭代器,第二个用于前向迭代器和从中派生的迭代器,即双向和随机访问迭代器。

请参阅上的CPP参考。第二个重载特别提到,当向量包含整数类型时,为了避免选择这个重载,我们做了一些事情。如果向量类型为整型,则模板重载将选择在您自己的模板重载之上。您需要提供一个处理它的实现。在C++11中,这是使用SFINAE完成的,但是我在模板方面没有足够的能力回答您的问题,对不起。为什么要调用默认版本?int不是size\u t,因此模板版本更适合。只需尝试void fsize_t、const int&;模板空隙fT,T;然后调用f1,2;。它将调用模板化的f。请参阅上的CPP参考。第二个重载特别提到,当向量包含整数类型时,为了避免选择这个重载,我们做了一些事情。如果向量类型为整型,则模板重载将选择在您自己的模板重载之上。您需要提供一个处理它的实现。在C++11中,这是使用SFINAE完成的,但是我在模板方面没有足够的能力回答您的问题,对不起。为什么要调用默认版本?int不是size\u t,因此模板版本更适合。只需尝试void fsize_t、const int&;模板空隙fT,T;然后调用f1,2;。它将调用模板f。非常感谢。这正是我所需要的:@RadicalEdward,更新了iterator类别的标记分派。非常感谢。这正是我所需要的:@RadicalEdward,更新为iterator类别的标记分派。
template<typename InputIt>
using RequireInputIter = typename enable_if<is_convertible<typename
    iterator_traits<InputIt>::iterator_category, input_iterator_tag>::value>::type;
template<typename InputIt>
using RequireInputIter = enable_if_t<is_convertible_v<typename
    iterator_traits<InputIt>::iterator_category, input_iterator_tag>>;
template<typename InputIt, typename = std::RequireInputIter<InputIt>>
void assign(InputIt first, InputIt last) {
    M_assign_aux(first, last, 
        typename iterator_traits<InputIt>::iterator_category{});
}
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);