简体   繁体   English

接近STL算法,lambda,局部类和其他方法

[英]Approaching STL algorithms, lambda, local classes and other approaches

One of the things that seems to be necessary with use of STL is a way to specify local functions. 使用STL似乎必需的一件事是指定本地函数的方法。 Many of the functions that I would normally provide cannot be created using STL function object creation tools ( eg bind ), I have to hand roll my function object. 我通常会提供的许多功能无法使用STL函数对象创建工具(例如bind)来创建,我必须手动滚动函数对象。

Since the C++ standard forbids local types to be used as arguments in template instantiations the best I was able to use was to create a small library, ( just showing relevant parts ) 由于C ++标准禁止将本地类型用作模板实例化中的参数,因此我能够使用的最好的方法是创建一个小型库(仅显示相关部分)

// library header
class MyFunctionBase<R,T>  
{  
 public:  
   virtual ~MyFunctionBase();  
   virtual R operator()(const T &) const=0;  
};    


class  MyFunction<R,T> 
{   
    MyFunctionBase<R,T> *b; 
 public: 
    ~MyFunction()
    {
       delete b;
    }
    virtual R operator()(const T &) const
    {
        return (*b)(T);
    } 
};


// source file
....

    class func: public MyFunctionBase  ...
    std::stl_alg(....    MyFunction(new funct));

This has always seemed unwieldly to me. 在我看来,这始终是无稽之谈。 I guess to the people on the ISO committee believe so too and added a lambda to C++. 我想对ISO委员会的成员也是如此,并在C ++中增加了一个lambda。

In the meantime how have compilers addressed this problem? 同时,编译器如何解决此问题? ( Especially Windows compilers. ) (尤其是Windows编译器。)

A correction which might clarify a bit. 可能会澄清一点的更正。 Changelog: Nov 2 replaced to clarify Since the C++ standard forbids local classes as function objects 变更日志:替换11月2日以进行澄清,因为C ++标准禁止将本地类用作函数对象

The standard way is a "functor" - basically, a struct that supplies an operator() 的标准方法是一个“算符” -基本上,一个struct供给一个operator()

For example: 例如:

struct MyMinFunctor {
  bool operator()(const int& a, const int& b) { return a > b; }
};

vector<int> v;
sort(v.begin(), v.end(), MyMinFunctor());

Because it is a struct/class, you can subclass any of the things like 'binary_operator' as well as maintain state for more advanced functors. 因为它是结构/类,所以您可以将诸如“ binary_operator”之类的任何子类化,也可以为更高级的函子维护状态。

With C++0x you can use lambda's (as you mentioned): 使用C ++ 0x,您可以使用lambda(如前所述):

for_each(container.begin(), container.end(),
  [](auto item) {
    // do something with item
  }
  );

This is already available in MS Visual C++ 2010 (currently in Community Tech Preview) and GCC 4.3.x (with the -std=c++0x compiler flag). 这已在MS Visual C ++ 2010(当前在Community Tech Preview中)和GCC 4.3.x(带有-std = c ++ 0x编译器标志)中可用。 However, without lambda's, you just need to provide a type that: 但是,如果没有lambda,则只需提供以下类型:

  1. Is default constructible 默认可构造
  2. Is copy constructible 复制可构造
  3. Defines a function operator overload 定义函数运算符重载

There are some algorithms that require binary function objects while there are some that require unary function objects. 有些算法需要二进制函数对象,而有些算法则需要一元函数对象。 Refer your vendor's STL documentation to find out exactly which algorithms require binary function objects and which ones require unary function objects. 请参阅供应商的STL文档,以准确找出哪些算法需要二进制函数对象,哪些算法需要一元函数对象。

One thing you might also want to look into are the newer implementations of bind and function in TR1 (based on Boost.Bind and Boost.Function). 您可能还需要研究的一件事是TR1中基于bindfunction的较新实现(基于Boost.Bind和Boost.Function)。

Boost.Bind,Boost.Function和Boost.Lambda是您的朋友。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM