i am trying to read a file and sorting it alphabetically, i do not know how to tackle this issue.
so i am reading a file that contains , States, counties, and cities. and i need to sort the State alphabetically. can you please guide on how to solve this? i do not use vector.
i have tried this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
struct state {
string name; //name of state
struct county *c; //name of counties
int counties; //number of counties in state
int population; //total population of state
bool operator< (const state& rhs) {
return lexicographical_compare(name.cbegin(), name.cend(), rhs.name.cbegin(), rhs.name.cend());
}
bool operator== (const state& rhs) {
return name == rhs.name;
}
};
|
1 2 3 4 5 6 7 8 9 10
|
void sorting(struct state* array, int x){
sort(begin(array), end(array));
for(int i=0; i<x; i++){
cout << array[i].name << endl;
}
}
|
those are the errors i get:
t2.cpp: In member function ‘bool state::operator<(const state&)’:
t2.cpp:22:45: error: ‘std::string’ has no member named ‘cbegin’
return lexicographical_compare(name.cbegin(), name.cend(), rhs.name.cbegin(), rhs.name.cend());
^
t2.cpp:22:60: error: ‘std::string’ has no member named ‘cend’
return lexicographical_compare(name.cbegin(), name.cend(), rhs.name.cbegin(), rhs.name.cend());
^
t2.cpp:22:77: error: ‘const string’ has no member named ‘cbegin’
return lexicographical_compare(name.cbegin(), name.cend(), rhs.name.cbegin(), rhs.name.cend());
^
t2.cpp:22:96: error: ‘const string’ has no member named ‘cend’
return lexicographical_compare(name.cbegin(), name.cend(), rhs.name.cbegin(), rhs.name.cend());
^
t2.cpp: In function ‘void sorting(state*, int)’:
t2.cpp:182:17: error: ‘begin’ was not declared in this scope
sort(begin(array), end(array));
^
t2.cpp:182:29: error: ‘end’ was not declared in this scope
sort(begin(array), end(array));