0
//simply print each elements in a vector
template<class Iterp>
void print_iter(Iterp begin, Iterp end, std::string delim=", ", std::string ending="\n"){
    int len = (end-begin);
    for (size_t i = 0; i < len-1; i++)
    {
        std::cout << (*(begin+i)) << delim;
    }
    if (len>1){
        std::cout << (*(end-1));
    }
    std::cout << ending;
}    

template<class A, class B>
void pr(A first, A end, std::vector<std::vector<B>> &the_result){
    int len = end-first;
    for (size_t a = 0; a < len; a++)
    {
        for (size_t b = 0; b < len; b++)
        {
            for (size_t c = 0; c < len; c++)
            {
                //theres a bunch ways i tried to save these in "the_result" variable, and I ended up with this.
                std::vector<B> result(first, end);
                result.at(a) = (*(first+a));
                result.at(b) = (*(first+b));
                result.at(c) = (*(first+c));
                
                print_iter(result.begin(), result.end());
                //the problem is the "result" variable is not updating using .at() function. So the "result" variable content is still 1, 2, 3.
                

                the_result.push_back(result); //this is not the problem since the "result" variable is still 1, 2, 3

                // std::cout << (*(first+a)) << " " << (*(first+b)) << " " << (*(first+c))
                // But, this one shows it's producing the right permutations.
            }
            
        }
                
    }
    
}
    int main(){
        std::vector<int> test{1, 2, 3}; //tried to create the permutations of this
        int n = 27; //3^3 = 27

        std::vector<std::vector<int>> result(n, std::vector<int>(test.begin(), test.end()));
/*hoping to save all the permutations in "result" variable, each as a vector. ex:
    123
    113
    111
    ...

*/
        pr(test.begin(), test.end(), result);
    }

So I tried to create a function to produce permutation with repeated elements and hoping to save each permutation in a 2-dimensional vector. However, I even cannot update the "result" variable vector using .at() function, pointer element. but when I print it manually, it's showing it's the right answer.

I also explored some other StackOverflow with similar problems, no answer works for me. vector element not updating

Why is my elements in my vector of objects not updating upon calling one of the objects member function?

Dereference vector pointer to access element //tried to use pointer but still doesn't work

Where I made the mistake?

Kevin Agusto
  • 56
  • 1
  • 5
  • 1
    please include a [mcve]. What you posted is just a template. Its not a function and we don't know how you call the function – 463035818_is_not_an_ai Nov 26 '20 at 10:07
  • @idclev463035818 oh yeah sorry, i forgot – Kevin Agusto Nov 26 '20 at 10:12
  • 3
    You never update `the_result` in the function. – Ted Lyngmo Nov 26 '20 at 10:13
  • @TedLyngmo not even the_result variable, the core problem is "result' variable in pr() is not updating – Kevin Agusto Nov 26 '20 at 10:21
  • the code you posted does not produce any output. Are you using a debugger? In that case you need to be specific at what point in the program you observed the values – 463035818_is_not_an_ai Nov 26 '20 at 10:26
  • @idclev463035818 actually I have another function that simply prints each element in a vector. Please try to lookup the content of "result" variable in pr() function – Kevin Agusto Nov 26 '20 at 10:28
  • You set `result` at its final size, and then `push_back` vectors in it. – Damien Nov 26 '20 at 10:32
  • You could use a debugger or just print what you are about to assign, like `std::cout << a << '=' << *(first+a) << ' ' << b << '=' << *(first+b) << ' ' << c << '=' << *(first+c) << '\n';` and it'll become obvious. – Ted Lyngmo Nov 26 '20 at 10:33
  • @TedLyngmo notice that I tried to print the "result" content manually using std::cout <<. The output shows the right answer for me. – Kevin Agusto Nov 26 '20 at 10:43
  • @KevinAgusto Notice that you assign the same values already present in the vector after the initialization: https://gcc.godbolt.org/z/WrM1hb This becomes very clear if you add the debugging line I suggested that you add. – Ted Lyngmo Nov 26 '20 at 16:00

1 Answers1

2

Your logic is flawed:

   result.at(a) = (*(first+a));
   result.at(b) = (*(first+b));
   result.at(c) = (*(first+c));

No matter what are the values of a,b and c, you assign the same numbers to the same elements in each iteraton. They are the same elements you used to initialize result.

For 3 elements you rather need something along the line of:

   result[1] = (*(first+a));
   result[2] = (*(first+b));
   result[3] = (*(first+c));
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185