有没有的IEnumerable℃的标准C ++同等学历; T>在C#中? [英] Is there a standard C++ equivalent of IEnumerable<T> in C#?

查看:104
本文介绍了有没有的IEnumerable℃的标准C ++同等学历; T>在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

还是可以安全使用矢量如果T的枚举是刚刚上市的所有元素?

Or is it safe to use vector if the Enumerator of T is just listing all the elements?

推荐答案

这是不是需要C ++,和这里的原因:

It isn't needed in C++, and here's why:

C#只支持动态多态。因此,要创建一个可重用的算法,你需要的所有迭代器将实现一个接口。这是的IEnumerator< T> 的IEnumerable< T> 是一个工厂返回一个迭代器

C# only supports dynamic polymorphism. So to create a reusable algorithm, you need an interface which all iterators will implement. That's IEnumerator<T>, and IEnumerable<T> is a factory for returning an iterator.

C ++模板,而另一方面,支持鸭打字。这意味着你不需要为了限制通过一个接口泛型类型参数访问成员 - 编译器会查找成员的名字为模板的每个实例

C++ templates, on the other hand, support duck typing. That means you don't need to constrain a generic type parameter by an interface in order to access members -- the compiler will look up members by name for each individual instantiation of the template.

C ++的容器和迭代器具有隐式接口相当于.NET 的IEnumerable&LT; T&GT; 的IEnumerator&LT; T&GT; 的ICollection&LT; T&GT; 的IList&LT; T&GT; ,即:

C++ containers and iterators have implicit interfaces which is equivalent to .NET IEnumerable<T>, IEnumerator<T>, ICollection<T>, IList<T>, namely:

有关容器:


  • 迭代器常量性的typedef

  • 开始()成员函数 - 填充需要的IEnumerable&LT; T&GT; ::的GetEnumerator()

  • 端()成员函数 - 而不是的IEnumerator&LT; T&GT; :: MoveNext的()返回值

  • iterator and const_iterator typedefs
  • begin() member function -- fills the need for IEnumerable<T>::GetEnumerator()
  • end() member function -- instead of IEnumerator<T>::MoveNext() return value

有关前向迭代器:


  • VALUE_TYPE 的typedef

  • 符++ - 而不是的IEnumerator&LT; T&GT; :: MoveNext的()

  • 运算符* 操作符&GT; - 而不是的IEnumerator&LT; T&GT; ::目前

  • 引用返回从运算符* 类型 - 而不是的IList&LT; T&GT; 索引二传手

  • 运算符== 运算符= ! - 没有真正的等同于.NET,但与容器的端()匹配的IEnumerator&LT; T&GT; :: MoveNext的()返回值

  • value_type typedef
  • operator++ -- instead of IEnumerator<T>::MoveNext()
  • operator* and operator-> -- instead of IEnumerator<T>::Current
  • reference return type from operator* -- instead of IList<T> indexer setter
  • operator== and operator!= -- no true equivalent in .NET, but with container's end() matches IEnumerator<T>::MoveNext() return value

有关随机访问迭代器:


  • 运营商+ 运营商 - 运算符[] - 而不是的IList&LT; T&GT;

  • operator+, operator-, operator[] -- instead of IList<T>

如果您定义这些,那么标准算法将与您的容器和迭代器工作。接口没有必要,不需要虚函数。不使用虚函数使得C ++通用code比同等.NET code快,有时要快得多。

If you define these, then standard algorithms will work with your container and iterator. No interface is needed, no virtual functions are needed. Not using virtual functions makes C++ generic code faster than equivalent .NET code, sometimes much faster.

请注意:写泛型算法时,最好使用的std ::开始(容器)的std ::结束(容器)而不是容器成员函数。允许你的算法与原始阵列(不具有成员函数)除了STL容器一起使用。原数组和原始指针满足容器和迭代器的所有其他要求,这个唯一的例外。

Note: when writing generic algorithms, it's best to use std::begin(container) and std::end(container) instead of the container member functions. That allows your algorithm to be used with raw arrays (which don't have member functions) in addition to the STL containers. Raw arrays and raw pointers satisfy all other requirements of containers and iterators, with this single exception.

这篇关于有没有的IEnumerable℃的标准C ++同等学历; T&GT;在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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