boost用法【转】

/  示例1
#include 
< string >  
#include 
< iostream >  
#include 
< boost / regex.hpp >  

int  main() { 
      boost::regex reg(
" \\d+ " ); 
      boost::smatch m; 
      std::
string  str( " my phone is 88886666 and age is 25 " ); 
      std::
string ::const_iterator it  =  str.begin(); 
      std::
string ::const_iterator end  =  str.end(); 
    
while (boost::regex_search(it, end, m, reg)) { 
          std::cout 
<<  m.str()  <<  std::endl; 
          it 
=  m[ 0 ].second; 
      } 
      boost::regex my_reg(
" My " , boost::regex::icase  |  boost::regex::perl); 
      std::
string  res  =  boost::regex_replace(str, my_reg,  " Solee's " ); 
      std::cout 
<<  res  <<  std::endl; 
    
return   0
}

//  示例2
//  使用迭代器获取所有匹配
#include  < boost / regex.hpp >  
#include 
< iostream >  
#include 
< string >  
#include 
< algorithm >  
using   namespace  std; 

class  Show { 
public :     
    template
< class  T >  
    
void   operator ()( const  T &  t) 
         cout 
<<  t.str()  <<  endl; 
     } 
}; 

int  main( int  argc,  char *  argv[]) 

     boost::regex reg(
" \\d+ " ); 
     
string  str( " my num is 21314 and age is 26. " ); 
     boost::sregex_iterator it(str.begin(), str.end(), reg); 
     boost::sregex_iterator end; 
     for_each(it, end, Show()); 
    
return   0
}

转载于:https://www.cnblogs.com/NeuqUstcIim/archive/2008/12/25/1361995.html

Boost库是一个跨平台的C++库,提供了许多多线程编程的工具和类。下面是使用Boost库进行多线程编程的一些方法: 1. 创建线程 ```cpp #include <boost/thread.hpp> void my_thread_func() { // 线程执行的代码 } int main() { // 创建新线程 boost::thread my_thread(my_thread_func); // 等待线程结束 my_thread.join(); return 0; } ``` 2. 线程同步和互斥 ```cpp #include <boost/thread.hpp> boost::mutex my_mutex; void my_thread_func() { // 申请互斥锁 boost::mutex::scoped_lock lock(my_mutex); // 互斥锁保护的代码 } int main() { // 创建新线程 boost::thread my_thread(my_thread_func); // 等待线程结束 my_thread.join(); return 0; } ``` 3. 线程池 ```cpp #include <boost/thread.hpp> #include <boost/bind.hpp> void my_thread_func(int arg) { // 线程执行的代码 } int main() { // 创建线程池 boost::thread_pool my_thread_pool(4); // 提交任务到线程池 for (int i = 0; i < 10; ++i) { my_thread_pool.submit(boost::bind(my_thread_func, i)); } // 等待任务完成 my_thread_pool.wait(); return 0; } ``` 4. 条件变量 ```cpp #include <boost/thread.hpp> #include <boost/date_time.hpp> boost::mutex my_mutex; boost::condition_variable my_cond_var; void my_thread_func() { // 申请互斥锁 boost::mutex::scoped_lock lock(my_mutex); // 等待条件变量 my_cond_var.wait(lock); // 条件满足后继续执行 } int main() { // 创建新线程 boost::thread my_thread(my_thread_func); // 等待一段时间 boost::this_thread::sleep(boost::posix_time::seconds(2)); // 通知条件变量 my_cond_var.notify_one(); // 等待线程结束 my_thread.join(); return 0; } ``` 以上是使用Boost库进行多线程编程的一些方法,可以根据实际需求进行选择和组合,以实现更加复杂的多线程应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值