我有这个函数是假设返回向量中的所有可能的整数置换。代码是基于现有的代码来完成字符串的置换,我试图对它进行改造以使用向量,但显然,它们的工作方式与我想的不同。 我会感谢您提供的任何帮助,谢谢。使用向量的递归排列
vector<vector<int>> permute(vector<int> &v1, vector<int> &v2){
vector<vector<int>> v;
if(v1.empty())
{
v.push_back(v2);
return v;
}
for(auto it = v1.begin(); it != v1.end(); it++){
vector<int> temp1 = v1;
temp1.erase(it); //there's a runtime error on this line
vector<int> temp2 = v2;
temp2.push_back(*it);
permute(temp1, temp2);
}
return v;
}
这是排列字符串的原始代码。
void string_permutation(std::string& orig, std::string& perm)
{
if(orig.empty())
{
std::cout<<perm<<std::endl;
return;
}
for(int i=0;i<orig.size();++i)
{
std::string orig2 = orig;
orig2.erase(i,1);
std::string perm2 = perm;
perm2 += orig.at(i);
string_permutation(orig2,perm2);
}
}
哪些错误与'的std :: next_permutation' ? – PSIAlt
@PSIAlt,嗨,我很抱歉,我忘了提及,我不允许使用STL算法中的任何函数 – XDProgrammer
只是好奇。为什么“不允许”? – PSIAlt