zoukankan      html  css  js  c++  java
  • boost之算法

    STL里的算法已经很好了,在boost里有几个小的算法

    1.BOOST_FOREACH使用方法,定义一个容器里内部类型数据,容器作为参数传递。

    #include <iostream>
    #include <string>
    #include <vector>
    #include <boost/assign.hpp>
    #include <boost/foreach.hpp>
    using namespace std;
    using namespace boost::assign;
    
    
    int main()
    {
    	
    	vector<int> v = list_of(1)(2)(3)(4)(5);
    	BOOST_FOREACH(int x,v)
    	{
    		cout << x << ",";
    	}
    	cout << endl;
    	string str("boost foreach");
    	BOOST_FOREACH(char c,str)
    	{
    		cout << c << "-";
    	}
    	cout << endl;
    	return 0;
    }
    

     2.minmax同时返回两个数最大值和最小值,返回类型为tuple,使用方法:

    #include <iostream>
    #include <string>
    #include <vector>
    #include <boost/assign.hpp>
    #include <boost/typeof/typeof.hpp>
    #include <boost/algorithm/minmax.hpp>
    #include <boost/tuple/tuple.hpp>
    using namespace std;
    using namespace boost::assign;
    
    
    int main()
    {
    
    	BOOST_AUTO(x,boost::minmax(100,200));
    	cout << x.get<1>() << " " << x.get<0>() <<endl;
    	return 0;
    }
    

     3.minmax_element()用于找出容器中的最大值和最小值。

    #include <iostream>
    #include <string>
    #include <vector>
    #include <boost/assign.hpp>
    #include <boost/typeof/typeof.hpp>
    #include <boost/algorithm/minmax_element.hpp>
    #include <boost/tuple/tuple.hpp>
    using namespace std;
    using namespace boost::assign;
    using namespace boost;
    
    int main()
    {
    	vector<int> v = list_of(633)(90)(67)(83)(2);
    	BOOST_AUTO(x,boost::minmax_element(v.begin(),v.end()));
    	cout << "min: " << *x.first <<endl;
    	cout << "max: " << *x.second <<endl;
    
    	return 0;
    }
    
  • 相关阅读:
    Using Resource File on DotNet
    C++/CLI VS CSharp
    JIT VS NGen
    [Tip: disable vc intellisense]VS2008 VC Intelisense issue
    UVa 10891 Game of Sum(经典博弈区间DP)
    UVa 10723 Cyborg Genes(LCS变种)
    UVa 607 Scheduling Lectures(简单DP)
    UVa 10401 Injured Queen Problem(简单DP)
    UVa 10313 Pay the Price(类似数字分解DP)
    UVa 10635 Prince and Princess(LCS N*logN)
  • 原文地址:https://www.cnblogs.com/liuweilinlin/p/3264064.html
Copyright © 2011-2022 走看看