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;
    }
    
  • 相关阅读:
    搜索框下拉列表
    定时器修改button标题闪烁
    按钮设置文字图片排版
    SSKeychain
    IQKeyboardManager
    App内存性能优化
    支付宝集成
    友盟分享
    iOS 线程同步 加锁 @synchronized
    iOS 线程同步-信号量 dispatch_semaphore
  • 原文地址:https://www.cnblogs.com/liuweilinlin/p/3264064.html
Copyright © 2011-2022 走看看