How can I define operators so that a array of user-defined types can be transformed into an array of primitive types?

我的未来我决定 提交于 2019-12-02 06:39:31

Forget about automatic implicit conversion (at least for the Standard Library containers). But if you are willing to accept an explicit conversion like in the below example

 const std::vector<int> vi {1, 2, 3, 4, 5};
 const std::vector<double> vd = container_cast(vi);

then the implementation of the container_cast() utility follows. Note that it can cast not only between instantiations of the same template container for different element types (i.e. std::vector<int> to std::vector<double>), but also between different containers (e.g. std::vector to std::list).

#include <iostream>
#include <vector>
#include <list>

template<class SourceContainer>
class ContainerConverter
{
    const SourceContainer& s_;
public:
    explicit ContainerConverter(const SourceContainer& s) : s_(s) {}

    template<class TargetContainer>
    operator TargetContainer() const
    {
        return TargetContainer(s_.begin(), s_.end());
    }
};

template<class C>
ContainerConverter<C> container_cast(const C& c)
{
    return ContainerConverter<C>(c);
}

template<class C>
void printContainer(const C& c)
{
    std::cout << "{ ";
    for( auto x : c )
        std::cout << x << ' ';
    std::cout << "}" << std::endl;
}

int main()
{
    const std::vector<double> vd {2.2, 7.7, 5.5, 1.1, -4.4};
    printContainer(vd);

    const std::vector<int> vi = container_cast(vd);
    printContainer(vi);

    const std::list<float> lf = container_cast(vd);
    printContainer(lf);
    return 0;
}

You may consider the range constructor of std::vector.

std::vector<int> ar3(begin(ar), end(ar));

Whenever you want to transform something, the std::transform function might be good to use.

In your case you could do something like

// Create the new vector with the same size as the complex vector
std::vector<int> abc(complexArray.size());

std::transform(std::begin(complexVector), std::end(complexVector),
               std::begin(abc),
               [](Complex const& complex)
               {
                   int result;

                   // Code here that converts the complex structure to an integer
                   // and stores the integer in the variable result

                   return result;
               });

After the std::transform call above (once you complete it with the code to actually do the structure transformation) the vector abc will contain all the converted integers from the Complex structures in the source vector complexVector.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!