我有一个文件,它的第一行是英文字母,随机顺序,之后,名称。我应该根据给定的字母来排列名字。我的类看起来是这样的:设置自定义函数对象比较器c + +
class MyCompare(){
private:
static map<char, int> order;
public:
MyCompare(string alphabet){
//loop through the string, assign character to it's index in the map
// order[ alphabet[i] ] = i;
}
bool operator()(const string s1, const string s2) {
//compare every character using compchar, return the result
}
bool compchar(const char c1, const char c2){
return order[c1]<order[c2];
}
}
主,我做了这样的事情:
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();
}
当然
,这是不行的,因为设置ISN”在if块之外可见。我不能拿出任何东西,但... 请指教。
我不知道看这个代码应该做什么。 – 2011-05-09 18:05:15
“操作员”之后应该有一个操作员... – 2011-05-09 18:05:36
@Mike DeSimone对不起,错字。 – adamors 2011-05-09 18:06:39