Generate the Cartesian Product of 2 vector

2019-02-28 23:23发布

If I want to get the Cartesian Product of these two vector<string>s:

vector<string> final{"a","b","c"};
vector<string> temp{"1","2"};

But I want to put the result in final, such that final would contain:

a1
a2
b1
b2
c1
c2

I'd like to do this without creating a temporary array. Is it possible to do this? If it matters, the order of final is not important.

4条回答
Melony?
2楼-- · 2019-03-01 00:00

You may try the following approach

#include <iostream>
#include <vector>
#include <string>

int main() 
{
    std::vector<std::string> final{ "a", "b", "c" };
    std::vector<std::string> temp{ "1", "2" };

    auto n = final.size();

    final.resize( final.size() * temp.size() );

    for ( auto i = n, j = final.size(); i != 0; --i )
    {

        for ( auto it = temp.rbegin(); it != temp.rend(); ++it )
        {
            final[--j] = final[i-1] + *it; 
        }

    }

    for ( const auto &s : final ) std::cout << s << ' ';
    std::cout << std::endl;

    return 0;
}

The program output is

a1 a2 b1 b2 c1 c2 
查看更多
smile是对你的礼貌
3楼-- · 2019-03-01 00:03

This works for me:

void testCartesianString(vector<string>& final,
                         vector<string>const& temp)
{
   size_t size1 = final.size();
   size_t size2 = temp.size();

   // Step 1.
   // Transform final to : {"a","a","b","b","c","c"}
   final.resize(size1*size2);
   for ( size_t i = size1; i > 0; --i )
   {
      for ( size_t j = (i-1)*size2; j < i*size2; ++j )
      {
         final[j] = final[i-1];
      }
   }

   // Step 2.
   // Now fix the values and
   // change final to : {"a1","a2","b1","b2","c1","c2"}
   for ( size_t i = 0; i < size1; ++i )
   {
      for ( size_t j = 0; j < size2; ++j )
      {
         final[i*size2+j] = final[i*size2+j] + temp[j];
         cout << final[i*size2+j] << " ";
      }
      cout << endl;
   }
}
查看更多
Anthone
4楼-- · 2019-03-01 00:04

This is just a personal preference option to Vald from Moscow's solution. I think it may be faster for dynamic arrays because there would be less branching. But I haven't gotten around to writing a timing test bench.

Given the inputs vector<string> final and vector<string> temp:

const auto size = testValues1.first.size();

testValues1.first.resize(size * testValues1.second.size());

for (int i = testValues1.first.size() - 1; i >= 0; --i){
    testValues1.first[i] = testValues1.first[i % size] + testValues1.second[i / size];
}

EDIT:

Nope, this solution is slower not faster: http://ideone.com/e.js/kVIttT

And usually significantly faster, though I don't know why...

In any case, prefer Vlad from Moscow's answer

查看更多
姐就是有狂的资本
5楼-- · 2019-03-01 00:06

Try the function cartesian:

#include <vector>
#include <string>

using namespace std;

void cartesian(vector<string>& f, vector<string> &o) {
int oldfsize = f.size();
f.resize(oldfsize * o.size());
for (int i = o.size() - 1; i>=0; i--) {
  for (int j = 0; j < oldfsize; j++) {
     f[i*oldfsize + j] = f[j] + o[i];
  }
 }
}


int main() 
{
vector<string> f{"a","b","c"};
vector<string> temp{"1","2"};
cartesian(f, temp);
for (auto &s: f) {
  printf("%s\n", s.c_str());
 }
}
查看更多
登录 后发表回答