C++11 使用C++;11兰博达斯

C++11 使用C++;11兰博达斯,c++11,C++11,我对C++11很感兴趣,并试图想出一种更优雅的方法,对两个std::vector进行两两比较。对于成对最大值,我使用的是相当不雅的 typedef std::vector<double> vec; vec pairwiseMax(vec& a, vec& b) { ASSERT(a.size() == b.size()); vec c(a); // seed with a for (unsigned int i = 0; i < a.si

我对C++11很感兴趣,并试图想出一种更优雅的方法,对两个
std::vector
进行两两比较。对于成对最大值,我使用的是相当不雅的

typedef std::vector<double> vec;
vec pairwiseMax(vec& a, vec& b)
{
    ASSERT(a.size() == b.size());
    vec c(a); // seed with a
    for (unsigned int i = 0; i < a.size(); i++)
    {
        if (b[i] > a[i]) // bigger than
            c[i] = b[i];
    }
    return c;
}

在Igor发布后,我现在有:

vec c;
c.reserve(a.size());
std::transform(a.begin(), a.end(), b.begin(),
    std::back_inserter(c),
    [](double const& d, double const& e)
    { return max(d, e); });
两个问题:

  • 为什么不像我以前那样把
    veca
    复制到
    vec
  • 使用back_inserter不是增加了很多额外的操作吗?如果从
    c==a
    开始,则需要较少的
    c
    插入
  • 像这样:

    vec c(a); // seed with a
    std::transform(a.begin(), a.end(), b.begin(),
        c.begin(),
        [](double const& d, double const& e)
        { return max(d, e); });
    return c;
    
    vec;
    c、 储备(a.size());
    std::transform(a.begin(),a.end(),b.begin(),
    标准:背面插入器(c),
    标准:最大值);
    
    vec;
    c、 储备(a.size());
    std::transform(a.begin(),a.end(),b.begin(),
    标准:背面插入器(c),
    标准:最大值);
    
    Igor的解决方案很棒,但我在编译时遇到了麻烦。我发现我需要在匿名函数中包装std::max,以使其能够编译

    以下是两个模板函数,允许对任意数量的向量取成对最大值:

    // pairwise maximum                                               
    template<typename T>                                              
    vector<T> pmax(const std::vector<T>& a, const std::vector<T>& b) {
        std::vector<T> c;                                             
        assert(a.size() == b.size());                                 
        c.reserve(a.size());                                          
        std::transform(a.begin(), a.end(), b.begin(),                 
                       std::back_inserter(c),                         
                       [](T a, T b) { return std::max<T>(a, b); });   
        return c;                                                     
    }                                                                 
    
    // maximum of all vectors
    template<typename T>                                                      
    vector<T> vpmax(const std::vector<std::vector<T>>& vv) {                  
        std::vector<T> c;                                                     
        if (vv.empty()) return c;                                             
        c = vv.front();                                                       
        typename std::vector<std::vector<T> >::const_iterator v = vv.begin(); 
        ++v; // skip the first element                                        
        for ( ; v != vv.end(); ++v) {                                         
            c = pmax(c, *v);                                                  
        }                                                                     
        return c;                                                             
    }
    
    //成对最大值
    模板
    向量pmax(常数标准::向量a、常数标准::向量b){
    std::向量c;
    断言(a.size()==b.size());
    c、 储备(a.size());
    std::transform(a.begin(),a.end(),b.begin(),
    标准:背面插入器(c),
    [](ta,tb){return std::max(a,b);};
    返回c;
    }                                                                 
    //所有向量的最大值
    模板
    向量vpmax(const std::vector&vv){
    std::向量c;
    if(vv.empty())返回c;
    c=vv.front();
    typename std::vector::const_迭代器v=vv.begin();
    ++v、 //跳过第一个元素
    对于(;v!=vv.end();++v){
    c=pmax(c,*v);
    }                                                                     
    返回c;
    }
    
    Igor的解决方案很棒,但我在编译时遇到了麻烦。我发现我需要在匿名函数中包装std::max,以使其能够编译

    以下是两个模板函数,允许对任意数量的向量取成对最大值:

    // pairwise maximum                                               
    template<typename T>                                              
    vector<T> pmax(const std::vector<T>& a, const std::vector<T>& b) {
        std::vector<T> c;                                             
        assert(a.size() == b.size());                                 
        c.reserve(a.size());                                          
        std::transform(a.begin(), a.end(), b.begin(),                 
                       std::back_inserter(c),                         
                       [](T a, T b) { return std::max<T>(a, b); });   
        return c;                                                     
    }                                                                 
    
    // maximum of all vectors
    template<typename T>                                                      
    vector<T> vpmax(const std::vector<std::vector<T>>& vv) {                  
        std::vector<T> c;                                                     
        if (vv.empty()) return c;                                             
        c = vv.front();                                                       
        typename std::vector<std::vector<T> >::const_iterator v = vv.begin(); 
        ++v; // skip the first element                                        
        for ( ; v != vv.end(); ++v) {                                         
            c = pmax(c, *v);                                                  
        }                                                                     
        return c;                                                             
    }
    
    //成对最大值
    模板
    向量pmax(常数标准::向量a、常数标准::向量b){
    std::向量c;
    断言(a.size()==b.size());
    c、 储备(a.size());
    std::transform(a.begin(),a.end(),b.begin(),
    标准:背面插入器(c),
    [](ta,tb){return std::max(a,b);};
    返回c;
    }                                                                 
    //所有向量的最大值
    模板
    向量vpmax(const std::vector&vv){
    std::向量c;
    if(vv.empty())返回c;
    c=vv.front();
    typename std::vector::const_迭代器v=vv.begin();
    ++v、 //跳过第一个元素
    对于(;v!=vv.end();++v){
    c=pmax(c,*v);
    }                                                                     
    返回c;
    }
    
    您可能需要添加一个
    c.reserve(a.size())
    @igor-尚未找到
    std::transform
    -谢谢!但是您的建议不起作用,因为
    std::max
    有多个重载(如此处:)。如果我们使用匿名函数,这可以起作用。请参阅我的回答。您可能需要添加一个
    c.reserve(a.size())
    @igor-尚未找到
    std::transform
    -谢谢!但是您的建议不起作用,因为
    std::max
    有多个重载(如此处:)。如果我们使用匿名函数,这可以起作用。看我的回答。伊戈尔的方法比你原来的方法更有效。如果a和b包含均匀分布的随机值,则原始方法将生成1.5N个副本,但Igor的方法仅生成1N个副本。Igor的方法比原始方法更有效。如果a和b包含均匀分布的随机值,则原始方法将生成1.5N个副本,但Igor的方法仅生成1N个副本。您可能已经定义了最大值(例如Visual Studio)。如果是这样,只需编写“(std::max)”而不是“std::max”,让预处理器将其识别为单个实体。不需要额外的功能。啊,很有趣。我可以试试。它在gcc上似乎不起作用。我将保持原样。您可能已经定义了max(例如Visual Studio)。如果是这样,只需编写“(std::max)”而不是“std::max”,让预处理器将其识别为单个实体。不需要额外的功能。啊,很有趣。我可以试试看,它不起作用
    // pairwise maximum                                               
    template<typename T>                                              
    vector<T> pmax(const std::vector<T>& a, const std::vector<T>& b) {
        std::vector<T> c;                                             
        assert(a.size() == b.size());                                 
        c.reserve(a.size());                                          
        std::transform(a.begin(), a.end(), b.begin(),                 
                       std::back_inserter(c),                         
                       [](T a, T b) { return std::max<T>(a, b); });   
        return c;                                                     
    }                                                                 
    
    // maximum of all vectors
    template<typename T>                                                      
    vector<T> vpmax(const std::vector<std::vector<T>>& vv) {                  
        std::vector<T> c;                                                     
        if (vv.empty()) return c;                                             
        c = vv.front();                                                       
        typename std::vector<std::vector<T> >::const_iterator v = vv.begin(); 
        ++v; // skip the first element                                        
        for ( ; v != vv.end(); ++v) {                                         
            c = pmax(c, *v);                                                  
        }                                                                     
        return c;                                                             
    }