我在玩boost :: tokenizer,但是我意識到它不支持rbegin()和rend()。我想問一下如何將這兩個函數添加到現有類中?如何爲boost :: tokenizer實現tokenizer.rbegin()和rend()?
這是從提升網站:
#include <iostream>
#include <string>
#include <boost/tokenizer.hpp>
using namespace std;
using namespace boost;
int main() {
string str("12/12/1986");
typedef boost::tokenizer<boost::char_separator<char>> tokenizer;
boost::char_separator<char> sep("/");
tokenizer tokens(str, sep);
cout << *tokens.begin() << endl;
// cout << *tokens.rbegin() << endl; How could I implement this?
return 0;
}
更新
這是我目前的進展。我首先導航到標記生成器的src:
//===========================================================================
// A container-view of a tokenized "sequence"
template <
typename TokenizerFunc = char_delimiters_separator<char>,
typename Iterator = std::string::const_iterator,
typename Type = std::string
>
class tokenizer {
private:
typedef token_iterator_generator<TokenizerFunc,Iterator,Type> TGen;
// It seems that MSVC does not like the unqualified use of iterator,
// Thus we use iter internally when it is used unqualified and
// the users of this class will always qualify iterator.
typedef typename TGen::type iter;
public:
typedef iter iterator;
typedef iter const_iterator;
typedef Type value_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef value_type* pointer;
typedef const pointer const_pointer;
typedef void size_type;
typedef void difference_type;
tokenizer(Iterator first, Iterator last,
const TokenizerFunc& f = TokenizerFunc())
: first_(first), last_(last), f_(f) { }
template <typename Container>
tokenizer(const Container& c)
: first_(c.begin()), last_(c.end()), f_() { }
template <typename Container>
tokenizer(const Container& c,const TokenizerFunc& f)
: first_(c.begin()), last_(c.end()), f_(f) { }
void assign(Iterator first, Iterator last){
first_ = first;
last_ = last;
}
void assign(Iterator first, Iterator last, const TokenizerFunc& f){
assign(first,last);
f_ = f;
}
template <typename Container>
void assign(const Container& c){
assign(c.begin(),c.end());
}
template <typename Container>
void assign(const Container& c, const TokenizerFunc& f){
assign(c.begin(),c.end(),f);
}
iter begin() const { return iter(f_,first_,last_); }
iter end() const { return iter(f_,last_,last_); }
private:
Iterator first_;
Iterator last_;
TokenizerFunc f_;
};
然後我集中在兩種方法:
iter begin() const { return iter(f_,first_,last_); }
iter end() const { return iter(f_,last_,last_); }
因爲它返回類型ITER的一個構造(F_,first_,last_),I然後移動到這個班的來源。而且它實際上是:
typedef typename TGen::type iter;
它是token_iterator_generator類。而這一類的實現是:
template <
class TokenizerFunc = char_delimiters_separator<char>,
class Iterator = std::string::const_iterator,
class Type = std::string
>
class token_iterator_generator {
private:
public:
typedef token_iterator<TokenizerFunc,Iterator,Type> type;
};
所以現在我想通了,tokenizer類的迭代器實際上是token_iterator <>。而token_iterator的實現真的把我嚇壞了:
template <class TokenizerFunc, class Iterator, class Type>
class token_iterator
: public iterator_facade<
token_iterator<TokenizerFunc, Iterator, Type>
, Type
, typename detail::minimum_category<
forward_traversal_tag
, typename iterator_traversal<Iterator>::type
>::type
, const Type&
>
{
friend class iterator_core_access;
TokenizerFunc f_;
Iterator begin_;
Iterator end_;
bool valid_;
Type tok_;
void increment(){
BOOST_ASSERT(valid_);
valid_ = f_(begin_,end_,tok_);
}
const Type& dereference() const {
BOOST_ASSERT(valid_);
return tok_;
}
template<class Other>
bool equal(const Other& a) const{
return (a.valid_ && valid_)
?((a.begin_==begin_) && (a.end_ == end_))
:(a.valid_==valid_);
}
void initialize(){
if(valid_) return;
f_.reset();
valid_ = (begin_ != end_)?
f_(begin_,end_,tok_):false;
}
public:
token_iterator():begin_(),end_(),valid_(false),tok_() { }
token_iterator(TokenizerFunc f, Iterator begin, Iterator e = Iterator())
: f_(f),begin_(begin),end_(e),valid_(false),tok_(){ initialize(); }
token_iterator(Iterator begin, Iterator e = Iterator())
: f_(),begin_(begin),end_(e),valid_(false),tok_() {initialize();}
template<class OtherIter>
token_iterator(
token_iterator<TokenizerFunc, OtherIter,Type> const& t
, typename enable_if_convertible<OtherIter, Iterator>::type* = 0)
: f_(t.tokenizer_function()),begin_(t.base())
,end_(t.end()),valid_(!t.at_end()),tok_(t.current_token()) {}
Iterator base()const{return begin_;}
Iterator end()const{return end_;};
TokenizerFunc tokenizer_function()const{return f_;}
Type current_token()const{return tok_;}
bool at_end()const{return !valid_;}
};
這個類是非常複雜的,而且我在這裏失去了:(繼承部分來自iterator_facade <>太複雜 任何想法,我應該去下!?
謝謝,