gpt4 book ai didi

c++ - 如何使函数能够接受原始指针作为迭代器?

转载 作者:行者123 更新时间:2023-12-05 09:32:59 25 4
gpt4 key购买 nike

我有两个函数可以拆分字符串并将标记添加到 vector 中:

template < typename InputIterator, typename ContainerType >
void Slice(InputIterator begin,
InputIterator end,
typename InputIterator::value_type delimiter,
ContainerType& container)
{
using CharType = InputIterator::value_type;
InputIterator right = begin;
while (true) {
begin = find_if_not(right, end, [ delimiter ](CharType c) { return c == delimiter; });
if (begin == end) {
break;
} else {
right = find_if(begin + 1, end, [ delimiter ](CharType c) { return c == delimiter; });
container.push_back(std::basic_string< CharType >(begin, right));
}
}
}

template < typename InputIterator, typename ContainerType >
void Slice(InputIterator begin,
InputIterator end,
InputIterator delimitersBegin,
InputIterator delimitersEnd,
ContainerType& container)
{...}

它在像这样的调用中效果很好

std::string headers;
std::vector<std::string> rawHeaders;
Slice(headers.begin(), headers.end(), '\0', rawHeaders)

并且不适用于const char*:

auto begin = headers.c_str();
auto end = begin + headers.size();
Slice(begin, end, '\0', rawHeaders);

错误是(我使用 MSVC2017 和 MSVC2013 工具链):

error C2780: 'void Slice(InputIterator,InputIterator,InputIterator,InputIterator,ContainerType &)' : expects 5 arguments - 4 provided

error C2893: Failed to specialize function template 'void Slice(InputIterator,InputIterator,InputIterator::value_type,ContainerType &)With the following template arguments:'InputIterator=const char *''ContainerType=std::vectorstd::string,std::allocator<_Ty>'

更新:添加了函数体。目前最好不要使用 std::span 等附加功能。我希望有以下字符串构造函数:

template< class InputIt >
basic_string( InputIt first, InputIt last, const Allocator& alloc = Allocator() );

更新 2:仍然不起作用,但出于不同的原因,我尝试更改分隔符类型:

 template < typename InputIterator, typename ContainerType >
void Slice(
InputIterator begin,
InputIterator end,
typename std::iterator_traits< InputIterator >::value_type delimiter,
//typename InputIterator::value_type delimiter,
ContainerType& container)
{
using CharType = typename std::iterator_traits< InputIterator >::value_type;

现在它说:

error C3861: 'find_if_not': identifier not found. see reference to function template instantiation 'void StringUtils::Slice<char*,std::vectorstd::string,std::allocator<_Ty>>(InputIterator,InputIterator,char,ContainerType &)' being compiled with [_Ty=std::string, InputIterator=char *, ContainerType=std::vectorstd::string,std::allocator<std::string>

还有:

error C3861: 'find_if': identifier not found

最佳答案

SFINAE 开始。签名

template < typename InputIterator, typename ContainerType >
void Slice(InputIterator begin,
InputIterator end,
typename InputIterator::value_type delimiter,
ContainerType& container)

不是可能的候选者,因为 const char* 中没有嵌套成员 ::value_type

您可能需要这个签名:

template < typename InputIterator, typename ContainerType >
void Slice(InputIterator begin,
InputIterator end,
typename std::iterator_traits<InputIterator>::value_type delimiter,
ContainerType& container)

关于c++ - 如何使函数能够接受原始指针作为迭代器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67602373/

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