[C/C ++ school] 0904-boost smart pointer/boost multi-thread lock/HASH Library/Regular Expression
Boost_array_bind_fun_ref
Array. cpp
# Include
# Include
# Include
Using namespace std; using namespace boost; void mainA () {array
Barray = {1, 2, 3, 4, 5}; barray [0] = 10; barray. at (4) = 20; int * p = barray. data (); // the pointer to the storage array for (int I = 0; I <barray. size (); I ++) {cout <barray [I] <"" <p [I] <endl;} array
Cmd = {"calc", "notepad", "tasklist"}; cin. get ();}
Bind. cpp
# Include
# Include
# Include
# Include
# Include
Using namespace std; using namespace boost; // The default value of the binding function, which inherits all classes of the binary function class. class add: public std: binary_function
{Public: void operator () (int I, int j) const {std: cout <I + j <endl ;}}; void add (int I, int j) {std: cout <I + j <endl;} void mainB () {vector
Myv; myv. push_back (11); myv. push_back (23); myv. push_back (34); // for_each (myv. begin (), myv. end (), bind1st (add (), 10); for_each (myv. begin (), myv. end (), bind (add, 13, _ 1); // bind sets the default parameter call, function copy mechanism, and cannot copy to construct cin. get ();}
Fun. cpp
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Using namespace std; using namespace boost; void mainC () {// atoi // char * to intboost: function
Fun = atoi; cout <fun ("123") + fun ("234") <endl; fun = strlen; cout <fun ("123 ") + fun ("234") <endl; cin. get ();} void mainD () {boost: function
Fun = atoi; cout <fun ("123") + fun ("234") <endl; fun = boost: bind (strcmp, "ABC ", _ 1); cout <fun ("123") <endl; cout <fun ("ABC") <endl; cin. get ();} class manager {public: void allstart () {for (int I = 0; I <10; I ++) {if (workid) {workid (I) ;}} void setcallback (boost: function
Newid) // bind the call {workid = newid;} public: boost: function
Workid ;}; class worker {public: void run (int toid) {id = toid; cout <id <"work" <endl;} public: int id ;}; void mainE () {manager m; worker w; // a member function of the class needs to be called by an object and bound to a default object m. setcallback (boost: bind (& worker: run, & w, _ 1); m. allstart (); cin. get ();}
Ref. cpp
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Using namespace std; using namespace boost; void print (std: ostream & OS, int I) {OS <I <endl;} void mainF () {// objects that cannot be copied can use refboost: function.
Pt = boost: bind (print, boost: ref (cout), _ 1); vector
V; v. push_back (11); v. push_back (12); v. push_back (13); for_each (v. begin (), v. end (), pt); std: cin. get ();}
Boost smart pointer
RAII principle. cpp
# Define _ CRT_SECURE_NO_WARNINGS # include
# Include
# Include
Using namespace std; class mystr {public: char * p = nullptr; public: mystr (const char * str) {cout <"build" <endl; int length = strlen (str); p = new char [length + 1]; strcpy (p, str); p [length] = '\ 0 ';}~ Mystr () {cout <"Destroy" <endl; delete [] p ;}}; void go () {char * p = new char [100]; mystr str1 = "ABCD"; // RAII avoids Memory leakage. Generally, the memory on the stack is used as a stack. // The stack memory is limited and you want to release it automatically, use a large amount of memory .} Void mainHG () {go (); cin. get ();}
Smartpointer principle. cpp
# Include
# Include
# Include
# Include
# Include
Using namespace std; template
Class pmy {public: pmy () {}pmy (T * t) {p = t ;}~ Pmy () {if (p! = Nullptr) {delete p ;}} T operator * () {return * p;} private: T * p = nullptr ;}; class Test {public: Test () {cout <"Test create" <endl ;}~ Test () {cout <"Test delete" <endl ;}}; void run () {pmy
P (new Test); // smart pointer, smart release // * p;} void mainH () {run (); cin. get ();}
Smartpointer. cpp
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Using namespace std; void mainI () {boost: scoped_ptr
P (new int); // Automatically releases the memory * p = 12; cout <* p. get () <endl; p. reset (new int); * p. get () = 3; boost: scoped_ptr
PA (nullptr); // exclusive memory // pA = p; cout <* p. get () <endl; cin. get ();} void mainG () {boost: scoped_array
P (new int [10]); // Automatically releases memory // boost: scoped_array
PA (p); exclusive pointer * p. get () = 1; p [3] = 2; p. reset (new int [5]); // only pointers to cin. get ();} void show (boost: shared_ptr
P) {cout <* p <endl;} void mainK () {vector
> V; boost: shared_ptr
P1 (new int (11); boost: shared_ptr
P2 (new int (12); boost: shared_ptr
P3 (p2); // copy v. push_back (p1); v. push_back (p2); v. push_back (p3); for_each (v. begin (), v. end (), show); cin. get ();} class runclass {public: int I = 0; public: runclass (int num): I (num) {cout <"I create" <
* Pw = static_cast
*> (P); boost: shared_ptr
Sh = pw-> lock (); // locked cannot release Sleep (5000); if (sh) {std: cout <* sh <endl ;} else {std: cout <"pointer released" <endl;} return 0;} void main123 () {boost: shared_ptr
Sh (new int (99); boost: weak_ptr
Pw (sh); HANDLE threads [2]; threads [0] = CreateThread (0, 0, reset, & sh, 0, 0 ); // create a thread threads [1] = CreateThread (0, 0, print, & pw, 0, 0); Sleep (1000); WaitForMultipleObjects (2, threads, TRUE, INFINITE); // wait for the thread to end cin. get ();}
Boost multi-thread lock
Thread. cpp
#include
#include
#include#include
#include
using namespace std;using namespace boost;void wait(int sec){boost::this_thread::sleep(boost::posix_time::seconds(sec));}void threadA(){for (int i = 0; i < 10;i++){wait(1);std::cout << i << endl;}}void threadB(){try{for (int i = 0; i < 10; i++){wait(1);std::cout << i << endl;}}catch (boost::thread_interrupted &){}}void mainO(){boost::thread t(threadA );wait(3);//t.interrupt();t.join();cin.get();}
Hash Database
Unorderred. cpp
#include
#include
#include
using namespace std;void mainAAAC(){boost::unordered_set
myhashset;myhashset.insert("ABC");myhashset.insert("ABCA");myhashset.insert("ABCAG");for (auto ib = myhashset.begin(); ib != myhashset.end();ib++){cout << *ib << endl;}std::cout << (myhashset.find("ABCA1") != myhashset.end()) << endl;cin.get();}
Regular Expression
Regex. cpp
# Include
# Include
# Include
# Include
Using namespace std; void mainA123 () {std: locale: global (std: locale ("English"); string str = "chinaen8Glish"; boost :: regex expr ("\ w + \ d \ u \ w +"); // d indicates a number, // match is 1, 0 cout <boost: regex_match (str, expr) <endl; cin. get ();} void mainB123 () {// std: locale: global (std: locale ("English"); string str = "chinaen8Glish9abv"; boost:: regex expr ("(\ w +) \ d (\ w +)"); // d indicates a number, boost: smatch what; if (boost :: regex_search (str, what, expr) // retrieves {cout <what [0] <endl; cout <what [1] <endl;} By expression ;} else {cout <"retrieval failed";} cin. get ();} void mainC1234 () {string str = "chinaen8 Glish9abv"; boost: regex expr ("\ d"); // d indicates a number, string kongge = "______"; std: cout <boost: regex_replace (str, expr, kongge) <endl; cin. get ();}