覆盖迭代器的运算符*() [英] Overriding operator*() of iterators

查看:156
本文介绍了覆盖迭代器的运算符*()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一些用户定义的复杂结构,比如

Say that I have some user-defined complex struct, like

struct usrStruct{
     double a;
     T1 b;
     T2 c;
     /* and so on...*/
}

这是用作std :: vector,std :: list或任何 iterable 的基本元素..

which is used as a basic element for a std::vector, std::list or anything iterable..

假设 std :: vector< usrStruct> 通过迭代器传递给我的函数

Say that the std::vector<usrStruct> is passed to a function of mine through iterators

  template<class InputIterator>  
  T myFoo( InputIterator first, InputIterator last ){ /*...*/ }.

:是否有标准方法可以覆盖运算符*() InputIterator ,(在这种情况下为 std :: vector< usrStruct> :: iterator )以便 myFoo 只需 成员交互

Q: Is there a standard way to override operator*() of the InputIterator, (in this case of std::vector<usrStruct>::iterator ) so that myFoo just interacts with the member a?

即,

  *first == (*first).a;

因此 myFoo 正常工作 关于 usrStruct 的整个定义?

and thus myFoo works orthogonally with respect to the whole definition of usrStruct?

谢谢。

推荐答案

不,你做不到。您可以将结构隐式转换为double(通过运算符double )。或者你可以通过重载 operator ==(usrSruct,double)和/或 operator ==(double,usrStruct)

No, you cannot do that. You can make your struct implicitly convertible to a double (via operator double). Or you can allow direct comparisons by overloading operator==(usrSruct,double) and/or operator==(double,usrStruct).

这篇关于覆盖迭代器的运算符*()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