• 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;
    }
    
  • 相关阅读:
    python爬取糗事百科段子
    python爬虫-韩寒新浪博客博文
    python-函数
    Python-列表
    Linux学习笔记001——win下安装Linux虚拟机
    python爬虫-正则表达式
    IIS使用十大原则,(IIS过期时间,IIS缓存设置) 【转载】
    在mysql 中两种锁定问题
    php 迭代器与和生成器
    DBCP连接池使用问题
  • 原文地址:https://www.cnblogs.com/liuweilinlin/p/3264064.html
Copyright © 2020-2023  润新知