You only have one rest
vector (and only one sofar
vector) because you are passing by reference. That means that when you remove an element from rest
, it's gone. You never put it back, so it's gone forever. (In fact, you're removing elements from the vector passed as an argument to permute
. Some would say that modifying the argument is poor interface design.)
You probably want to pass the parameter vectors by value (other than ans
, which accumulates results and thus should be permanently modified). Of course, passing by value makes copies, which introduces an unnecessary quadratic complexity, but it will allow the algorithm to work as expected.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…