2015-05-26 75 views
1

我有存储在向量中的字符串,如:vector<string> ex = {"ab", "cd", "ef"}。 现在我需要创建这些字符串的笛卡尔乘积(向量中的字符串数量,也不是字符串的长度是固定的!)。结果应该是:多个字符串的C++笛卡尔积

ace 
    acf 
    ade 
    adf 
    bce 
    bcf 
    bde 
    bdf 

是否有这个已经存在一个内置的功能,或者你有任何建议如何执行落实?

字符串的单个字母应该用于笛卡尔积而不是整个字符串!

+0

如果答案(如我怀疑的那样)是“否”,你是否有后续问题? –

+0

至于建议:这看起来像递归工作。但是你需要指定你希望得到结果的形式(打印输出?另一个字符串向量?别的?)。 –

+0

[我如何创建矢量向量的笛卡尔积?](http://stackoverflow.com/questions/5279051/how-can-i-create-cartesian-product-of-vector-of-vectors) –

回答

0

好吧,我想出了一个解决方案。它可能不是最好的之一,并有一定的代码的一些改进可能的,但它足以让我的目的,万一有人需要它太:

vector<string> getProducts(vector<string> s) { 
int combinations = 1; 
vector<string> res; 
for (unsigned int i=0; i<s.size(); i++) { 
    combinations *= s.at(i).length(); 
} 

for (unsigned int i=0; i<s.size(); i++) { 
    string cur = s.at(i); 
    int div = combinations/cur.length(); 
    int count = 0; 
    for (unsigned int ch=0; ch<cur.length(); ch++) { 
     for (int len=0; len<div; len++) { 
      if (i==0) { 
       res.push_back(string(cur.substr(ch, 1))); 
      } else { 
       string tmp = res.at(count); 
       tmp.append(string(cur.substr(ch,1))); 
       res.at(count) = tmp; 
      } 
      count++; 
     } 


     if ((ch == cur.length()-1) && (count <= res.size()-1) && i>0) { 
      ch = -1; 
     } 
    } 
    combinations = div; 
} 

return res; 

}

-1

可能是std :: accumulate可以达到的结果。

人们需要这种形式

std::vector<string> product(const std::vector<string> &init, string value) 
{ 
    std::string result ; 
    for (auto ch : value) 
     if (init.empty()) 
      result.push_back(string(1, ch)) ; 
     else 
      for (auto str : init) 
       result.push_back(str + string(1, ch)) ; 
    return result ; 
} 

然后调用的std())vect.begin(),vect.end(,性病::向量(产品)的二进制运算::累加

+0

它不是可编译的,什么是std :: accumulator?什么是“prod”? – wasp256

+0

std :: accumulate是标准报头的功能。 prod是一个错字..我纠正它到产品 – marom

+0

我包括''现在 - >仍然得到一个'功能积累无法解决',该函数给我错误'不返回','result.push(... ) - >无效参数' – wasp256

2

我可以使用提供这样图书馆 https://cpplinq.codeplex.com/

#include <iostream> 
#include <vector> 
# include <algorithm> 
# include <iterator> 
# include <string> 
# include <tuple> 
# include <functional> 
# include <cmath> 
#include "cpplinq.hpp" 
using namespace cpplinq; 

std::vector<std::string> simpleTransform(const std::vector<std::string> &ex); 
int main() 
{ 
std::vector<std::string> ex1(3); 
ex1[0]="ab"; 
ex1[1]="cd"; 
ex1[2]="ef"; 

auto VS = simpleTransform(ex1); 
std::copy(VS.begin(),VS.end(),std::ostream_iterator<std::string>(std::cout,"\n")); 

return 0; 
} 

std::vector<std::string> simpleTransform(const std::vector<std::string> &ex) 
{ 
size_t N = ex.size(); 
size_t M = ex[0].size(); 
std::vector<std::string> VS(pow(M,N)); 
size_t count=0; 

std::function<void(size_t,std::vector<size_t>)> Var= 
    [&](size_t ind,std::vector<size_t> vec) 
    { 
    if(ind==0) 
     { 
     std::string r; 
     r.resize(N); 
     for(size_t j=0;j<N;j++) 
      r[j] = ex[j][vec[j]-1]; 

     VS[count] =r; 
     count++; 
     return; 
     } 
    else 
     { 
     std::vector<size_t> newvec(vec); 
     auto temp = N-ind+1; 
     newvec.resize(temp); 
     range(1,M)>>for_each([&](int const & j){ 
     newvec[temp-1]=j; 
     Var(ind-1,newvec);}); 
     } 
    }; 
Var(N,std::vector<size_t>()); 

return VS; 
} 

我没有做输入数据的验证。