程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 【C/C++學院】0904-boost智能指針/boost多線程鎖定/哈希庫/正則表達式

【C/C++學院】0904-boost智能指針/boost多線程鎖定/哈希庫/正則表達式

編輯:C++入門知識

【C/C++學院】0904-boost智能指針/boost多線程鎖定/哈希庫/正則表達式


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();//存儲數組的指針
	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 
#include 

using namespace std;

using namespace boost;

//綁定函數的默認值,繼承二進制函數類的所有類容
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設置默認參數調用,函數副本機制,不能拷貝構造

	cin.get();
}

Fun.cpp

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

using namespace boost;



void mainC()
{
    //atoi  //char * to  int
	boost::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)//綁定調用
	{
		workid = newid;
	} 
public:
	boost::function workid;
};

class worker
{
public:
	void run(int toid)
	{
		id = toid;
		cout << id << "工作" << endl;
	}
public:
	int id;
};


void mainE()
{
	manager m;
	worker w;
	//類的成員函數需要對象來調用,綁定了一個默認的對象
	m.setcallback(boost::bind(&worker::run, &w, _1));

	m.allstart();

	cin.get();
}

Ref.cpp

#include 
#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()
{
	//不可以拷貝的對象可以用ref
	boost::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智能指針

RAII原理.cpp

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 

using namespace std;

class mystr
{
public:
	char *p = nullptr;
public:
	mystr(const char *str)
	{
		cout << "構建" << endl;
		int length = strlen(str);
		p = new char[length + 1];
		strcpy(p, str);
		p[length] = '\0';
	}
	~mystr()
	{
		cout << "銷毀" << endl;
		delete[] p;
	}
};

void go()
{
	char *p = new char[100];
	mystr str1 = "ABCD";//RAII避免內存洩漏,一般情況下,堆上的內存當作棧上來使用
	//棧內存有限,希望自動釋放,用很大的內存。
}

void mainHG()
{
	go();

	cin.get();
}

Smartpointer原理.cpp

#include 
#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);//智能指針,智能釋放
	//*p;
}

void mainH()
{
	run();
	
	cin.get();
}

Smartpointer.cpp

#include 
#include 
#include
#include 
#include 
#include 
#include 
#include
#include 

using namespace std;

void mainI()
{
	boost::scoped_ptr p(new int);//自動釋放內存
	*p = 12;
	cout << *p.get() << endl;
	p.reset(new int);
	*p.get() = 3;
	boost::scoped_ptr pA(nullptr);//獨占內存
	//pA = p;

	cout << *p.get() << endl;
	cin.get();
}

void mainG()
{
	boost::scoped_array p(new int[10]);//自動釋放內存
	//boost::scoped_array pA(p);獨享指針
	*p.get() = 1;
	p[3] = 2;
	p.reset(new int[5]);//只能指針

	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);//拷貝
	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();//鎖定不可以釋放
	Sleep(5000);
	if (sh)
	{
		std::cout << *sh << endl;
	}
	else
	{
		std::cout << "指針已經被釋放" << 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);//創建一個線程
	threads[1] = CreateThread(0, 0, print, &pw, 0, 0);
	Sleep(1000);

	WaitForMultipleObjects(2, threads, TRUE, INFINITE);//等待線程結束

	cin.get();
}

boost多線程鎖定

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();
}

哈希庫

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();
}

正則表達式

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代表數字,
	//匹配就是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代表數字,
	boost::smatch what;
	if (boost::regex_search(str,what,expr))//按照表達式檢索
	{
		cout << what[0] << endl;
		cout << what[1] << endl;		
	}
	else
	{
		cout << "檢索失敗";
	}
	cin.get();
}

void   mainC1234()
{
	string  str = "chinaen8  Glish9abv";
	boost::regex expr("\\d");//d代表數字,
	string  kongge = "______";
	std::cout << boost::regex_replace(str, expr, kongge) << endl;

	cin.get();
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved