gpt4 book ai didi

c++ - 为 regex_iterator 使用自定义迭代器的问题

转载 作者:行者123 更新时间:2023-11-30 04:17:29 25 4
gpt4 key购买 nike

我想对要跳过文本中某些字符的文本进行正则表达式搜索。此处提出的原始问题:Regular Expression library that maintains state, takes input character by character and returns true whenever match is found为此,我制作了一个自定义迭代器,并试图将这些迭代器传递给 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而不是自己写东西。

关于c++ - 为 regex_iterator 使用自定义迭代器的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17069327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com