Cross Concatenate Elements of 2 Vectors to Produce a Third

后端 未结 3 802
余生分开走
余生分开走 2021-01-23 06:38

I have 2 vectors and want to distribute one across the other to form a third vector like:

V1 = (a,b,c)
V2 = (d,e,f)

Result:

V3          


        
3条回答
  •  轮回少年
    2021-01-23 06:56

    In C++:

    std::vector v3 { v1[0] * v2[0], v1[0] * v2[1], v1[0] * v2[2], v1[1] * v2[0], v1[1] * v2[1], v1[1] * v2[2], v1[2] * v2[0], v1[2] * v2[1], v1[2] * v2[2] };
    

    ...though, I usually would format it in multiple lines.

    As MCVE:

    #include 
    #include 
    using namespace std;
    
    template 
    ostream& operator << (ostream &out, const vector &vec)
    {
      const char *sep = "{ ";
      for (const ELEM &elem : vec) { out << sep << elem; sep = ", "; }
      return out << " }";
    }
    
    int main()
    {
      vector v1{ 1.0, 2.0, 3.0 }, v2{ 4.0, 5.0, 6.0 };
      // in one line:
      vector v3 { v1[0] * v2[0], v1[0] * v2[1], v1[0] * v2[2], v1[1] * v2[0], v1[1] * v2[1], v1[1] * v2[2], v1[2] * v2[0], v1[2] * v2[1], v1[2] * v2[2] };
      // output:
      cout << v3 << endl;
      // done
      return 0;
    }
    

    Output:

    { 4, 5, 6, 8, 10, 12, 12, 15, 18 }
    

    (Tested on ideone.)

    Thanks, Rory Daulton, for the inspiration. In C++, I can do this also:

    #include 
    #include 
    #include 
    
    using namespace std;
    
    string operator*(const string &str1, const string &str2)
    {
      return str1 + str2;
    }
    
    template 
    ostream& operator << (ostream &out, const vector &vec)
    {
      const char *sep = "{ ";
      for (const ELEM &elem : vec) { out << sep << elem; sep = ", "; }
      return out << " }";
    }
    
    int main()
    {
      vector v1{ "a", "b", "c" }, v2{ "d", "e", "f" };
      // in one line:
      vector v3 { v1[0] * v2[0], v1[0] * v2[1], v1[0] * v2[2], v1[1] * v2[0], v1[1] * v2[1], v1[1] * v2[2], v1[2] * v2[0], v1[2] * v2[1], v1[2] * v2[2] };
      // output:
      cout << v3 << endl;
      // done
      return 0;
    }
    

    Output:

    { ad, ae, af, bd, be, bf, cd, ce, cf }
    

    After a little improvement, it will even work for mixed types:

    #include 
    #include 
    #include 
    
    using namespace std;
    
    string operator*(const string &arg1, int arg2)
    {
      string ret; for (int i = 0; i < arg2; ++i) ret += arg1;
      return ret;
    }
    
    template 
    ostream& operator << (ostream &out, const vector &vec)
    {
      const char *sep = "{ ";
      for (const ELEM &elem : vec) { out << sep << elem; sep = ", "; }
      return out << " }";
    }
    
    int main()
    {
      vector v1{ "a", "b", "c" }; vector v2{ 1, 2, 3 };
      // in one line:
      vector v3 { v1[0] * v2[0], v1[0] * v2[1], v1[0] * v2[2], v1[1] * v2[0], v1[1] * v2[1], v1[1] * v2[2], v1[2] * v2[0], v1[2] * v2[1], v1[2] * v2[2] };
      // output:
      cout << v3 << endl;
      // done
      return 0;
    }
    

    Output:

    { a, aa, aaa, b, bb, bbb, c, cc, ccc }
    

提交回复
热议问题