I am writing an algorithm in order to generate all permutations of an array (or a vector in the context of the provided code).
Second, this is not my code and I'm using it as a learning experience; however, I know that this is a Class that involves the "permute" member in it, and that it still needs a main function in order to be called.
My problem: I cannot find out how to call this class in a way that would take the input "(1,2,3)" and then generate the permutations. I have tried the below method as well as other ways to input the value like [1,2,3] or ({1,2,3}), but non have worked so far.
the error I get is
Too many arguments to function call, expected single argument 'nums', have 3 arguments
Here is the code:
#include <stdio.h>
#include <vector>
class Solution
{
public:
std::vector<std::vector<int> > permute(std::vector<int>& nums)
{
if (nums.size() <= 1)
return { nums };
std::vector<std::vector<int> > result;
for (int i = 0; i < nums.size(); i)
{
std::vector<int> v(nums.begin(), nums.end());
v.erase(v.begin() i);
auto res = permute(v);
for (int j = 0; j < res.size(); j)
{
std::vector<int> _v = res[j];
_v.insert(_v.begin(), nums[j]);
result.push_back(_v);
}
}
return result;
}
};
int main()
{
Solution obj;
obj.permute(1, 2, 3);
return 0;
}
CodePudding user response:
Your function takes the parameter as reference, so you cannot give a temporary value as parameter. The parameter is not modified, so change it to constant:
std::vector<std::vector<int> > permute(const std::vector<int>& nums)
After that you can call the function with a temporary value:
obj.permute({1, 2, 3});
CodePudding user response:
- You supply 3 arguments to
permute
, but it accepts only one. Either call the function like this
or you could add aobj.permute({1, 2, 3});
permute
overload that takes a parameter pack and forwards it, expanded, to your originalpermute
function. The below would make it possible to call it withobj.permute(1, 2, 3);
template<class... Args> std::vector<std::vector<int>> permute(Args&&... args) { return permute({std::forward<Args>(args)...}); }
- You try to bind a non-
const
reference to the supplied temporary argument which isn't allowed. Either take it by value or make it aconst&
. Aconst&
can bind to a temporary.
Example:
#include <iostream>
#include <utility> // std::forward
#include <vector>
class Solution {
public:
std::vector<std::vector<int>> permute(const std::vector<int>& nums) {
// ^^^^^
if(nums.size() <= 1) return {nums};
std::vector<std::vector<int>> result;
for(int i = 0; i < nums.size(); i) {
std::vector<int> v(nums.begin(), nums.end());
v.erase(v.begin() i);
auto res = permute(v);
for(int j = 0; j < res.size(); j) {
std::vector<int> _v = res[j];
_v.insert(_v.begin(), nums[j]);
result.push_back(_v);
}
}
return result;
}
// Added overload:
template<class... Args>
std::vector<std::vector<int>> permute(Args&&... args) {
return permute({std::forward<Args>(args)...});
}
};
int main() {
Solution obj;
auto res = obj.permute({1, 2, 3}); // note: {1,2,3}
// auto res = obj.permute(1, 2, 3); // would use the added overload
for(auto& inner : res) {
for(auto v : inner) std::cout << ' ' << v;
std::cout << '\n';
}
}