liyanbo的个人博客分享 http://blog.sciencenet.cn/u/liyanbo

博文

代码研讨会3

已有 1902 次阅读 2016-2-24 22:10 |个人分类:C++|系统分类:科研笔记

第一部分:C++常用函数

1、getopt

参见http://blog.sciencenet.cn/blog-1515646-908824.html

2、T accumulate(InputIterator first,  InputIterator last, T init, BinaryOPeration binary_op) 与boost::bind

最常见用法:

struct comp {
   pair <double, double> operator() (const pair<double, double>& init, double x) const {
     return make_pair(init.first+x, init.second+x*x);
   }
};
pair <double,double> re = accumulate(test.begin(),test.end(),make_pair(0,0),comp());


与boost::bind结合

pair <double, double> sum(const pair<double, double>& init, double x) {
     return make_pair(init.first+x, init.second+x*x);
}
pair <double,double> re2 = accumulate(test.begin(),test.end(),make_pair(0,0),boost::bind(sum,_1,_2));


如果函数的参数超过两个,也可以用boost::bind适配:

pair <double, double> sum2(const pair<double, double>& init, double x, double y) {
     return make_pair(init.first+x + y, init.second+x*x);
}

double y= 10.0;
   pair <double,double> re3 = accumulate(test.begin(),test.end(),make_pair(0,0), boost::bind(sum2,_1,_2,y));

pair <double,double> re4 = boost::bind(sum2,_1,_2,y)(make_pair(0,0),10);

bind类中函数
class A {
public:
   pair <double, double> sum(const pair<double, double>& init, double x, double y) const {
     return make_pair(init.first+x+y, init.second+x*x);
   }
};
A a;
   pair <double,double> re5 = accumulate(test.begin(),test.end(),make_pair(0,0),boost::bind((&A::sum),&a,_1,_2,y));


3、Function for_each(InputIterator first,  InputIterator last, Function fn) ;

可以在cplusplus上查algorithm中的常用函数


4、 priority_queue,堆

   stack <int, vector<int> > S;
   stack <int, list<int> > S2;
   priority_queue<int, vector<int>, greater<int> > Q;//默认大顶堆,第二个指定实现的底层容器,改为了小顶堆

   for (int i=0; i < 10; ++i) {
       Q.push(i);
   }
   while (!Q.empty()) {
       cout << Q.top() << endl;
       Q.pop();
   }

bind2nd等适配器的使用,cplusplus.


第二部分:log4cxx

举个例子:

#include <algorithm>
#include <iostream>
#include <string>

#include <boost/format.hpp>
#include <boost/filesystem.hpp>

#include <log4cxx/logger.h>
#include <log4cxx/basicconfigurator.h>
#include <log4cxx/propertyconfigurator.h>

using namespace std;

static log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger("arcs.main"));

int main () {
   string log_config("log.config");

   if (boost::filesystem::exists(log_config)) {
       log4cxx::PropertyConfigurator::configure(log_config);
   } else {
       log4cxx::BasicConfigurator::configure();
   }
   
   int a = 10, b = 10;
   LOG4CXX_ERROR(logger, boost::format("a = %d b = %d") % a % b);
   return 0;
}


第三部分:boost库

boost库实现多线程:

#include <iostream>

#include <boost/thread/thread.hpp>
#include <boost/thread/locks.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/bind.hpp>

using namespace std;

boost::mutex mtx;
void show(int i) {
   boost::unique_lock< boost::mutex > lock(mtx);//互斥锁
   //mtx.lock();
   cout << "wangbing" << i << endl;
   //mtx.unlock();
}

int main() {
   int thread_num = 8;
   boost::thread_group group;
   for(int i = 0; i < thread_num; ++i) {
       group.create_thread( boost::bind(show, i));
   }
   cout << "xxxxxx" << endl;
   group.join_all();//等所有进程都运行结束
   return 0;
}






https://blog.sciencenet.cn/blog-1515646-958412.html

上一篇:代码研讨会1
下一篇:STL中的数据结构-序列式容器
收藏 IP: 159.226.43.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-7-17 19:36

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部