boost.algorithm boost.assign boost.lambda

// algrothim.cpp : 定义控制台应用程序的入口点。
//made by davdidsu33


//#include "stdafx.h"


#include <boost/config/warning_disable.hpp>
#include <boost/algorithm/minmax.hpp>
#include <boost/algorithm/minmax_element.hpp>
#include <boost/algorithm/hex.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string_regex.hpp>
#include <boost/algorithm/clamp.hpp>
#include <boost/typeof/typeof.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/algorithm/gather.hpp>


#include <boost/lambda/lambda.hpp>
#include <boost/function.hpp>


#include <algorithm>
#include <string>
#include <boost/assert.hpp>
#include <boost/assign.hpp>


void use_minmax()
{
	BOOST_AUTO( r ,boost::minmax(200, 30));
	BOOST_ASSERT(boost::get<0>(r) == 30);


	struct Compare
	{
		bool beyondNum(const std::string& s1)
		{
			for (int i=0; i<s1.size(); ++i)
			{
				if (s1[i]< '0' || s1[i]>'9')
				{
					return true;
				}
			}


			return true;
		}


		bool operator()(const std::string& s1, const std::string& s2)
		{
			if(beyondNum(s1))
				return true;


			if (beyondNum(s2))
				return false;


			return s1 <  s2;
		}
	};




	std::string s1("100");
	std::string s2("100a");


	//直接这么写会在boost1.5.5中报错,因为
	//cref可能是stl的也可能是boost的,所以无法正确解析
	//BOOST_AUTO(r2, boost::minmax<std::string>(s1, s2, Compare()));
	
	//正确做法
	//using namespace std;
	//BOOST_AUTO(r2, boost::minmax<std::string>(s1, s2));
	BOOST_AUTO(r3, boost::minmax(10, 20));




	//BOOST_ASSERT(boost::get<0>(r2).compare("100a") == 0);
	//BOOST_ASSERT(boost::get<1>(r2).compare("100") == 0);
}


void use_minmaxelement()
{
	using namespace boost::assign;
	std::vector<int> v;
	v += 1, 2,3,1,5,6,100,20,200,30,200;


	std::minmax_element(v.begin(), v.end(), std::less<int>());
	BOOST_AUTO(it, boost::minmax_element(v.begin(), v.end(), std::less<int>()));


	BOOST_ASSERT((*(it.first) == 1));
	BOOST_ASSERT((*(it.second)) == 200);


	BOOST_AUTO(firstMaxIt, boost::first_max_element(v.begin(), v.end()));
	int pos = firstMaxIt - v.begin();
	BOOST_ASSERT(pos == 8);
	BOOST_ASSERT(*firstMaxIt == 200);


	BOOST_AUTO(firstMinIt,boost::first_min_element(v.begin(), v.end()));
	pos = firstMinIt - v.begin();
	BOOST_ASSERT(pos == 0);
	BOOST_ASSERT(*firstMinIt == 1);


	std::vector<int> v2 = (list_of(20) (30) (40) (20) (50) (60) (90) (90));


	int stop = 100000000;
}


template<class T>
struct IsEven
{
	bool operator()(T const &val)
	{
		return (val % 2 == 0);
	}
};


void use_hex()
{
	std::string hex1 = boost::algorithm::hex<std::string>("1234");
	std::string hex2 = boost::algorithm::hex<std::string>("4567");
	//int hex3 = boost::algorithm::hex<int>(1234);


	std::string unhex1 = boost::algorithm::unhex(hex1);
	std::string unhex2 = boost::algorithm::unhex(hex2);


	//clamp(testvalue, lowvalue, highvalue);
	int r = boost::algorithm::clamp(10, 20, 30);
	BOOST_ASSERT(r == 20);


	r = boost::algorithm::clamp(21, 20, 30);
	BOOST_ASSERT(r == 21);


	r = boost::algorithm::clamp(31, 20, 30);
	BOOST_ASSERT(r == 30);


	//boost.algorithm.gather
	//临时结构体
	struct GetVal
	{
		GetVal(int val):m_startVal(val){};


		int operator()()
		{
			return ++m_startVal;
		}


	private:
		int m_startVal;
	};


	//局部函数
	using namespace boost::lambda;
	boost::function<int ()> f = GetVal(1);


	using namespace boost::assign;
	std::vector<int> v = list_of(1).repeat_fun(9, f);
	//或者
	//std::vector<int> v = list_of(1).repeat_fun(9,GetVal(1));
	


	//boost::algorithm::gather(v.begin(), v.end(), v.begin()+1, IsEven<int>());


	int stop = 0;
}




int main(int argc, char* argv[])
{
	use_minmax();
	use_minmaxelement();
	use_hex();


	getchar();
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值