When I try to compile the following code,
insert
Code:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class c
{
public:
struct match
{
int index;
int count;
}n;
bool func(const match& a,const match& b)
{
return a.count<b.count;
}
void compare()
{
vector<match> chk;
for(int i=5;i>=0;i++)
{
n.index=i;
n.count=i;
chk.push_back(n);
}
sort(chk.begin(),chk.end(),func);
for(int i=0;i<chk.size();i++)
{
cout<<chk[i].index<<" "<<chk[i].count<<endl;
}
// return 0;
}
};
int main()
{
c obj;
obj.compare();
return 0;
}
I get the following error....
sample.cpp: In member function ‘void c::compare()’:
sample.cpp:31: error: no matching function for call to ‘sort(__gnu_cxx::__normal_iterator<c::match*, std::vector<c::match, std::allocator<c::match> > >, __gnu_cxx::__normal_iterator<c::match*, std::vector<c::match, std::allocator<c::match> > >, <unresolved overloaded function type>)’
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:2735: note: candidates are: void std::sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<c::match*, std::vector<c::match, std::allocator<c::match> > >, _Compare = bool (c::*)(const c::match&, const c::match&)]
Aren't the iterators for a vector random access by default????Kindly help!!!!!