Currently I am trying to generate combinations from a vector that contains some integers. For now I want it to print out all of the combinations that are of length to_generate.
I found this code at combination and permutation in C++ that uses std::next_permutation to generate combinations. The next_combination code is as follows:
template<class RandIt, class Compare>
bool next_combination(RandIt first, RandIt mid, RandIt last, Compare comp)
{
std::sort(mid, last, std::tr1::bind(comp, std::tr1::placeholders::_2
, std::tr1::placeholders::_1));
return std::next_permutation(first, last, comp);
}
I tried to call it like this:
bool mycomp (int c1, int c2)
{ return (c1)<(c2); }
void test_combos(int to_generate){
std::vector<int> combo(30);
for(int i=0;i<30;++i){
combo.push_back(i);
}
while(next_combination<std::vector<int>::iterator, bool>(combo.begin(),combo.begin()+to_generate,combo.end(),mycomp)){
for(std::vector<int>::iterator iter = combo.begin(); iter != combo.end() ; ++iter){
std::cout << *iter << " ";
}
}
}
but I get a huge stl garbled error message. Is there anything really obvious going wrong here?
Which headers can I find std::placeholder?
As requested, here is the error message:
$ make
g++ -g -std=c++0x -I /usr/lib/ -c equity.cpp
In file included from /usr/include/c++/4.4/algorithm:62,
from misc.hpp:6,
from equity.cpp:3:
/usr/include/c++/4.4/bits/stl_algo.h: In function ‘bool std::next_permutation(_BIter, _BIter, _Compare) [with _BIter = __gnu_cxx::__normal_iterator > >, _Compare = bool]’:
misc.hpp:15: instantiated from ‘bool next_combination(RandIt, RandIt, RandIt, Compare) [with RandIt = __gnu_cxx::__normal_iterator > >, Compare = bool]’
equity.cpp:28: instantiated from here
/usr/include/c++/4.4/bits/stl_algo.h:3655: error: ‘__comp’ cannot be used as a function
/usr/include/c++/4.4/bits/stl_algo.h:3658: error: ‘__comp’ cannot be used as a function
In file included from /usr/include/c++/4.4/functional:70,
from /usr/include/c++/4.4/memory:70,
from /usr/include/boost/config/no_tr1/memory.hpp:21,
from /usr/include/boost/get_pointer.hpp:12,
from /usr/include/boost/bind/mem_fn.hpp:25,
from /usr/include/boost/mem_fn.hpp:22,
from /usr/include/boost/bind/bind.hpp:26,
from /usr/include/boost/bind.hpp:22,
from misc.hpp:4,
from equity.cpp:3:
/usr/include/c++/4.4/tr1_impl/functional: At global scope:
/usr/include/c++/4.4/tr1_impl/functional: In instantiation of ‘std::_Result_of_impl’:
/usr/include/c++/4.4/tr1_impl/functional:149: instantiated from ‘std::result_of’
/usr/include/c++/4.4/bits/stl_algo.h:124: instantiated from ‘const _Tp& std::__median(const _Tp&, const _Tp&, const _Tp&, _Compare) [with _Tp = int, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_algo.h:2301: instantiated from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator > >, _Size = long int, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_algo.h:5258: instantiated from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator > >, _Compare = std::_Bind, std::_Placeholder)>]’
misc.hpp:13: instantiated from ‘bool next_combination(RandIt, RandIt, RandIt, Compare) [with RandIt = __gnu_cxx::__normal_iterator > >, Compare = bool]’
equity.cpp:28: instantiated from here
/usr/include/c++/4.4/tr1_impl/functional:195: error: ‘const volatile bool’ is not a class, struct, or union type
/usr/include/c++/4.4/tr1_impl/functional: In instantiation of ‘std::_Result_of_impl’:
/usr/include/c++/4.4/tr1_impl/functional:149: instantiated from ‘std::result_of’
/usr/include/c++/4.4/bits/stl_algo.h:124: instantiated from ‘const _Tp& std::__median(const _Tp&, const _Tp&, const _Tp&, _Compare) [with _Tp = int, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_algo.h:2301: instantiated from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator > >, _Size = long int, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_algo.h:5258: instantiated from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator > >, _Compare = std::_Bind, std::_Placeholder)>]’
misc.hpp:13: instantiated from ‘bool next_combination(RandIt, RandIt, RandIt, Compare) [with RandIt = __gnu_cxx::__normal_iterator > >, Compare = bool]’
equity.cpp:28: instantiated from here
/usr/include/c++/4.4/tr1_impl/functional:195: error: ‘volatile bool’ is not a class, struct, or union type
/usr/include/c++/4.4/tr1_impl/functional: In instantiation of ‘std::_Result_of_impl’:
/usr/include/c++/4.4/tr1_impl/functional:149: instantiated from ‘std::result_of’
/usr/include/c++/4.4/bits/stl_algo.h:124: instantiated from ‘const _Tp& std::__median(const _Tp&, const _Tp&, const _Tp&, _Compare) [with _Tp = int, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_algo.h:2301: instantiated from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator > >, _Size = long int, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_algo.h:5258: instantiated from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator > >, _Compare = std::_Bind, std::_Placeholder)>]’
misc.hpp:13: instantiated from ‘bool next_combination(RandIt, RandIt, RandIt, Compare) [with RandIt = __gnu_cxx::__normal_iterator > >, Compare = bool]’
equity.cpp:28: instantiated from here
/usr/include/c++/4.4/tr1_impl/functional:195: error: ‘const bool’ is not a class, struct, or union type
/usr/include/c++/4.4/tr1_impl/functional: In instantiation of ‘std::_Result_of_impl’:
/usr/include/c++/4.4/tr1_impl/functional:149: instantiated from ‘std::result_of’
/usr/include/c++/4.4/bits/stl_algo.h:124: instantiated from ‘const _Tp& std::__median(const _Tp&, const _Tp&, const _Tp&, _Compare) [with _Tp = int, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_algo.h:2301: instantiated from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator > >, _Size = long int, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_algo.h:5258: instantiated from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator > >, _Compare = std::_Bind, std::_Placeholder)>]’
misc.hpp:13: instantiated from ‘bool next_combination(RandIt, RandIt, RandIt, Compare) [with RandIt = __gnu_cxx::__normal_iterator > >, Compare = bool]’
equity.cpp:28: instantiated from here
/usr/include/c++/4.4/tr1_impl/functional:195: error: ‘bool’ is not a class, struct, or union type
In file included from /usr/include/c++/4.4/algorithm:62,
from misc.hpp:6,
from equity.cpp:3:
/usr/include/c++/4.4/bits/stl_algo.h: In function ‘const _Tp& std::__median(const _Tp&, const _Tp&, const _Tp&, _Compare) [with _Tp = int, _Compare = std::_Bind, std::_Placeholder)>]’:
/usr/include/c++/4.4/bits/stl_algo.h:2301: instantiated from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator > >, _Size = long int, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_algo.h:5258: instantiated from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator > >, _Compare = std::_Bind, std::_Placeholder)>]’
misc.hpp:13: instantiated from ‘bool next_combination(RandIt, RandIt, RandIt, Compare) [with RandIt = __gnu_cxx::__normal_iterator > >, Compare = bool]’
equity.cpp:28: instantiated from here
/usr/include/c++/4.4/bits/stl_algo.h:124: error: no match for call to ‘(std::_Bind, std::_Placeholder)>) (const int&, const int&)’
/usr/include/c++/4.4/bits/stl_algo.h:125: error: no match for call to ‘(std::_Bind, std::_Placeholder)>) (const int&, const int&)’
/usr/include/c++/4.4/bits/stl_algo.h:127: error: no match for call to ‘(std::_Bind, std::_Placeholder)>) (const int&, const int&)’
/usr/include/c++/4.4/bits/stl_algo.h:131: error: no match for call to ‘(std::_Bind, std::_Placeholder)>) (const int&, const int&)’
/usr/include/c++/4.4/bits/stl_algo.h:133: error: no match for call to ‘(std::_Bind, std::_Placeholder)>) (const int&, const int&)’
In file included from /usr/include/c++/4.4/functional:70,
from /usr/include/c++/4.4/memory:70,
from /usr/include/boost/config/no_tr1/memory.hpp:21,
from /usr/include/boost/get_pointer.hpp:12,
from /usr/include/boost/bind/mem_fn.hpp:25,
from /usr/include/boost/mem_fn.hpp:22,
from /usr/include/boost/bind/bind.hpp:26,
from /usr/include/boost/bind.hpp:22,
from misc.hpp:4,
from equity.cpp:3:
/usr/include/c++/4.4/tr1_impl/functional: At global scope:
/usr/include/c++/4.4/tr1_impl/functional: In instantiation of ‘std::_Result_of_impl’:
/usr/include/c++/4.4/tr1_impl/functional:149: instantiated from ‘std::result_of’
/usr/include/c++/4.4/bits/stl_algo.h:2230: instantiated from ‘_RandomAccessIterator std::__unguarded_partition(_RandomAccessIterator, _RandomAccessIterator, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator > >, _Tp = int, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_algo.h:2301: instantiated from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator > >, _Size = long int, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_algo.h:5258: instantiated from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator > >, _Compare = std::_Bind, std::_Placeholder)>]’
misc.hpp:13: instantiated from ‘bool next_combination(RandIt, RandIt, RandIt, Compare) [with RandIt = __gnu_cxx::__normal_iterator > >, Compare = bool]’
equity.cpp:28: instantiated from here
/usr/include/c++/4.4/tr1_impl/functional:195: error: ‘const volatile bool’ is not a class, struct, or union type
/usr/include/c++/4.4/tr1_impl/functional: In instantiation of ‘std::_Result_of_impl’:
/usr/include/c++/4.4/tr1_impl/functional:149: instantiated from ‘std::result_of’
/usr/include/c++/4.4/bits/stl_algo.h:2230: instantiated from ‘_RandomAccessIterator std::__unguarded_partition(_RandomAccessIterator, _RandomAccessIterator, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator > >, _Tp = int, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_algo.h:2301: instantiated from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator > >, _Size = long int, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_algo.h:5258: instantiated from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator > >, _Compare = std::_Bind, std::_Placeholder)>]’
misc.hpp:13: instantiated from ‘bool next_combination(RandIt, RandIt, RandIt, Compare) [with RandIt = __gnu_cxx::__normal_iterator > >, Compare = bool]’
equity.cpp:28: instantiated from here
/usr/include/c++/4.4/tr1_impl/functional:195: error: ‘volatile bool’ is not a class, struct, or union type
/usr/include/c++/4.4/tr1_impl/functional: In instantiation of ‘std::_Result_of_impl’:
/usr/include/c++/4.4/tr1_impl/functional:149: instantiated from ‘std::result_of’
/usr/include/c++/4.4/bits/stl_algo.h:2230: instantiated from ‘_RandomAccessIterator std::__unguarded_partition(_RandomAccessIterator, _RandomAccessIterator, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator > >, _Tp = int, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_algo.h:2301: instantiated from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator > >, _Size = long int, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_algo.h:5258: instantiated from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator > >, _Compare = std::_Bind, std::_Placeholder)>]’
misc.hpp:13: instantiated from ‘bool next_combination(RandIt, RandIt, RandIt, Compare) [with RandIt = __gnu_cxx::__normal_iterator > >, Compare = bool]’
equity.cpp:28: instantiated from here
/usr/include/c++/4.4/tr1_impl/functional:195: error: ‘const bool’ is not a class, struct, or union type
/usr/include/c++/4.4/tr1_impl/functional: In instantiation of ‘std::_Result_of_impl’:
/usr/include/c++/4.4/tr1_impl/functional:149: instantiated from ‘std::result_of’
/usr/include/c++/4.4/bits/stl_algo.h:2230: instantiated from ‘_RandomAccessIterator std::__unguarded_partition(_RandomAccessIterator, _RandomAccessIterator, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator > >, _Tp = int, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_algo.h:2301: instantiated from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator > >, _Size = long int, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_algo.h:5258: instantiated from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator > >, _Compare = std::_Bind, std::_Placeholder)>]’
misc.hpp:13: instantiated from ‘bool next_combination(RandIt, RandIt, RandIt, Compare) [with RandIt = __gnu_cxx::__normal_iterator > >, Compare = bool]’
equity.cpp:28: instantiated from here
/usr/include/c++/4.4/tr1_impl/functional:195: error: ‘bool’ is not a class, struct, or union type
In file included from /usr/include/c++/4.4/algorithm:62,
from misc.hpp:6,
from equity.cpp:3:
/usr/include/c++/4.4/bits/stl_algo.h: In function ‘_RandomAccessIterator std::__unguarded_partition(_RandomAccessIterator, _RandomAccessIterator, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator > >, _Tp = int, _Compare = std::_Bind, std::_Placeholder)>]’:
/usr/include/c++/4.4/bits/stl_algo.h:2301: instantiated from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator > >, _Size = long int, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_algo.h:5258: instantiated from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator > >, _Compare = std::_Bind, std::_Placeholder)>]’
misc.hpp:13: instantiated from ‘bool next_combination(RandIt, RandIt, RandIt, Compare) [with RandIt = __gnu_cxx::__normal_iterator > >, Compare = bool]’
equity.cpp:28: instantiated from here
/usr/include/c++/4.4/bits/stl_algo.h:2230: error: no match for call to ‘(std::_Bind, std::_Placeholder)>) (int&, int&)’
/usr/include/c++/4.4/bits/stl_algo.h:2233: error: no match for call to ‘(std::_Bind, std::_Placeholder)>) (int&, int&)’
/usr/include/c++/4.4/bits/stl_algo.h: In function ‘void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator > >, _Compare = std::_Bind, std::_Placeholder)>]’:
/usr/include/c++/4.4/bits/stl_algo.h:2193: instantiated from ‘void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator > >, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_algo.h:5260: instantiated from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator > >, _Compare = std::_Bind, std::_Placeholder)>]’
misc.hpp:13: instantiated from ‘bool next_combination(RandIt, RandIt, RandIt, Compare) [with RandIt = __gnu_cxx::__normal_iterator > >, Compare = bool]’
equity.cpp:28: instantiated from here
/usr/include/c++/4.4/bits/stl_algo.h:2128: error: no match for call to ‘(std::_Bind, std::_Placeholder)>) (int&, int&)’
/usr/include/c++/4.4/bits/stl_algo.h: In function ‘void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator > >, _Compare = std::_Bind, std::_Placeholder)>]’:
/usr/include/c++/4.4/bits/stl_algo.h:5108: instantiated from ‘void std::partial_sort(_RAIter, _RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator > >, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_algo.h:2288: instantiated from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator > >, _Size = long int, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_algo.h:5258: instantiated from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator > >, _Compare = std::_Bind, std::_Placeholder)>]’
misc.hpp:13: instantiated from ‘bool next_combination(RandIt, RandIt, RandIt, Compare) [with RandIt = __gnu_cxx::__normal_iterator > >, Compare = bool]’
equity.cpp:28: instantiated from here
/usr/include/c++/4.4/bits/stl_algo.h:1919: error: no match for call to ‘(std::_Bind, std::_Placeholder)>) (int&, int&)’
/usr/include/c++/4.4/bits/stl_algo.h: In function ‘void std::__unguarded_linear_insert(_RandomAccessIterator, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator > >, _Tp = int, _Compare = std::_Bind, std::_Placeholder)>]’:
/usr/include/c++/4.4/bits/stl_algo.h:2134: instantiated from ‘void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator > >, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_algo.h:2193: instantiated from ‘void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator > >, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_algo.h:5260: instantiated from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator > >, _Compare = std::_Bind, std::_Placeholder)>]’
misc.hpp:13: instantiated from ‘bool next_combination(RandIt, RandIt, RandIt, Compare) [with RandIt = __gnu_cxx::__normal_iterator > >, Compare = bool]’
equity.cpp:28: instantiated from here
/usr/include/c++/4.4/bits/stl_algo.h:2084: error: no match for call to ‘(std::_Bind, std::_Placeholder)>) (int&, int&)’
In file included from /usr/include/c++/4.4/bits/stl_algo.h:62,
from /usr/include/c++/4.4/algorithm:62,
from misc.hpp:6,
from equity.cpp:3:
/usr/include/c++/4.4/bits/stl_heap.h: In function ‘void std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator > >, _Distance = long int, _Tp = int, _Compare = std::_Bind, std::_Placeholder)>]’:
/usr/include/c++/4.4/bits/stl_heap.h:434: instantiated from ‘void std::make_heap(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator > >, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_algo.h:1917: instantiated from ‘void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator > >, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_algo.h:5108: instantiated from ‘void std::partial_sort(_RAIter, _RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator > >, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_algo.h:2288: instantiated from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator > >, _Size = long int, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_algo.h:5258: instantiated from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator > >, _Compare = std::_Bind, std::_Placeholder)>]’
misc.hpp:13: instantiated from ‘bool next_combination(RandIt, RandIt, RandIt, Compare) [with RandIt = __gnu_cxx::__normal_iterator > >, Compare = bool]’
equity.cpp:28: instantiated from here
/usr/include/c++/4.4/bits/stl_heap.h:303: error: no match for call to ‘(std::_Bind, std::_Placeholder)>) (int&, int&)’
/usr/include/c++/4.4/bits/stl_heap.h: In function ‘void std::__push_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator > >, _Distance = long int, _Tp = int, _Compare = std::_Bind, std::_Placeholder)>]’:
/usr/include/c++/4.4/bits/stl_heap.h:316: instantiated from ‘void std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator > >, _Distance = long int, _Tp = int, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_heap.h:434: instantiated from ‘void std::make_heap(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator > >, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_algo.h:1917: instantiated from ‘void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator > >, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_algo.h:5108: instantiated from ‘void std::partial_sort(_RAIter, _RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator > >, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_algo.h:2288: instantiated from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator > >, _Size = long int, _Compare = std::_Bind, std::_Placeholder)>]’
/usr/include/c++/4.4/bits/stl_algo.h:5258: instantiated from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator > >, _Compare = std::_Bind, std::_Placeholder)>]’
misc.hpp:13: instantiated from ‘bool next_combination(RandIt, RandIt, RandIt, Compare) [with RandIt = __gnu_cxx::__normal_iterator > >, Compare = bool]’
equity.cpp:28: instantiated from here
/usr/include/c++/4.4/bits/stl_heap.h:180: error: no match for call to ‘(std::_Bind, std::_Placeholder)>) (int&, int&)’
make: *** [equity.o] Error 1
The second template argument shouldn't be bool. You can allow compiller to handle types and simply write:
while(next_combination(combo.begin(), combo.begin() + to_generate, combo.end(), std::less<int>()))
for(std::vector<int>::iterator iter = combo.begin(); iter != combo.end() ; ++iter)
std::cout << *iter << " ";
And use spaces - it makes code look better.
Related
I am making a class named MyClass, in which a member function uses C++ sort() function, and I pass another member function cmp to it as an argument.
#include <algorithm>
using namespace std;
/* If I use this cmp for sort(), there is no error.
bool cmp(int& x, int& y) {
return x < y;
}
*/
class MyClass {
public:
/* Or I use this cmp, there is no error too.
static bool cmp(int& x, int& y) {
return x < y;
}
*/
// compile failed.
bool cmp (int& x, int& y) {
return x < y;
}
void mySort(vector<int>& v) {
sort(v.begin(), v.end(), cmp);
}
};
(I know the cmp function is redundant actually, but it doesn't matter for showing the error).
Then the compiler gives following error message:
In file included from C:/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algobase.h:71,
from C:/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/char_traits.h:39,
from C:/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ios:40,
from C:/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ostream:38,
from C:/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/iostream:39,
from Untitled.cpp:1:
C:/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/predefined_ops.h: In instantiation of 'constexpr bool __gnu_cxx::__ops::_Iter_comp_iter<_Compare>::operator()(_Iterator1, _Iterator2) [with _Iterator1 = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Iterator2 = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Compare = bool (MyClass::*)(const int&, const int&)]':
C:/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:81:17: required from 'void std::__move_median_to_first(_Iterator, _Iterator, _Iterator, _Iterator, _Compare) [with _Iterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (MyClass::*)(const int&, const int&)>]'
C:/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:1921:34: required from '_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (MyClass::*)(const int&, const int&)>]'
C:/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:1953:38: required from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Size = long
long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (MyClass::*)(const int&, const int&)>]'
C:/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:1968:25: required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (MyClass::*)(const int&, const int&)>]'
C:/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:4866:18: required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Compare = bool (MyClass::*)(const int&, const int&)]'
Untitled.cpp:28:37: required from here
C:/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/predefined_ops.h:143:18: error: must use '.*' or '->*' to call pointer-to-member function in '((__gnu_cxx::__ops::_Iter_comp_iter<bool (MyClass::*)(const int&, const int&)>*)this)->__gnu_cxx::__ops::_Iter_comp_iter<bool (MyClass::*)(const int&, const int&)>::_M_comp (...)', e.g. '(... ->* ((__gnu_cxx::__ops::_Iter_comp_iter<bool (MyClass::*)(const int&, const int&)>*)this)->__gnu_cxx::__ops::_Iter_comp_iter<bool (MyClass::*)(const int&, const int&)>::_M_comp) (...)'
{ return bool(_M_comp(*__it1, *__it2)); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Val_comp_iter<_Compare>::operator()(_Value&, _Iterator) [with _Value = int; _Iterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Compare = bool (MyClass::*)(const int&, const int&)]':
C:/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:1828:20: required from 'void std::__unguarded_linear_insert(_RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Compare = __gnu_cxx::__ops::_Val_comp_iter<bool (MyClass::*)(const int&, const int&)>]'
C:/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:1855:36: required from 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (MyClass::*)(const int&, const int&)>]'
C:/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:1885:25: required from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (MyClass::*)(const int&, const int&)>]'
C:/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:1971:31: required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (MyClass::*)(const int&, const int&)>]'
C:/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:4866:18: required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Compare = bool (MyClass::*)(const int&, const int&)]'
Untitled.cpp:28:37: required from here
C:/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/predefined_ops.h:215:11: error: must use '.*' or '->*' to call pointer-to-member function in '((__gnu_cxx::__ops::_Val_comp_iter<bool (MyClass::*)(const int&, const int&)>*)this)->__gnu_cxx::__ops::_Val_comp_iter<bool (MyClass::*)(const int&, const int&)>::_M_comp (...)', e.g. '(... ->* ((__gnu_cxx::__ops::_Val_comp_iter<bool (MyClass::*)(const int&, const int&)>*)this)->__gnu_cxx::__ops::_Val_comp_iter<bool (MyClass::*)(const int&, const int&)>::_M_comp) (...)'
{ return bool(_M_comp(__val, *__it)); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~
C:/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Iter_comp_val<_Compare>::operator()(_Iterator, _Value&) [with _Iterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Value = int; _Compare = bool (MyClass::*)(const int&, const int&)]':
C:/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_heap.h:133:48: required from 'void std::__push_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Distance = long long int; _Tp = int; _Compare = __gnu_cxx::__ops::_Iter_comp_val<bool (MyClass::*)(const int&, const int&)>]'
C:/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_heap.h:237:23: required from 'void std::__adjust_heap(_RandomAccessIterator,
_Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Distance = long long int; _Tp = int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (MyClass::*)(const int&, const int&)>]'
C:/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_heap.h:342:22: required from 'void std::__make_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (MyClass::*)(const int&, const int&)>]'
C:/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:1672:23: required from 'void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >;
_Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (MyClass::*)(const int&, const int&)>]'
C:/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:1933:25: required from 'void std::__partial_sort(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (MyClass::*)(const int&, const int&)>]'
C:/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:1948:27: required from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Size = long
long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (MyClass::*)(const int&, const int&)>]'
C:/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:1968:25: required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (MyClass::*)(const int&, const int&)>]'
C:/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:4866:18: required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Compare = bool (MyClass::*)(const int&, const int&)]'
Untitled.cpp:28:37: required from here
C:/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/predefined_ops.h:177:11: error: must use '.*' or '->*' to call pointer-to-member function in '((__gnu_cxx::__ops::_Iter_comp_val<bool (MyClass::*)(const int&, const int&)>*)this)->__gnu_cxx::__ops::_Iter_comp_val<bool (MyClass::*)(const int&, const int&)>::_M_comp (...)', e.g. '(... ->* ((__gnu_cxx::__ops::_Iter_comp_val<bool (MyClass::*)(const int&, const int&)>*)this)->__gnu_cxx::__ops::_Iter_comp_val<bool (MyClass::*)(const int&, const int&)>::_M_comp) (...)'
{ return bool(_M_comp(*__it, __val)); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~
I also find that if I make the cmp function static, or I move it outside the class, everything will be OK. Why does this happen? What is the difference between these cmp functions?
A non-static member function needs an object to called on. So in fact, MyClass::cmp function takes three arguments, the third one(in fact, it's the first one) is this.
A possible solution for your problem is using lambda expressions.
It could be written as follows.
std::sort(v.begin(), v.end(), [this](auto a, auto b){ return this->cmp(a, b);});
Or you could use std::bind.
using namespace std::placeholders;
std::sort(v.begin(), v.end(), std::bind(&MyClass::cmp, this, _1, _2));
Let the code speak it:
auto SetCompare = [](const string& a, const string& b)
{
size_t L = a.length();
size_t R = b.length();
if (L == R) return (a < b);
return L < R;
};
using MySet = std::set<string, decltype(SetCompare)>;
unordered_map<string, MySet> Map;
The insertion or access won't work:
Map["abc"];
Map["xyz"].insert("mapped to xyz");
// Insert TO set works
MySet mySet(SetCompare); // HINT
mySet.insert("x");
mySet.insert("abc");
// But not to the map!
Map.insert({"pqr", mySet});
At the HINTed location, I pass SetCompare lambda (not just type) to MySet's constructor. The question is how to pass it to the "Value" type of unordered_map?
EDIT (Compiler errors):
GCC C++17
In file included from /usr/include/c++/7/set:60:0,
from main.cpp:10:
/usr/include/c++/7/bits/stl_tree.h: In instantiation of ‘std::_Rb_tree_key_compare<_Key_compare>::_Rb_tree_key_compare() [with _Key_compare = main()::<lambda(const string&, const string&)>]’:
/usr/include/c++/7/bits/stl_tree.h:688:4: required from ‘std::pair<_T1, _T2>::pair(std::tuple<_Args1 ...>&, std::tuple<_Args2 ...>&, std::_Index_tuple<_Indexes1 ...>, std::_Index_tuple<_Indexes2 ...>) [with _Args1 = {char&&}; long unsigned int ..._Indexes1 = {0}; _Args2 = {}; long unsigned int ..._Indexes2 = {}; _T1 = const char; _T2 = std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> >]’
/usr/include/c++/7/tuple:1641:63: required from ‘std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>) [with _Args1 = {char&&}; _Args2 = {}; _T1 = const char; _T2 = std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> >]’
/usr/include/c++/7/ext/new_allocator.h:136:4: required from ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > >; _Args = {const std::piecewise_construct_t&, std::tuple<char&&>, std::tuple<>}; _Tp = std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > >]’
/usr/include/c++/7/bits/alloc_traits.h:475:4: required from ‘static void std::allocator_traits<std::allocator<_CharT> >::construct(std::allocator_traits<std::allocator<_CharT> >::allocator_type&, _Up*, _Args&& ...) [with _Up = std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > >; _Args = {const std::piecewise_construct_t&, std::tuple<char&&>, std::tuple<>}; _Tp = std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > >; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > > >]’
/usr/include/c++/7/bits/hashtable_policy.h:2066:37: required from ‘std::__detail::_Hashtable_alloc<_NodeAlloc>::__node_type* std::__detail::_Hashtable_alloc<_NodeAlloc>::_M_allocate_node(_Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<char&&>, std::tuple<>}; _NodeAlloc = std::allocator<std::__detail::_Hash_node<std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > >, false> >; std::__detail::_Hashtable_alloc<_NodeAlloc>::__node_type = std::__detail::_Hash_node<std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > >, false>]’
/usr/include/c++/7/bits/hashtable_policy.h:750:8: required from ‘std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::mapped_type& std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::operator[](std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::key_type&&) [with _Key = char; _Pair = std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > >; _Alloc = std::allocator<std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > > >; _Equal = std::equal_to<char>; _H1 = std::hash<char>; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<false, false, true>; std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::mapped_type = std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> >; std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::key_type = char]’
/usr/include/c++/7/bits/unordered_map.h:977:20: required from ‘std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::mapped_type& std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::operator[](std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::key_type&&) [with _Key = char; _Tp = std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> >; _Hash = std::hash<char>; _Pred = std::equal_to<char>; _Alloc = std::allocator<std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > > >; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::mapped_type = std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> >; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::key_type = char]’
<span class="error_line" onclick="ide.gotoLine('main.cpp',31)">main.cpp:31:7</span>: required from here
/usr/include/c++/7/bits/stl_tree.h:149:24: error: use of deleted function ‘main()::::()’
: _M_key_compare()
^
main.cpp:17:24: note: a lambda closure type has a deleted default constructor
auto SetCompare = [](const string& a, const string& b)
GCC C++17
In file included from /usr/include/c++/6/set:60:0,
from main.cpp:10:
/usr/include/c++/6/bits/stl_tree.h: In instantiation of ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Rb_tree_impl<_Key_compare, <anonymous> >::_Rb_tree_impl() [with _Key_compare = main()::<lambda(const string&, const string&)>; bool <anonymous> = false; _Key = std::basic_string<char>; _Val = std::basic_string<char>; _KeyOfValue = std::_Identity<std::basic_string<char> >; _Compare = main()::<lambda(const string&, const string&)>; _Alloc = std::allocator<std::basic_string<char> >]’:
/usr/include/c++/6/bits/stl_tree.h:821:18: required from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Rb_tree() [with _Key = std::basic_string<char>; _Val = std::basic_string<char>; _KeyOfValue = std::_Identity<std::basic_string<char> >; _Compare = main()::<lambda(const string&, const string&)>; _Alloc = std::allocator<std::basic_string<char> >]’
/usr/include/c++/6/bits/stl_set.h:146:14: required from ‘std::set<_Key, _Compare, _Alloc>::set() [with _Key = std::basic_string<char>; _Compare = main()::<lambda(const string&, const string&)>; _Alloc = std::allocator<std::basic_string<char> >]’
/usr/include/c++/6/tuple:1590:70: required from ‘std::pair<_T1, _T2>::pair(std::tuple<_Args1 ...>&, std::tuple<_Args2 ...>&, std::_Index_tuple<_Indexes1 ...>, std::_Index_tuple<_Indexes2 ...>) [with _Args1 = {char&&}; long unsigned int ..._Indexes1 = {0ul}; _Args2 = {}; long unsigned int ..._Indexes2 = {}; _T1 = const char; _T2 = std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> >]’
/usr/include/c++/6/tuple:1579:63: required from ‘std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>) [with _Args1 = {char&&}; _Args2 = {}; _T1 = const char; _T2 = std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> >]’
/usr/include/c++/6/ext/new_allocator.h:120:4: required from ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > >; _Args = {const std::piecewise_construct_t&, std::tuple<char&&>, std::tuple<>}; _Tp = std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > >]’
/usr/include/c++/6/bits/alloc_traits.h:475:4: required from ‘static void std::allocator_traits<std::allocator<_CharT> >::construct(std::allocator_traits<std::allocator<_CharT> >::allocator_type&, _Up*, _Args&& ...) [with _Up = std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > >; _Args = {const std::piecewise_construct_t&, std::tuple<char&&>, std::tuple<>}; _Tp = std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > >; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > > >]’
/usr/include/c++/6/bits/hashtable_policy.h:1953:37: required from ‘std::__detail::_Hashtable_alloc<_NodeAlloc>::__node_type* std::__detail::_Hashtable_alloc<_NodeAlloc>::_M_allocate_node(_Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<char&&>, std::tuple<>}; _NodeAlloc = std::allocator<std::__detail::_Hash_node<std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > >, false> >; std::__detail::_Hashtable_alloc<_NodeAlloc>::__node_type = std::__detail::_Hash_node<std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > >, false>]’
/usr/include/c++/6/bits/hashtable_policy.h:620:8: required from ‘std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::mapped_type& std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::operator[](std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::key_type&&) [with _Key = char; _Pair = std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > >; _Alloc = std::allocator<std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > > >; _Equal = std::equal_to<char>; _H1 = std::hash<char>; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<false, false, true>; std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::mapped_type = std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> >; std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::key_type = char]’
/usr/include/c++/6/bits/unordered_map.h:908:20: required from ‘std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::mapped_type& std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::operator[](std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::key_type&&) [with _Key = char; _Tp = std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> >; _Hash = std::hash<char>; _Pred = std::equal_to<char>; _Alloc = std::allocator<std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > > >; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::mapped_type = std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> >; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::key_type = char]’
<span class="error_line" onclick="ide.gotoLine('main.cpp',31)">main.cpp:31:7</span>: required from here
/usr/include/c++/6/bits/stl_tree.h:602:21: error: use of deleted function ‘main()::::()’
_M_node_count(0)
^
main.cpp:17:24: note: a lambda closure type has a deleted default constructor
auto SetCompare = [](const string& a, const string& b)
^
In file included from /usr/include/c++/6/set:60:0,
from main.cpp:10:
/usr/include/c++/6/bits/stl_tree.h:628:4: warning: ‘void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Rb_tree_impl<_Key_compare, >::_M_initialize() [with _Key_compare = main()::; bool = false; _Key = std::basic_string; _Val = std::basic_string; _KeyOfValue = std::_Identity >; _Compare = main()::; _Alloc = std::allocator >]’ used but never defined
_M_initialize()
^~~~~~~~~~~~~
VC C++14/17
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.27.29110\include\set(84,1): error C2280: 'main::<lambda_b1395bcc88464fd844969fa0764f137c>::<lambda_b1395bcc88464fd844969fa0764f137c>(void)': attempting to reference a deleted function
1>xx.cpp(923): message : see declaration of 'main::<lambda_b1395bcc88464fd844969fa0764f137c>::<lambda_b1395bcc88464fd844969fa0764f137c>'
1>xxx.cpp(923,20): message : 'main::<lambda_b1395bcc88464fd844969fa0764f137c>::<lambda_b1395bcc88464fd844969fa0764f137c>(void)': function was explicitly deleted
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.27.29110\include\set(84): message : while compiling class template member function 'std::set<std::string,main::<lambda_b1395bcc88464fd844969fa0764f137c>,std::allocator<std::string>>::set(void)'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.27.29110\include\tuple(975): message : see reference to function template instantiation 'std::set<std::string,main::<lambda_b1395bcc88464fd844969fa0764f137c>,std::allocator<std::string>>::set(void)' being compiled
1>xxx.cpp(937): message : see reference to class template instantiation 'std::set<std::string,main::<lambda_b1395bcc88464fd844969fa0764f137c>,std::allocator<std::string>>' being compiled
Lambdas are never default constructible before C++20 (see ClosureType::ClosureType() section on cppreference), so you must pass the SetCompare variable when constructing a MySet. Using -std=c++20/-std=c++2a on clang or GCC should work (demo), as well as /std:c++latest on MSVC.
To work around this limitation in earlier C++ versions define SetCompare as a functor type:
struct SetCompare
{
bool operator()(const string& a, const string& b)
{
size_t L = a.length();
size_t R = b.length();
if (L == R) return (a < b);
return L < R;
}
};
and use this type when defining MySet
using MySet = std::set<std::string, SetCompare>;
While compiling the code I found this error and am not able to trace this as I do not see any references to such errors on the net. Please help ---
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/memory:52,
from ./PbxComm.h:24,
from ./NsaComm.h:23,
from ./NsaBundle.h:22,
from NsaBundle.C:19:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_uninitialized.h: In static member function âstatic _ForwardIterator std::__uninitialized_copy<<anonymous> >::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::auto_ptr<SnowTTan>*, std::vector<std::auto_ptr<SnowTTan>, std::allocator<std::auto_ptr<SnowTTan> > > >, _ForwardIterator = std::auto_ptr<SnowTTan>*, bool <anonymous> = false]â:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_uninitialized.h:117: instantiated from â_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::auto_ptr<SnowTTan>*, std::vector<std::auto_ptr<SnowTTan>, std::allocator<std::auto_ptr<SnowTTan> > > >, _ForwardIterator = std::auto_ptr<SnowTTan>*]â
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_uninitialized.h:257: instantiated from â_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::auto_ptr<SnowTTan>*, std::vector<std::auto_ptr<SnowTTan>, std::allocator<std::auto_ptr<SnowTTan> > > >, _ForwardIterator = std::auto_ptr<SnowTTan>*, _Tp = std::auto_ptr<SnowTTan>]â
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_vector.h:243: instantiated from âstd::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = std::auto_ptr<SnowTTan>, _Alloc = std::allocator<std::auto_ptr<SnowTTan> >]â
./NsaForm207.h:45: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_uninitialized.h:74: error: passing âconst std::auto_ptr<SnowTTan>â as âthisâ argument of âstd::auto_ptr<_Tp>::operator std::auto_ptr_ref<_Tp1>() [with _Tp1 = SnowTTan, _Tp = SnowTTan]â discards qualifiers
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_uninitialized.h: In static member function âstatic _ForwardIterator std::__uninitialized_copy<<anonymous> >::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::auto_ptr<line_range>*, std::vector<std::auto_ptr<line_range>, std::allocator<std::auto_ptr<line_range> > > >, _ForwardIterator = std::auto_ptr<line_range>*, bool <anonymous> = false]â:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_uninitialized.h:117: instantiated from â_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::auto_ptr<line_range>*, std::vector<std::auto_ptr<line_range>, std::allocator<std::auto_ptr<line_range> > > >, _ForwardIterator = std::auto_ptr<line_range>*]â
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_uninitialized.h:257: instantiated from â_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::auto_ptr<line_range>*, std::vector<std::auto_ptr<line_range>, std::allocator<std::auto_ptr<line_range> > > >, _ForwardIterator = std::auto_ptr<line_range>*, _Tp = std::auto_ptr<line_range>]â
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_vector.h:243: instantiated from âstd::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = std::auto_ptr<line_range>, _Alloc = std::allocator<std::auto_ptr<line_range> >]â
./NsaForm346.h:70: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_uninitialized.h:74: error: passing âconst std::auto_ptr<line_range>â as âthisâ argument of âstd::auto_ptr<_Tp>::operator std::auto_ptr_ref<_Tp1>() [with _Tp1 = line_range, _Tp = line_range]â discards qualifiers
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_uninitialized.h: In static member function âstatic _ForwardIterator std::__uninitialized_copy<<anonymous> >::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::auto_ptr<tsg_500>*, std::vector<std::auto_ptr<tsg_500>, std::allocator<std::auto_ptr<tsg_500> > > >, _ForwardIterator = std::auto_ptr<tsg_500>*, bool <anonymous> = false]â:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_uninitialized.h:117: instantiated from â_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::auto_ptr<tsg_500>*, std::vector<std::auto_ptr<tsg_500>, std::allocator<std::auto_ptr<tsg_500> > > >, _ForwardIterator = std::auto_ptr<tsg_500>*]â
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_uninitialized.h:257: instantiated from â_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::auto_ptr<tsg_500>*, std::vector<std::auto_ptr<tsg_500>, std::allocator<std::auto_ptr<tsg_500> > > >, _ForwardIterator = std::auto_ptr<tsg_500>*, _Tp = std::auto_ptr<tsg_500>]â
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_vector.h:243: instantiated from âstd::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = std::auto_ptr<tsg_500>, _Alloc = std::allocator<std::auto_ptr<tsg_500> >]â
./NsaForm500.h:66: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_uninitialized.h:74: error: passing âconst std::auto_ptr<tsg_500>â as âthisâ argument of âstd::auto_ptr<_Tp>::operator std::auto_ptr_ref<_Tp1>() [with _Tp1 = tsg_500, _Tp = tsg_500]â discards qualifiers
from the title of your message it looks like you are discarding the 'const' qualifier from auto_ptr which is not allowed
It was an issue with the use of auto_ptr within vector (STL containers in general). I have been able to use unique_ptr with the appropriate compile flag. That has solved my issue.
There are tons of codes that sort an array of strings lexicographically but I can't find one that does sorting characters in ONE string reverse lexicographically. So far I discovered that using std::sort() in <algorithm> might be the closest candidate.
This is what I tried:
template <typename T>
class ReverseComparator{
bool operator()(T l, T r){return !(l < r);}
};
//.....later
std::sort(str.begin(), str.end(), comparator);
Here's the question: how can I initialize my comparator so that it can compare the characters in the string? I tried ReverseComparator<char> comparator but compiler throws a ton of error msg. gcc-4.5.1 showed these errors:
In file included from /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/algorithm:63:0,
from prog.cpp:1:
prog.cpp: In function 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]':
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2192:4: instantiated from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5252:4: instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
prog.cpp:12:49: instantiated from here
prog.cpp:6:11: error: 'bool ReverseComparator<T>::operator()(T, T) [with T = char]' is private
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2125:4: error: within this context
prog.cpp: In function 'void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]':
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5100:7: instantiated from 'void std::partial_sort(_RAIter, _RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2297:8: instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Size = int, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5250:4: instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
prog.cpp:12:49: instantiated from here
prog.cpp:6:11: error: 'bool ReverseComparator<T>::operator()(T, T) [with T = char]' is private
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1914:2: error: within this context
prog.cpp: In function 'void std::__move_median_first(_Iterator, _Iterator, _Iterator, _Compare) [with _Iterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]':
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2260:7: instantiated from '_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2302:62: instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Size = int, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5250:4: instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
prog.cpp:12:49: instantiated from here
prog.cpp:6:11: error: 'bool ReverseComparator<T>::operator()(T, T) [with T = char]' is private
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:108:7: error: within this context
prog.cpp:6:11: error: 'bool ReverseComparator<T>::operator()(T, T) [with T = char]' is private
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:110:4: error: within this context
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2260:7: instantiated from '_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2302:62: instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Size = int, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5250:4: instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
prog.cpp:12:49: instantiated from here
prog.cpp:6:11: error: 'bool ReverseComparator<T>::operator()(T, T) [with T = char]' is private
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:112:9: error: within this context
prog.cpp:6:11: error: 'bool ReverseComparator<T>::operator()(T, T) [with T = char]' is private
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:115:12: error: within this context
prog.cpp:6:11: error: 'bool ReverseComparator<T>::operator()(T, T) [with T = char]' is private
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:117:12: error: within this context
prog.cpp: In function '_RandomAccessIterator std::__unguarded_partition(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Tp = char, _Compare = ReverseComparator<char>]':
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2261:78: instantiated from '_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2302:62: instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Size = int, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5250:4: instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
prog.cpp:12:49: instantiated from here
prog.cpp:6:11: error: 'bool ReverseComparator<T>::operator()(T, T) [with T = char]' is private
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2229:4: error: within this context
prog.cpp:6:11: error: 'bool ReverseComparator<T>::operator()(T, T) [with T = char]' is private
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2232:4: error: within this context
prog.cpp: In function 'void std::__unguarded_linear_insert(_RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]':
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2133:6: instantiated from 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2192:4: instantiated from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5252:4: instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
prog.cpp:12:49: instantiated from here
prog.cpp:6:11: error: 'bool ReverseComparator<T>::operator()(T, T) [with T = char]' is private
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2083:7: error: within this context
In file included from /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:62:0,
from /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/algorithm:63,
from prog.cpp:1:
prog.cpp: In function 'void std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Distance = int, _Tp = char, _Compare = ReverseComparator<char>]':
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_heap.h:434:4: instantiated from 'void std::make_heap(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1912:7: instantiated from 'void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5100:7: instantiated from 'void std::partial_sort(_RAIter, _RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2297:8: instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Size = int, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5250:4: instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
prog.cpp:12:49: instantiated from here
prog.cpp:6:11: error: 'bool ReverseComparator<T>::operator()(T, T) [with T = char]' is private
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_heap.h:303:4: error: within this context
prog.cpp: In function 'void std::__push_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Distance = int, _Tp = char, _Compare = ReverseComparator<char>]':
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_heap.h:316:7: instantiated from 'void std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Distance = int, _Tp = char, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_heap.h:434:4: instantiated from 'void std::make_heap(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1912:7: instantiated from 'void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5100:7: instantiated from 'void std::partial_sort(_RAIter, _RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2297:8: instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Size = int, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5250:4: instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
prog.cpp:12:49: instantiated from here
prog.cpp:6:11: error: 'bool ReverseComparator<T>::operator()(T, T) [with T = char]' is private
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_heap.h:180:7: error: within this context
While solving the compiler error simply requires the operator() to be public as according to most other answers to this question, you should note that your comparator isn't a valid strict comparator. A strict comparator has to return false for elements which are treated as being equal. You should also use const references instead of values for the parameter types of your comparator.
I want to present you multiple options to solve your problem (not the compiler-error but the original problem, sorting a string in reversed lexical order). All of them return false for two equal elements.
The first option is your solution but I fixed the strictness:
template <typename T>
class ReverseComparator{
public:
bool operator()(const T& l, const T& r){return (r < l);}
};
std::sort(str.begin(), str.end(), ReverseComparator());
The second option uses C++11's lambda function, but is essentially the same:
std::sort(str.begin(), str.end(), [](const T& l, const T& r){return (r < l);});
As a third option, as first proposed by Mooning Duck, you could simply use the std functor for operator> on your type char:
std::sort(str.begin(), str.end(), std::greater<char>());
The fourth option (the simplest one) would be to use the default comparator on the reversed string, as first proposed by Peter Wood (note the r!):
std::sort(str.rbegin(), str.rend());
Usually, only the first error is relevant, so we have this:
In file included from /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/algorithm:63:0,
from prog.cpp:1:
prog.cpp: In function 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]':
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2192:4: instantiated from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5252:4: instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
prog.cpp:12:49: instantiated from here
prog.cpp:6:11: error: 'bool ReverseComparator<T>::operator()(T, T) [with T = char]' is private
Most of that is simply context, so here's the key part:
'bool ReverseComparator<T>::operator()(T, T) [with T = char]' is private
Well that's easy to fix:
#include <algorithm>
#include <string>
template <typename T>
class ReverseComparator{
public: //forgot this
bool operator()(T l, T r){return !(l < r);}
};
int main() {
std::string str = "HELLO";
ReverseComparator<char> comparator;
std::sort(str.begin(), str.end(), comparator);
}
This compiles fine, however, sorts are based on what's called a "strict weak ordering", and !(l < r) is not a strict weak ordering. Namely, comparitor(a,a) should return false, but yours returns true. Luckily, the idea of sorting this way is already in the standard library under the name std::greater. (std::sort without a comparitor uses std::less by default)
Using std::sort() is clearly the right approach. The default comparator is std::less<value_type> where value_type is decltype(*it) for the type of the iterators passed. To change the default you'd just pass a different comparator, e.g. std::greater<char>.
To use your ReverseComparator as it is defined you'd need to instantiate it correctly:
std::sort(s.begin(), s.end(), ReverseComparator<char>());
Of course, to make it work you'd need to make the operator()() publically accessible. You might want to make it a const member function as well:
template <typename T>
class ReverseComparator {
public:
bool operator()(T lhs, T rhs) const { return rhs < lhs; }
};
Note that the expression in the operator is changed as well: using !(lhs < rhs) doesn't define a strict weak order: comparator(x, x) would yield true but it is required to yield false.
This question already has answers here:
Can I use const in vectors to allow adding elements, but not modifications to the already added?
(14 answers)
Does C++11 allow vector<const T>?
(5 answers)
Closed 6 years ago.
I encountered this problem maintaining a port for a large (relative to the size of our team) project, but it was simple to create a small example.
stackoverflow.cpp:
#include<iostream>
#include<string>
#include<stack>
using namespace std;
int main (int argc, char *argv[]) {
stack<const string> strstack;
string str("Hello, world");
strstack.push(str);
cout << strstack.top() << endl;
return 0;
}
Looks correct, right? MSVS thinks so too. However:
g++ stackoverflow.cpp
In file included from /usr/include/c++/4.7/x86_64-linux-gnu/bits/c++allocator.h:34:0,
from /usr/include/c++/4.7/bits/allocator.h:48,
from /usr/include/c++/4.7/string:43,
from /usr/include/c++/4.7/bits/locale_classes.h:42,
from /usr/include/c++/4.7/bits/ios_base.h:43,
from /usr/include/c++/4.7/ios:43,
from /usr/include/c++/4.7/ostream:40,
from /usr/include/c++/4.7/iostream:40,
from stackoverflow.cpp:1:
/usr/include/c++/4.7/ext/new_allocator.h: In instantiation of ‘class __gnu_cxx::new_allocator<const std::basic_string<char> >’:
/usr/include/c++/4.7/bits/allocator.h:89:11: required from ‘class std::allocator<const std::basic_string<char> >’
/usr/include/c++/4.7/bits/stl_deque.h:489:61: required from ‘class std::_Deque_base<const std::basic_string<char>, std::allocator<const std::basic_string<char> > >’
/usr/include/c++/4.7/bits/stl_deque.h:728:11: required from ‘class std::deque<const std::basic_string<char>, std::allocator<const std::basic_string<char> > >’
/usr/include/c++/4.7/bits/stl_stack.h:98:46: required from ‘class std::stack<const std::basic_string<char> >’
stackoverflow.cpp:9:23: required from here
/usr/include/c++/4.7/ext/new_allocator.h:83:7: error: ‘const _Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::const_reference) const [with _Tp = const std::basic_string<char>; __gnu_cxx::new_allocator<_Tp>::const_pointer = const std::basic_string<char>*; __gnu_cxx::new_allocator<_Tp>::const_reference = const std::basic_string<char>&]’ cannot be overloaded
/usr/include/c++/4.7/ext/new_allocator.h:79:7: error: with ‘_Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::reference) const [with _Tp = const std::basic_string<char>; __gnu_cxx::new_allocator<_Tp>::pointer = const std::basic_string<char>*; __gnu_cxx::new_allocator<_Tp>::reference = const std::basic_string<char>&]’
/usr/include/c++/4.7/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::deallocate(__gnu_cxx::new_allocator<_Tp>::pointer, __gnu_cxx::new_allocator<_Tp>::size_type) [with _Tp = const std::basic_string<char>; __gnu_cxx::new_allocator<_Tp>::pointer = const std::basic_string<char>*; __gnu_cxx::new_allocator<_Tp>::size_type = long unsigned int]’:
/usr/include/c++/4.7/bits/stl_deque.h:540:2: required from ‘void std::_Deque_base<_Tp, _Alloc>::_M_deallocate_node(_Tp*) [with _Tp = const std::basic_string<char>; _Alloc = std::allocator<const std::basic_string<char> >]’
/usr/include/c++/4.7/bits/stl_deque.h:643:2: required from ‘void std::_Deque_base<_Tp, _Alloc>::_M_destroy_nodes(_Tp**, _Tp**) [with _Tp = const std::basic_string<char>; _Alloc = std::allocator<const std::basic_string<char> >]’
/usr/include/c++/4.7/bits/stl_deque.h:566:4: required from ‘std::_Deque_base<_Tp, _Alloc>::~_Deque_base() [with _Tp = const std::basic_string<char>; _Alloc = std::allocator<const std::basic_string<char> >]’
/usr/include/c++/4.7/bits/stl_deque.h:781:15: required from ‘std::deque<_Tp, _Alloc>::deque() [with _Tp = const std::basic_string<char>; _Alloc = std::allocator<const std::basic_string<char> >]’
stackoverflow.cpp:9:23: required from here
/usr/include/c++/4.7/ext/new_allocator.h:100:9: error: invalid conversion from ‘const void*’ to ‘void*’ [-fpermissive]
In file included from /usr/include/c++/4.7/ext/new_allocator.h:34:0,
from /usr/include/c++/4.7/x86_64-linux-gnu/bits/c++allocator.h:34,
from /usr/include/c++/4.7/bits/allocator.h:48,
from /usr/include/c++/4.7/string:43,
from /usr/include/c++/4.7/bits/locale_classes.h:42,
from /usr/include/c++/4.7/bits/ios_base.h:43,
from /usr/include/c++/4.7/ios:43,
from /usr/include/c++/4.7/ostream:40,
from /usr/include/c++/4.7/iostream:40,
from stackoverflow.cpp:1:
/usr/include/c++/4.7/new:97:6: error: initializing argument 1 of ‘void operator delete(void*)’ [-fpermissive]
In file included from /usr/include/c++/4.7/deque:63:0,
from /usr/include/c++/4.7/stack:61,
from stackoverflow.cpp:3:
/usr/include/c++/4.7/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, const _T2&) [with _T1 = const std::basic_string<char>; _T2 = std::basic_string<char>]’:
/usr/include/c++/4.7/bits/stl_uninitialized.h:77:3: required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::_Deque_iterator<const std::basic_string<char>, const std::basic_string<char>&, const std::basic_string<char>*>; _ForwardIterator = std::_Deque_iterator<const std::basic_string<char>, const std::basic_string<char>&, const std::basic_string<char>*>; bool _TrivialValueTypes = false]’
/usr/include/c++/4.7/bits/stl_uninitialized.h:119:41: required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::_Deque_iterator<const std::basic_string<char>, const std::basic_string<char>&, const std::basic_string<char>*>; _ForwardIterator = std::_Deque_iterator<const std::basic_string<char>, const std::basic_string<char>&, const std::basic_string<char>*>]’
/usr/include/c++/4.7/bits/stl_uninitialized.h:260:63: required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = std::_Deque_iterator<const std::basic_string<char>, const std::basic_string<char>&, const std::basic_string<char>*>; _ForwardIterator = std::_Deque_iterator<const std::basic_string<char>, const std::basic_string<char>&, const std::basic_string<char>*>; _Tp = const std::basic_string<char>]’
/usr/include/c++/4.7/bits/stl_deque.h:841:9: required from ‘std::deque<_Tp, _Alloc>::deque(const std::deque<_Tp, _Alloc>&) [with _Tp = const std::basic_string<char>; _Alloc = std::allocator<const std::basic_string<char> >; std::deque<_Tp, _Alloc> = std::deque<const std::basic_string<char>, std::allocator<const std::basic_string<char> > >]’
/usr/include/c++/4.7/bits/stl_stack.h:130:14: required from ‘std::stack<_Tp, _Sequence>::stack(const _Sequence&) [with _Tp = const std::basic_string<char>; _Sequence = std::deque<const std::basic_string<char>, std::allocator<const std::basic_string<char> > >]’
stackoverflow.cpp:9:23: required from here
/usr/include/c++/4.7/bits/stl_construct.h:85:7: error: invalid static_cast from type ‘const std::basic_string<char>*’ to type ‘void*’
With stack<string> strstack; it's all clean. Is this a g++ bug? Is there a workaround so that we won't have to drop the const until the bug is fixed?
The members of a standard container have to be copy assignable or movable (C++11). If the type is const it fails the requirements.
It is possible for the container to hold pointers (or preferably, smart pointers) to constants. This maintains the desired const semantics, although perhaps the syntax is a bit more clunky.
For example:
stack<shared_ptr<string const>> s;
s.push(make_shared<string>("foo"));
cout << *s.top() << endl;
For more info see this question.