I have two programs over here.
Set.cpp
using namespace std;
typedef pair<int, int> pairs;
struct sort_asc
{
template
bool operator()(const t& a, const t& b)
{
return (a.second > b.second);
}
};
int main() {
int n;
cin >> n;
set<pairs> st;
for(int i = 0; i < n; i++)
{
int t;
cin >> t;
st.insert({t, n - i});
}
set<pairs, sort_asc> st_asc(st.begin(), st.end());
set<pairs>::iterator it = st_asc.begin();
for(; it != st_asc.end(); it++)
cout << it->first <<" " << it->second<<endl;
}
vector.cpp
bool sortbysec(const pair<int,int> &a, const pair<int,int> &b)
{
return (a.first < b.first);
}
int main()
{
int n;
cin >> n;
vector< pair <int, int> > vect;
for (int i=0; i < n; i++)
{
int t;
cin >> t;
vect.push_back(make_pair(t, n - i) );
}
sort(vect.begin(), vect.end(), sortbysec);
cout << "The vector after sort operation is:\n" ;
for (int i=0; i<n; i++)
{
cout << vect[i].first << " "<< vect[i].second << endl;
}
return 0;
}
When i am trying to use the functor in the set without the struct defination, it gives error. But when we do the same in vector it works fine.
Why does it happen?