字符串分割

c++处理数据时用到的,来自于chrome代码中的StringTokenizerT模板类

//std::string test = " hello world";
//base::StringTokenizer str(test, " ");
//while (str.GetNext())
//{
//  std::cout << str.token() << std::endl;
//}

template <typename str, typename const_iterator>
class  StringTokenizerT
{
public:
    StringTokenizerT(const str& string, const str& delims)
    {
        Init(string.begin(), string.end(), delims);
    }

    StringTokenizerT(const_iterator string_begin, const_iterator string_end, const str& delims)
    {
        Init(string_begin, string_end, delims);
    }

    bool GetNext()
    {
        for (;;)
        {
            token_begin_ = token_end_;
            if (token_begin_ == end_)
            {
                return false;
            }
            ++token_end_;
            if (delims_.find(*token_begin_)==str::npos)
            {
                break;
            }
        }

        while (token_end_ != end_ && delims_.find(*token_end_) == str::npos)
        {
            ++token_end_;
        }
        return true;
    }

    str token() const
    {
        return str(token_begin_, token_end_);
    }

    const_iterator token_start()
    {
        return token_begin_;
    }

    const_iterator token_end()
    {
        return token_end_;
    }

private:
    void Init(const_iterator string_begin, const_iterator string_end, const str& delims)
    {
        start_ = string_begin;
        end_ = string_end;
        token_begin_ = string_begin;
        token_end_ = string_begin;
        delims_ = delims;
    }

private:
    const_iterator start_;
    const_iterator end_;
    const_iterator token_begin_;
    const_iterator token_end_;
    str delims_;

};

typedef StringTokenizerT<std::string, std::string::const_iterator>  StringTokenizer;
typedef StringTokenizerT<std::wstring, std::wstring::const_iterator>  WStringTokenizer;

你可能感兴趣的:(字符串分割)