为什么std :: transform使用构造函数?

时间:2012-07-12 14:16:53

标签: c++ optimization stl transform

我使用std :: transform将一些值添加到列表中的现有值。下面的代码工作正常,我只是想知道是否有可能在执行转换时避免对复制构造函数的所有调用(参见程序的输出)。如果我只是破解代码,并使for循环显式调用Base的+ =运算符,则不会执行复制构造,并且更改值的位置更有效。

我可以使变换调用运算符+ = Base而不是复制构造吗?我应该专注于increment<Type>吗?

该计划:

#include <iostream>
#include<list>
#include <algorithm>
#include <iterator>

template<class T>
class Base;

template<class T>
std::ostream& operator << (std::ostream& os, const Base<T>& b);

template<class T>
class Base
{
    private: 
        T b_;
    public: 
        typedef T value_type;

        Base()
            :
                b_()
        { std::cout << "Base::def ctor" << std::endl; }

        Base (const T& b)
            : 
                b_(b)
        { std::cout << "Base::implicit conversion ctor: " << b_ << std::endl; }

        const T& value()
        {
            return b_;
        }

        const Base operator+(const Base& b) const
        {
            std::cout << "Base operator+ " << std::endl;
            return Base(b_ + b.b_);
        }

        const Base&  operator+=(const T& t) 
        {
            b_ += t;
            return *this;
        }

        friend std::ostream& operator<< <T> (std::ostream& os, const Base<T>& b);
};

template<class T>
std::ostream& operator<< (std::ostream& os, const Base<T>& b)
{
    os << b.b_; 
    return os;
}

template<class Type> 
class increment
{
    typedef typename Type::value_type T; 

    T initial_; 

    public: 

        increment()
            :
                initial_()
        {};

        increment(const T& t)
            :
                initial_(t)
        {}

        T operator()()
        {
            return initial_++;
        }
};

template<class Container>
void write(const Container& c)
{
    std::cout << "WRITE: " << std::endl;
    copy(c.begin(), c.end(), 
         std::ostream_iterator<typename Container::value_type > (std::cout, " "));
    std::cout << std::endl;
    std::cout << "END WRITE" << std::endl;
}

using namespace std;

int main(int argc, const char *argv[])
{
    typedef list<Base<int> > bList; 

    bList baseList(10); 

    cout << "GENERATE" << endl;
    generate_n (baseList.begin(), 10, increment<Base<int> >(10));
    cout << "END GENERATE" << endl;

    write(baseList); 

    // Let's add some integers to Base<int>

    cout << "TRANSFORM: " << endl;

    std::transform(baseList.begin(), baseList.end(), 
                   baseList.begin(), 
                   bind2nd(std::plus<Base<int> >(), 4)); 
    cout << "END TRANSFORM " << endl;

    write(baseList);

    // Hacking the code: 
    cout << "CODE HACKING: " << endl;
    int counter = 4;
    for (bList::iterator it = baseList.begin(); 
         it != baseList.end(); 
         ++it)
    {
        *it += counter; // Force the call of the operator+=
        ++counter;
    }
    write (baseList);
    cout << "END CODE HACKING" << endl;

    return 0;
}

1 个答案:

答案 0 :(得分:8)

Base (const T& b)不是复制构造函数,它是Base<T>的构造函数,接受const T&。复制构造函数通常具有签名Base(const Base& )

也就是说,每次您从Base<int>创建一个新的int时,都会调用您的构造函数,而这正是您在添加运算符中所做的。

最后,std :: transform()使用输出迭代器赋值运算符将函数的结果赋给输出。如果您想完全避免使用该副本,则应使用std::for_eachstd::bind2nd( std::mem_fun_ref(&Base<int>::operator +=), 4 ))。这将避免复制,因为它将在引用上运行。