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

コンテナーを検索スペースとして使用したくない場合は、トークン イテレーターを手動で作成する必要があります。

#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 に答える
1

ブーストを使用しているため、次のことができます。

#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) を使用します (ここでは1 つの実装を示します)。おそらく標準ライブラリにも入っています。

更新: string_viewC++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

コンパイラ エラーが示すように、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 に答える