我有两个函数可以拆分字符串并将标记添加到向量中:
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)
{...}
Run Code Online (Sandbox Code Playgroud)
它在电话中运行良好
std::string headers;
std::vector<std::string> rawHeaders;
Slice(headers.begin(), headers.end(), '\0', rawHeaders)
Run Code Online (Sandbox Code Playgroud)
并且不适用于const char*:
auto begin = headers.c_str();
auto end = begin + headers.size();
Slice(begin, end, '\0', rawHeaders);
Run Code Online (Sandbox Code Playgroud)
错误是(我将 MSVC2017 与 MSVC2013 工具链一起使用):
错误 C2780:“void Slice(InputIterator,InputIterator,InputIterator,InputIterator,ContainerType &)”:需要 5 个参数 - 提供 4 个
错误 C2893:无法专门化函数模板 'void Slice(InputIterator,InputIterator,InputIterator::value_type,ContainerType &) 使用以下模板参数:'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() );
Run Code Online (Sandbox Code Playgroud)
更新 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;
Run Code Online (Sandbox Code Playgroud)
现在它说:
错误 C3861:“find_if_not”:未找到标识符。请参阅使用 [_Ty=std 编译的函数模板实例化 'void StringUtils::Slice<char*,std::vectorstd::string,std::allocator<_Ty>>(InputIterator,InputIterator,char,ContainerType &)' 的参考::string, InputIterator=char *, ContainerType=std::vectorstd::string,std::allocator<std::string>
并且:
错误 C3861:“find_if”:未找到标识符
SFINAE 开始。签名
template < typename InputIterator, typename ContainerType >
void Slice(InputIterator begin,
InputIterator end,
typename InputIterator::value_type delimiter,
ContainerType& container)
Run Code Online (Sandbox Code Playgroud)
不是一个可能的候选者,因为::value_type在 a 中没有嵌套成员const char*。
你可能想要这个签名:
template < typename InputIterator, typename ContainerType >
void Slice(InputIterator begin,
InputIterator end,
typename std::iterator_traits<InputIterator>::value_type delimiter,
ContainerType& container)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
82 次 |
| 最近记录: |