[英]How to properly implement -> and (*). so that they behave like -> and (*). in an iterator
[英]How to make iterator behave like a type through it iterates?
我想遍历stirngs的向量,并将所有输入与一个值进行比较。 所以我创建了这个构造:
for (vector<string>::const_iterator i = needle.begin(); i != needle.end(); ++i) {
cout << *i << " ";
}
印刷工作的很好,但我希望能够访问std::string
的功能,即find
因此,尽管我能写needle[index].find("sampleString");
我希望能够使迭代器的行为与此类似。 那么如何制作类似*i.find("sampleString");
?
您*i.find()
,但是运算符优先级意味着*i.find()
不起作用。 它尝试取消引用i.find()
,而不是取消引用i
然后在结果上调用find
。 使用以下最适合您的方法:
(*i).find("sampleString");
i->find("sampleString");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.