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 07:23:26

问题


I give the following code to illustrate my question:

#include <vector>

struct Complex
{
     int a, b, c;

     Complex() : a(3), b(4), c(10) {}

     operator int() const {  return a+b+c; }
};

int main()
{
   Complex abc;
   int value = (abc);
   Complex def;
   def.a = 20;
   int value2 = (def);

   std::vector<Complex> ar;
   ar.push_back(abc);
   ar.push_back(def);

   std::vector<int> ar2;
   ar2.push_back(abc);
   ar2.push_back(def);

   std::vector<int> ar3;
   ar3 = (ar);
}

This won't compile, due to the expression ar3 = (ar). I have declared a conversion operator so that the Complex class can be used in where int is expected. Can I also make it work for assigning an array of Complex objects to an array of int?

I tried to declare a non-member conversion operator for array of Complex, but that's not allowed:

void std::vector<int> operator = (std::vector<Complex> complexArray)
{
    std::vector<int> abc;
    for(int i=0; i<complexArray.size(); i++)
     abc.push_back(complexArray[i]);
    return abc;
}

回答1:


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;
}



回答2:


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

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



回答3:


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.



来源:https://stackoverflow.com/questions/38304299/how-can-i-define-operators-so-that-a-array-of-user-defined-types-can-be-transfor

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