C++ Reversely sort characters in a string - c++

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.

Related

"<<" Cant convert types when using result of dict.find() in C++ [duplicate]

This question already has answers here:
error: no match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘std::_List_iterator<int>’)
(4 answers)
Why should I not #include <bits/stdc++.h>?
(9 answers)
Closed 8 months ago.
I'm trying to make a program that converts text you put in to bigger ascii art text. I made a prototype and ran it, and I got this error
error: no match for 'operator<<' (operand types are 'std::ostream' {aka 'std::basic_ostream<char>'} and 'std::map<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >::iterator' {aka 'std::_Rb_tree<std::__cxx11::basic_string<char>, std::pair<const std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >, std::_Select1st<std::pair<const std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> > >, std::less<std::__cxx11::basic_string<char> >, std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> > > >::iterator'})
(The full error is way too large to put here)
I think it's because I'm using a very specific type that std::cout doesn't support. I've tried tons of different ways to convert it into a normal string, but all of the functions I've tried don't support it either.
Code:
#include <map>
#include <string>
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
int main() {
std::map<std::string, std::string> bigabc = {{"A", " ||||| \n|| || \n||||||| \n|| ||"}, {"B", "|||||||\n| |\n||||||\n| |\n|||||||"}, {"C", "|||||||\n||\n||\n||\n|||||||"}};
std::string input;
std::cin >> input;
std::transform(input.begin(), input.end(), input.begin(), ::toupper);
std::vector<std::string> chars(input.begin(), input.end());
for (int pos = 0; pos < chars.size(); pos++) {
std::cout << bigabc.find(chars[pos]) << "\n\n";
}
}
I've been stuck on this for hours. Is there any way to make this work, or should I just rewrite my code entirely?
I tried using bigabc.at() instead and got this error:
In file included from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/vector:66,
from asciisnake.cpp:4:
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_uninitialized.h: In instantiation of '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; _ForwardIterator = std::__cxx11::basic_string<char>*]':
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_uninitialized.h:325:37: required from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; _ForwardIterator = std::__cxx11::basic_string<char>*; _Tp = std::__cxx11::basic_string<char>]'
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_vector.h:1585:33: required from 'void std::vector<_Tp, _Alloc>::_M_range_initialize(_ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _ForwardIterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; _Tp = std::__cxx11::basic_string<char>; _Alloc = std::allocator<std::__cxx11::basic_string<char> >]'
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_vector.h:657:23: required from 'std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&) [with _InputIterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; <template-parameter-2-2> = void; _Tp = std::__cxx11::basic_string<char>; _Alloc = std::allocator<std::__cxx11::basic_string<char> >; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::__cxx11::basic_string<char> >]'
asciisnake.cpp:15:62: required from here
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_uninitialized.h:137:72: error: static assertion failed: result type must be constructible from value type of input range
137 | static_assert(is_constructible<_ValueType2, decltype(*__first)>::value,
|
Tried using const map, <char, string> and bigabc[chars[pos]] and got this error:
asciisnake.cpp: In function 'int main()':
asciisnake.cpp:20:23: error: no match for 'operator[]' (operand types are 'const std::map<char, std::__cxx11::basic_string<char> >' and '__gnu_cxx::__alloc_traits<std::allocator<std::__cxx11::basic_string<char> >, std::__cxx11::basic_string<char> >::value_type' {aka 'std::__cxx11::basic_string<char>'})
20 | cout << bigabc[chars[pos]] << "\n\n";
| ^
In file included from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/map:61,
from asciisnake.cpp:1:
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_map.h:492:7: note: candidate: 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = char; _Tp = std::__cxx11::basic_string<char>; _Compare = std::less<char>; _Alloc = std::allocator<std::pair<const char, std::__cxx11::basic_string<char> > >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = std::__cxx11::basic_string<char>; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = char]'
492 | operator[](const key_type& __k)
| ^~~~~~~~
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_map.h:492:34: note: no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::__cxx11::basic_string<char> >, std::__cxx11::basic_string<char> >::value_type' {aka 'std::__cxx11::basic_string<char>'} to 'const key_type&' {aka 'const char&'}
492 | operator[](const key_type& __k)
| ~~~~~~~~~~~~~~~~^~~
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_map.h:512:7: note: candidate: 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](std::map<_Key, _Tp, _Compare, _Alloc>::key_type&&) [with _Key = char; _Tp = std::__cxx11::basic_string<char>; _Compare = std::less<char>; _Alloc = std::allocator<std::pair<const char, std::__cxx11::basic_string<char> > >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = std::__cxx11::basic_string<char>; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = char]'
512 | operator[](key_type&& __k)
| ^~~~~~~~
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_map.h:512:29: note: no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::__cxx11::basic_string<char> >, std::__cxx11::basic_string<char> >::value_type' {aka 'std::__cxx11::basic_string<char>'} to 'std::map<char, std::__cxx11::basic_string<char> >::key_type&&' {aka 'char&&'}
512 | operator[](key_type&& __k)
| ~~~~~~~~~~~^~~
In file included from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/vector:66,
from asciisnake.cpp:4:
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_uninitialized.h: In instantiation of '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; _ForwardIterator = std::__cxx11::basic_string<char>*]':
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_uninitialized.h:325:37: required from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; _ForwardIterator = std::__cxx11::basic_string<char>*; _Tp = std::__cxx11::basic_string<char>]'
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_vector.h:1585:33: required from 'void std::vector<_Tp, _Alloc>::_M_range_initialize(_ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _ForwardIterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; _Tp = std::__cxx11::basic_string<char>; _Alloc = std::allocator<std::__cxx11::basic_string<char> >]'
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_vector.h:657:23: required from 'std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&) [with _InputIterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; <template-parameter-2-2> = void; _Tp = std::__cxx11::basic_string<char>; _Alloc = std::allocator<std::__cxx11::basic_string<char> >; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::__cxx11::basic_string<char> >]'
asciisnake.cpp:17:52: required from here
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_uninitialized.h:137:72: error: static assertion failed: result type must be constructible from value type of input range
137 | static_assert(is_constructible<_ValueType2, decltype(*__first)>::value,
|
bigabc.find() will return an iterator pointing to the matching pair {"A","||......"}. You can use ->second to get the second string, and that will print fine.
Even easier, you can use the [] operator instead of find(): cout << bigabc[chars[pos]]; will print the matching 'big' string for chars[pos].

Why can't I use a member function cmp in C++ sort()?

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));

