0

I've been practicing C++ and messing around with different coding samples. I have been able to make a list of all permutation of integers, but I would like to expand it to any vector of any item. I tried to use templates, in fact all I did was define the template and replace int with T, as below, but I must be messing up syntax somehow, as I am getting errors on build.

As I understand, when I pass a template function, the type of the parameters defines the type of the template, effectively replacing all the instances of type T in my template <typename T> with that inferred type. So, in passing add_combinations_to(vector<int>, vector<vector<int>>::iterator, vector<vector<int>>::iterator, vector<vector<int>>) I expext T to be replaced by int and yet the compiler does not understand.

template <typename T>
void add_combinations_to(vector<T> prefix, vector<vector<T>>::iterator start, vector<vector<T>>::iterator stop, vector<vector<T>>& ret) {
    if(start == stop) {ret.push_back(prefix); return;}
    for(auto v : *start) {
        auto next = prefix;
        next.push_back(v);
        add_combinations_to(next, start + 1, stop, ret);
    }
};

int main() {
    vector<vector<int>> v{{1,2},{3,4}};
    vector<vector<int>> ret;
    vector<int> init{};
    add_combinations_to(init, v.begin(),v.end(),ret);
    // ret should be: {{1, 3},{1, 4},{2, 3},{2, 4}}
}

Errors:

C:\Users\Peter\CLionProjects\CodingPractice\main.cpp:65:63: error: 'std::vector<std::vector<_RealType, std::allocator<_CharT> >, std::allocator<std::vector<_RealType, std::allocator<_CharT> > > >::iterator' is not a type
 void add_combinations_to(vector<T> prefix, vector<vector<T>>::iterator start, vector<vector<T>>::iterator stop, vector<vector<T>>& ret) {
                                                               ^
C:\Users\Peter\CLionProjects\CodingPractice\main.cpp:65:98: error: 'std::vector<std::vector<_RealType, std::allocator<_CharT> >, std::allocator<std::vector<_RealType, std::allocator<_CharT> > > >::iterator' is not a type
 void add_combinations_to(vector<T> prefix, vector<vector<T>>::iterator start, vector<vector<T>>::iterator stop, vector<vector<T>>& ret) {
                                                                                                  ^
C:\Users\Peter\CLionProjects\CodingPractice\main.cpp: In function 'void add_combinations_to(std::vector<_RealType>, int, int, std::vector<std::vector<_RealType> >&)':
C:\Users\Peter\CLionProjects\CodingPractice\main.cpp:67:19: error: invalid type argument of unary '*' (have 'int')
     for(auto v : *start) {
                   ^
C:\Users\Peter\CLionProjects\CodingPractice\main.cpp: In function 'int main()':
C:\Users\Peter\CLionProjects\CodingPractice\main.cpp:79:52: error: no matching function for call to 'add_combinations_to(std::vector<int>&, std::vector<std::vector<int> >::iterator, std::vector<std::vector<int> >::iterator, std::vector<std::vector<int> >&)'
     add_combinations_to(init, v.begin(),v.end(),ret);
                                                    ^
C:\Users\Peter\CLionProjects\CodingPractice\main.cpp:65:6: note: candidate: template<class T> void add_combinations_to(std::vector<_RealType>, int, int, std::vector<std::vector<_RealType> >&)
 void add_combinations_to(vector<T> prefix, vector<vector<T>>::iterator start, vector<vector<T>>::iterator stop, vector<vector<T>>& ret) {
      ^
C:\Users\Peter\CLionProjects\CodingPractice\main.cpp:65:6: note:   template argument deduction/substitution failed:
C:\Users\Peter\CLionProjects\CodingPractice\main.cpp:79:38: note:   cannot convert 'v.std::vector<_Tp, _Alloc>::begin<std::vector<int>, std::allocator<std::vector<int> > >()' (type 'std::vector<std::vector<int> >::iterator {aka __gnu_cxx::__normal_iterator<std::vector<int>*, std::vector<std::vector<int> > >}') to type 'int'
     add_combinations_to(init, v.begin(),v.end(),ret);

Anyone able to provide help for why this is happening, and how I could get this to work in a general case?

Peter Moran
  • 295
  • 3
  • 13

0 Answers0