STL is really both hateful and loving. Hate is that his syntax is too cumbersome (basically, when it is used many times, you have to read the materials, his syntax is really hard to remember). What I love is that his function is really powerful. However, as far as I am concerned, Ren ran loves big and hate, huh, huh... if you reasonably combine STL containers and Algorithms in your program, rather than writing and debugging your own version from start to end, you can save a lot of time and effort.
In most online chat programs, users are allowed to have a "friend list" or a friend list. I always wanted to know how to make something with such a function? How can we make it simple and efficient? According to the professional C ++ of Nicolas A. solter, we finally found a shortcut.
The chat program grants privileges to users listed in the friend list, such as allowing them to send active messages to users. One way to implement a friend list for online chat programs is to store information in a multimap. A multimap can store a list of friends of all users. Each item in the container corresponds to a user's friend. The key is a user and the value is a friend. For example, if one of my friends and I are in the list of friends of the other party, for example, if we are in the list of friends of the other party, there will be two items in the form below, "shajin" maps to "Hui ge", while "Hui ge" maps to "shajin ". Multimap allows a single key to have multiple values, so a user can have multiple friends. The following is the definition of the buddylist class:
# Include <map> <br/> # include <string> <br/> # include <list> </P> <p> Using STD: multimap; <br/> Using STD: string; <br/> Using STD: List; </P> <p> class buddylist <br/>{< br/> public: <br/> buddylist (); <br/> // Add a buddy by name <br/> void addbuddy (const string & name, const string & buddy ); <br/> void removebuddy (const string & name, const string & buddy); <br/> // If Buddy is friend of name, ruturn true, otherwise return false <br/> bool isbuddy (const string & name, const string & buddy) const; <br/> // list of all friends <br/> List <string> getbuddies (const string & name) const; <br/> protected: <br/> multimap <string, string> mbuddies; <br/> PRIVATE: <br/> buddylist (const buddylist & SRC ); <br/> buddylist & operator = (const buddylist & RHs); </P> <p> };
The following is an implementation. It shows how to use lower_bound (), upper_bound (), and low_range (). (No way, this is the name in STL. In fact, I do not like this method at all ):
# Include "buddylist. H "</P> <p> using namespace STD; <br/> buddylist: buddylist () <br/>{< br/>}< br/> void buddylist:: addbuddy (const string & name, const string & buddy) <br/>{< br/> If (! Isbuddy (name, buddy) <br/> mbuddies. insert (make_pair (name, buddy); <br/>}< br/> void buddylist: removebuddy (const string & name, const string & buddy) <br/>{< br/> multimap <string, string >:: iterator start, end; <br/> Start = mbuddies. lower_bound (name); <br/> end = mbuddies. upper_bound (name); <br/> for (start; start! = End; ++ start) <br/>{< br/> If (START-> second = buddy) <br/>{< br/> mbuddies. erase (start); <br/> break; <br/>}< br/> bool buddylist :: isbuddy (const string & name, const string & buddy) const <br/>{< br/> multimap <string, string >:: const_iterator start, end; <br/> Start = mbuddies. lower_bound (name); <br/> end = mbuddies. upper_bound (name); <br/> for (start; start! = End; ++ start) <br/>{< br/> If (START-> second = buddy) <br/>{< br/> return true; <br/>}< br/> return false; <br/>}</P> <p> List <string> buddylist :: getbuddies (const string & name) const <br/>{< br/> pair <multimap <string, string >:: const_iterator, multimap <string, string> :: const_iterator> its; </P> <p> its = mbuddies. performance_range (name); </P> <p> List <string> buddies; <br/> for (its. first; its. first! = Its. second; ++ its. first) <br/>{< br/> buddies. push_back (its. first)-> second); <br/>}< br/> return buddies; <br/>}
In this way, it is almost the same. However, you must note that removebuddy () cannot only use the erase () of the "completely deleted" version, that is, to delete all elements with a given key, because it should only delete one element with a given key, not all. Also, getbuddies () cannot use insert () of list to insert elements in the range returned by equal_range (), because the elements indicated by multimap iterator are key/value pairs rather than strings. Getbuddies () must display the list of iterative processing, extract the string from each key/value pair, and press it into the new list to be returned.
The following is a simple test:
Int main () <br/>{< br/> buddylist buddies; <br/> buddies. addbuddy ("shajin", "Hain"); <br/> buddies. addbuddy ("shajin", "SS"); <br/> buddies. addbuddy ("shajin", "shj"); <br/> buddies. addbuddy ("shajin", "shj348794"); </P> <p> buddies. removebuddy ("shajin", "shj"); </P> <p> buddies. addbuddy ("girl", "ll"); <br/> buddies. addbuddy ("girl", "xcy"); <br/> buddies. addbuddy ("girl", "GM"); </P> <p> List <string> shajinbuds = buddies. getb Uddies ("shajin"); </P> <p> cout <"shajin's friend:/N"; <br/> for (list <string> :: const_iterator it = shajinbuds. begin (); it! = Shajinbuds. end (); ++ it) <br/>{< br/> cout <"/t" <* It <Endl; <br/>}< br/> return 0; <br/>}