1

我正在执行以下操作:

using namespace boost;
const char* line = // ...
size_t line_length = // ...
// ...
tokenizer<escaped_list_separator<char> > line_tokenizer(
    line, line + line_length,
    escaped_list_separator<char>('\\', ',', '\"'));

期望使用boost::tokenizer构造函数

tokenizer(Iterator first, Iterator last,
          const TokenizerFunc& f = TokenizerFunc()) 
  : first_(first), last_(last), f_(f) { }

但 GCC 4.9.3 给了我:

no known conversion for argument 1 from ‘const char*’ to ‘__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >’

现在,我已经看到了几个相关的 问题,其中的答案忘记了#include <algorithm>——但我已经把它包括在内了。是否还有其他缺失的包含,或者是另一个问题?

4

3 回答 3

1

正如编译器错误所说,没有办法从 const char* 构建迭代器。您可以使用 std::string 修复它:

std::string line = "some string";
// ...
tokenizer<escaped_list_separator<char> > line_tokenizer(
    line.begin(), line.end(),
    escaped_list_separator<char>('\\', ',', '\"'));
于 2016-04-12T14:03:27.290 回答
1

由于您使用的是 boost,因此您可以执行以下操作:

#include <boost/utility/string_ref.hpp>
// ...
const boost::string_ref line_(line, line_length);
tokenizer<escaped_list_separator<char> > line_tokenizer(
    line_, escaped_list_separator<char>('\\', ',', '\"'));

这似乎有效。在此处阅读有关string_ref其他实用程序的更多信息。

当然,如果您有指南支持库的实现,请从那里使用string_span(aka )(这里是一个实现)。它甚至可能会进入标准库。string_view

更新:string_view在 C++17 的 C++ 标准中。现在你可以写:

#include <string_view>
// ...
std::string_view line_ { line, line_length };
tokenizer<escaped_list_separator<char> > line_tokenizer(
    line_, escaped_list_separator<char>('\\', ',', '\"'));
于 2016-04-12T14:35:38.580 回答
1

如果您不想将容器用作搜索空间,则必须手动构建令牌迭代器

#include <iostream>
#include <string>
#include <boost/tokenizer.hpp>

int main()
{
    const char xx[] = "a,b,c,d,e,f,g";
    auto line = xx;
    size_t line_length = strlen(line);

    using namespace boost;

    auto f = escaped_list_separator<char>('\\', ',', '\"');
    auto beg = make_token_iterator<char>(line ,line + line_length,f);
    auto end = make_token_iterator<char>(line + line_length,line + line_length,f);
    // The above statement could also have been what is below
    // Iter end;
    for(;beg!=end;++beg){
        std::cout << *beg << "\n";
    }
    return 0;
}
于 2016-04-12T14:12:39.923 回答