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
现在可以很容易地修改,以编写更有意义的过滤函数。
谢谢Ahmed,谢谢Sebastian!
相关文章:
- ESP8266单片机矢量迭代器的C++问题
- 成员函数中的迭代器出现问题
- 迭代器的指针操作问题
- 为什么在这个C++问题中使用const_iterator而不仅仅是迭代器?
- std::filesystem::directory_迭代器链接器问题(C++17)
- 使用 find() 通过 std::set 的迭代器将不起作用。出了什么问题?
- 无法取消引用超出范围的向量迭代器 - 有什么问题?
- 调用模板函数的问题"No matching function for call"参数:迭代器、对象函数
- 迭代器的问题
- 无法理解迭代器的问题?
- 对于循环迭代器问题 c++
- 将MAP与一对用作C 迭代器问题中的键
- C++字符串的迭代器问题
- 模板变量作为unordered_map键,迭代器问题
- 映射迭代器问题.包括代码
- 链接列表帮助,迭代器问题
- 仅visual studio 2010中存在可延迟迭代器问题
- 向列表添加项和迭代器问题
- C++ 提升::p tr_vector<S>::迭代器问题
- C++删除字符串上的标点符号,擦除()/迭代器问题