I was reading about string and function pointer in C++. There I found a problem sort array of string based on first character value. And here is my code solution.
#include<bits/stdc++.h>
using namespace std;
bool compare1(const void *a, const void *b)
{
string c = *(string *)a;
string d = *(string *)b;
return c[0]<d[0];
}
int main()
{
string str[3];
int i;
for(i=0;i<3;i++)
{
cout<<"Enter "<<i+1<<" string";
cin>>str[i];
}
cout<<"Before sort"<<endl;
for(i=0;i<3;i++)
{
cout<<str[i]<<" ";
}
cout<<endl;
sort(str.begin(),str.end(),compare1);
cout<<"After sort"<<endl;
for(i=0;i<3;i++)
{
cout<<str[i]<<" ";
}
return 0;
}
But I am getting compile time error: [Error] request for member 'begin' in 'str', which is of non-class type 'std::string [3] {aka std::basic_string [3]}'
How to fix this issue?