簡體   English   中英

將自定義迭代器用於regex_iterator的問題

[英]Issues with using Custom iterators for regex_iterator

我想對要跳過文本中某些字符的文本進行正則表達式搜索。 最初的問題在這里: 維護狀態的正則表達式庫,一個字符一個字符地輸入,並在找到匹配項時返回true 。為此,我制作了一個自定義迭代器,並試圖將這些迭代器傳遞給regex_iterator。 但是我遇到以下錯誤:


regex(2775): error C2512: 'fixed_array::iterator' : no appropriate default constructor available
          with
          [
              T=char
          ]
regex(2773) : while compiling class template member function 'std::tr1::regex_iterator::regex_iterator(void)'
          with
          [
              _BidIt=fixed_array::iterator
          ]
iterator.cpp(168) : see reference to class template instantiation 'std::tr1::regex_iterator' being compiled
          with
          [
              _BidIt=fixed_array::iterator
          ]
regex(2775): error C2512: 'fixed_array::iterator' : no appropriate default constructor available
          with
          [
              T=char
          ]

這是我的代碼:



    template 
    class fixed_array
    {
        public:

        typedef int size_type;

        class iterator
        {
            public:
            typedef iterator self_type;
            typedef T value_type;
            typedef T& reference;
            typedef T* pointer;
            typedef std::bidirectional_iterator_tag iterator_category;
            typedef int difference_type;
            iterator(pointer ptr) : ptr_(ptr) { }

            self_type operator++() /*skip character 'a'*/
            {
                self_type i = *this;
                ptr_++;
                if(*ptr_ == 'a')
                ptr_++;
                return i;
            }

            self_type operator++(int junk)
            {
                ptr_++;
                if(*ptr_ == 'a')
                ptr_++;
                return *this;
            }

            reference operator*()
            {
                return *ptr_;
            }

            pointer operator->()
            {
                return ptr_;
            }

            bool operator==(const self_type& rhs)
            {
                return ptr_ == rhs.ptr_;
            }

            bool operator!=(const self_type& rhs)
            {
                return ptr_ != rhs.ptr_;
            }
            private:
            pointer ptr_;
        };

        class const_iterator
        {
            public:
            typedef const_iterator self_type;
            typedef T value_type;
            typedef T& reference;
            typedef T* pointer;
            typedef int difference_type;
            typedef std::bidirectional_iterator_tag iterator_category;
            const_iterator(pointer ptr) : ptr_(ptr) { }
            self_type operator++() { self_type i = *this; ptr_++; return i; }
            self_type operator++(int junk) { ptr_++; return *this; }
            const reference operator*() { return *ptr_; }
            const pointer operator->() { return ptr_; }
            bool operator==(const self_type& rhs) { return ptr_ == rhs.ptr_; }
            bool operator!=(const self_type& rhs) { return ptr_ != rhs.ptr_; }
            private:
            pointer ptr_;
        };


        fixed_array(size_type size) : size_(size) {
            data_ = new T[size_];
        }

        size_type size() const { return size_; }

        T& operator[](size_type index)
        {
            assert(index < size_);
            return data_[index];
        }

        const T& operator[](size_type index) const
        {
            assert(index < size_);
            return data_[index];
        }

        iterator begin()
        {
            return iterator(data_);
        }

        iterator end()
        {
            return iterator(data_ + size_);
        }

        const_iterator begin() const
        {
            return const_iterator(data_);
        }

        const_iterator end() const
        {
            return const_iterator(data_ + size_);
        }

        private:
        T* data_;
        size_type size_;
    };

    using namespace std;

    int main()
    {
        fixed_array<char> point3d(50);

        //initialize the array with some string.
        regex e ("a.*ea");

        fixed_array<char>::iterator beg = point3d.begin();
        fixed_array<char>::iterator end = point3d.end();

        regex_iterator<fixed_array<char>::iterator> rit ( beg, end, e );
        regex_iterator<fixed_array<char>::iterator> rend;

        while (rit!=rend) {
            cout << rit->str() << std::endl;
            ++rit;
        }
        return 0;
    }

篩選器迭代器很難正確處理。 同樣,您的const和非const迭代器也不一致。

我建議您使用Boost filter_iterator而不是編寫自己的東西。

好的,最終,借助塞巴斯蒂安(Sebastian)的回答以及一些研究,我得以實現我的目標。

我使用了在以下頁面上給出的過濾器迭代器代碼: http : //blogs.msdn.com/b/vcblog/archive/2010/08/09/the-filterator.aspx

除了上面的迭代器,這是我寫的:

struct is_current_charT {
    bool operator()(char s) const {
        return s != 't';
    }
};

int main() {
    using namespace std;

    string str = "test1 ,,ktest2,, ktest3,, ktest0,,";
    regex e ("kes[0-9]");

    auto fit = make_filterator(str.begin(), str.end(),
        is_current_charT());

    filterator<string::iterator,is_current_charT> cdogs(fit);

    regex_iterator<filterator<string::iterator,is_current_charT>> rit ( cdogs, cdogs.end(), e );
    regex_iterator<filterator<string::iterator,is_current_charT>> rend;

    while (rit!=rend) {
        cout << rit->str() << std::endl;
        ++rit;
    }
}

輸出:

kes2
kes3
kes0

現在可以輕松對其進行修改,以編寫更有意義的過濾功能。

謝謝艾哈邁德和塞巴斯蒂安!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM