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;
}
一切源于对计算机的热爱

浙公网安备 33010602011771号