I have a file, the first line on it is the English alphabet, in random order, after that the names. I have to sort the names according to the specified alphabet. My class looks something like this:
class MyCompare(){
private:
static map<char, int> order;
public:
MyCompare(string alphabet){
}
bool operator()(const string s1, const string s2) {
}
bool compchar(const char c1, const char c2){
return order[c1]<order[c2];
}
}
basically, I did something like this:
int i=0;
if (myfile.is_open()) {
while ( myfile.good() ) {
i++;
getline (myfile,line);
if(i ==1){
MyCompare st(line);
set<string, MyCompare> words(st);
}
words.insert(line);
}
myfile.close();
}
Of course, this does not work, because the set is not visible outside the if block. I can’t think of anything else, though ... Please inform.
source
share