no match for call while std::sort with lambda functin

I'm trying to sort a vector using std::sort, i.e
ScanIndex::ScanIndex(std::vector<ScanData*> *scans, int currVersion, KeyCell minKey, KeyCell maxKey){
std::sort(scans->begin(), scans->end(),
[](const ScanData *& a, const ScanData *& b) -> bool
{
return (a->version.load() > b->version.load());
});
}
While having this error:
/usr/include/c++/5/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<ScanData**, std::vector<ScanData*> >; _Iterator2 = __gnu_cxx::__normal_iterator<ScanData**, std::vector<ScanData*> >; _Compare = ScanIndex::ScanIndex(std::vector<ScanData*>*, int, KeyCell, KeyCell)::<lambda(const ScanData*&, const ScanData*&)>]’:
/usr/include/c++/5/bits/stl_algo.h:1842:14: required from ‘void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ScanData**, std::vector<ScanData*> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<ScanIndex::ScanIndex(std::vector<ScanData*>*, int, KeyCell, KeyCell)::<lambda(const ScanData*&, const ScanData*&)> >]’
/usr/include/c++/5/bits/stl_algo.h:1880:25: required from ‘void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ScanData**, std::vector<ScanData*> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<ScanIndex::ScanIndex(std::vector<ScanData*>*, int, KeyCell, KeyCell)::<lambda(const ScanData*&, const ScanData*&)> >]’
/usr/include/c++/5/bits/stl_algo.h:1966:31: required from ‘void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ScanData**, std::vector<ScanData*> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<ScanIndex::ScanIndex(std::vector<ScanData*>*, int, KeyCell, KeyCell)::<lambda(const ScanData*&, const ScanData*&)> >]’
/usr/include/c++/5/bits/stl_algo.h:4729:18: required from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<ScanData**, std::vector<ScanData*> >; _Compare = ScanIndex::ScanIndex(std::vector<ScanData*>*, int, KeyCell, KeyCell)::<lambda(const ScanData*&, const ScanData*&)>]’
/home/dvir/CLionProjects/KiWi-cpp-pq-port/ScanIndex.cpp:38:11: required from here
/usr/include/c++/5/bits/predefined_ops.h:125:46: error: no match for call to ‘(ScanIndex::ScanIndex(std::vector<ScanData*>*, int, KeyCell, KeyCell)::<lambda(const ScanData*&, const ScanData*&)>) (ScanData*&, ScanData*&)’
{ return bool(_M_comp(*__it1, *__it2)); }
This is the ScanData object
class ScanData{
public:
static const ScanData* empty_ScanData;
ScanData(KeyCell min, KeyCell max) : min(min), max(max), version(0)
{}
ScanData(const ScanData& scanData) : min(scanData.min), max(scanData.max), version(version.load())
{}
std::atomic<int> version;
KeyCell min;
KeyCell max;
};
I'm guessing that I've declared a different type(signature) of lambda than the one expected, but it seems to correspond to the signature in the docs.
Thoughts?
scans is a pointer to a vector containing ScanData*.
Therefore, the lambda can expect an argument that may bind to ScanData* const &.
The type you specify is const ScanData* & (the referred to pointer is not const, but the pointee). The qualifications are mismatching. While a conversion is possible from ScanData* to const ScanData*, that will require a temporary pointer, and a non-const lvalue reference cannot bind to one.
Since pointers are value types, and cheap to copy value types at that, just don't pass by reference. Pass the pointers by value to the lambda.
[](const ScanData *a, const ScanData *b) -> bool
{
return (a->version.load() > b->version.load());
});

Why does stack<const string> not compile in g++? [duplicate]

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.

Usage of this next_combination code

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.