随笔

 

 

 

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <vector>
 4 #include <fstream>
 5 #include <map>
 6 #include <set>
 7 using namespace std;
 8 
 9 bool exclusion_gen(set<string> * w_x)
10 {
11     w_x->insert("a");
12     w_x->insert("an");
13     w_x->insert("or");
14     w_x->insert("and");
15     w_x->insert("but");
16 }
17 
18 // class String_Length_Less{
19 struct String_Length_Less{
20 public:
21     bool operator()(string st1,string st2) const;
22 };
23 
24 inline bool String_Length_Less :: operator()(string st1,string st2) const
25 {
26     return st1.length()<st2.length();
27 }
28 
29 
30 int main()
31 {
32     set<string> word_exclusion;
33     map< string , int > words;
34     vector<string> vec_words;
35     exclusion_gen( &word_exclusion );
36     string st;
37     ifstream infile("in.txt");
38     while( infile >> st )
39     {
40         if ( word_exclusion.count(st) )
41         {
42             cout << "string " << st <<" is in word_exclusion"<<endl;
43             continue;
44         }
45         vec_words.push_back(st);
46     }
47     String_Length_Less sll;
48     sort(vec_words.begin(),vec_words.end(),sll );
49 // sort(vec_words.begin(),vec_words.end(),greater<int>() );
50     vector<string> :: iterator it = vec_words.begin();
51     for (;it!=vec_words.end();++it)
52         cout << *it << endl;
53 
54 }
View Code

 

在进行essential C++习题练习过程中,遇到了 .h .cpp文件 include过程中的重定义问题,这也恰恰是我一直疑惑的地方,预期会在学习编译连接过程中得到解答,目前的解决方法是 不定义Userprofile.cpp,全部class member function/data 与 nonmember function 定义于 Userprofile.h头文件中。

 

#include <iostream>
#include <string.h>
#include <map>
using namespace std;

class Userprofile{
public:
    enum ulevel{ Beginner,Itermediate , Advanced , Master };

    Userprofile( string & login ,ulevel level = Beginner);
    Userprofile();

    bool operator ==(const Userprofile & rhs);
    bool operator !=(const Userprofile & rhs);

    string login() const {return _login;}
    string user_name() const {return _user_name;}
    int login_time() const {return _login_time;}
    string user_level() const {
        static string user_level[4]={"Beginner","Itermediate","Advanced","Master"};
        return user_level[ _user_level ];
    }
    double correct_rate()const;
    bool reset_guess(){_guess_correct=0;_guess_time=0;}
    bool reset_login(const string & val) { _login=val;return true; }
    bool reset_user_name(const string & val) {_user_name = val;return true;}
    bool reset_login_time(const int val=0) {_login_time=val;return true;}
    bool add_login_time(const int val=1) { _login_time+=val; return true; }
    bool reset_user_level(const ulevel val) {_user_level = val;return true; }
    bool reset_user_level(const string & val);


private:
    string _login;
    string _user_name;
    int _login_time;
    int _guess_correct;
    int _guess_time;
    ulevel _user_level;
    static map<string , ulevel > _level_map;
    static void init_level_map();
    static string guest_login();
};

map< string , Userprofile::ulevel > Userprofile::_level_map;
void Userprofile::init_level_map(){
    _level_map.clear();
    _level_map["Beginner"]=Beginner;
    _level_map["Itermediate"]=Itermediate;
    _level_map["Advanced"]=Advanced;
    _level_map["Master"]= Master;
}

bool Userprofile::reset_user_level(const string & val){
    if (_level_map.empty()) init_level_map();
    _user_level = _level_map[val];
    return true;
}

inline bool Userprofile::operator!= (const Userprofile & rhs ){
    return !((*this)==rhs);
}

inline bool Userprofile::operator== (const Userprofile & rhs ){
    if ( (_login_time == rhs._login_time ) && ( _user_name==rhs.user_name()) )
        return true;
    return false;
} // why here in rhs we can use privater data member _login, like rhs._login_time;

inline double Userprofile::correct_rate() const { if(_guess_time==0) return double(0); return 1.0*_guess_correct/_guess_time;}

inline Userprofile:: Userprofile(string& login , ulevel level)
: _login(login) , _user_level(level) {
    _login_time=0;_guess_correct=0;_guess_time=0;
}

inline Userprofile::Userprofile()
: _login("guest") , _user_level(Beginner){
    static int _guest_id=0;
    ++_guest_id;
    string guest_id_string="0000";
    int tmp=3,guest_id=_guest_id;
    while (guest_id){
        guest_id_string[tmp--]+=guest_id%10;
        guest_id/=10;
    }
    _login+=guest_id_string;
    _guess_correct=_guess_time=0;
}

ostream &operator << ( ostream &os , const Userprofile &rhs){
    os << rhs.login() << ' ' << rhs.user_name() << ' ' << rhs.user_level() <<' '<< rhs.correct_rate()<<'\n' ;
    return os;
}

istream &operator >> ( istream &is , const Userprofile &rhs){
    ;

}
View Code

 

posted on 2019-10-07 12:32  askl123  阅读(124)  评论(0)    收藏  举报

导航