0

I have this function that is suppose to return all possible permutation of integers inside the vector. The code is based from an existing code that does a permutation of strings, I tried to remodeled it to work on vectors but apparently, they dont work similarly as I thought.. I'll appreciate any help that you could offer thanks;

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

This is the original code that permutes a string.

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);
        } 
    }
3
  • Whats wrong with std::next_permutation? Commented Jun 19, 2015 at 19:47
  • @PSIAlt ,Hi, Im sorry, I forgot to mention, I am not allowed to use any function in STL algorithm Commented Jun 19, 2015 at 19:52
  • just curious.. Why "not allowed" ? Commented Jun 19, 2015 at 20:22

2 Answers 2

2

Here you go:

   template < typename T>
   void vec_permute( std::vector<T> &orig, std::vector<T> &perm)
   {
       if(orig.empty())
       {
            for( auto &x : perm)
                std::cout<<x;
            std::cout<<"\n";
            return;
       }
       for(typename std::vector<T>::size_type i=0;i <orig.size();++i)
       {
            std::vector<T> orig2(orig);
            orig2.erase(std::find(orig2.begin(),orig2.end(),orig.at(i)));
            std::vector<T> perm2(perm);
            perm2.push_back(orig.at(i));
            vec_permute(orig2,perm2);            
       }
   }

Demo: http://coliru.stacked-crooked.com/a/01ded4b778aa4165

Sign up to request clarification or add additional context in comments.

1 Comment

Hi thank for this code, I test it and work.. , apparently I am not allowed to use algorithm STL.. but this is really helpful, I will just remodeled , it accordingly.. thanks..
0

Iterators can only be used with the container that you instanciated them with

2 Comments

thanks for reply, is there another way to erase the element without using an iterators?
You can erase using temp.begin () + pos. So you can loop using int positions and erase that way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.