本文章所有内容源于《BOOST程序库完全开发指南:深入C++“准”标准库(第3版)》第八章
1. foreach
1.1 功能:
遍历循环。
for( int i = 0 ; i < 10 ; i++ ){ }
for( p = c.begin(); p != c.end(); p++ ){ }
for ( auto &x:vec ){ } //vec是一个容器,如vector<int> //c++11才有
1.2 头文件:
#include <boost/foreach.hpp>
1.3 用法:
BOOST_FOREACH和BOOST_REVERSE_FOREACH进行正向遍历和反向遍历,注意使用时序列长度是固定的,不会动态变化
#include <iostream>
using namespace std;
#include <boost/foreach.hpp>//遍历循环
#include <boost/assign.hpp>//列表
void case1()
{
using namespace boost::assign; //初始化assign
vector<int> v = (list_of(1),2,3,4,5);
BOOST_FOREACH( auto x , v )
{
cout << x << ", ";
}
cout << endl;
string str("boost foreach") ;
//这里也可以写c,但是经常用const或&修饰,推导常量或引用类型,使用auto&最好,可以避免无用的拷贝
BOOST_REVERSE_FOREACH( auto &c,str )
{
cout << c << "- ";
}
cout << endl;
cout <<"---------------------------------------------------------"<<endl;
}
int main()
{
case1();
}
#include <iostream>
using namespace std;
#include <boost/foreach.hpp>//遍历循环
#include <boost/assign.hpp>//列表
//更改宏的名字,使代码更好看
#define foreach BOOST_FOREACH
#define reverse_foreach BOOST_REVERSE_FOREACH
void case1()
{
using namespace boost::assign;
vector <int> v = (list_of(1),2,3,4,5);
foreach( auto&x , v )
{
cout << x <<", ";
}
cout << endl;
//遍历普通数组
int arr[] = {1,2,3,4,5,6};
reverse_foreach( auto&y, arr )
{
cout << y << "-";
}
cout << endl;
//遍历map
map<int, string> m = { {1,"aaa"}, {2,"bbb"}, {3,"ccc"}, {4,"ddd"} };
for (auto v: m)
{
cout << v.first<< v.second<<endl;
}
foreach(auto& z, m)
{
cout << z.first <<" " << z.second <<endl; //注意这里访问时候的方法
}
//用vector实现二维数组
vector<vector<int>> vss = {{1,3},{2,4}};
reverse_foreach(auto&w, vss)
{
reverse_foreach( auto& wm, w )
{
cout << wm << ", ";
}
cout << endl;
}
cout << "------------------------"<<endl;
}
#include <boost/array.hpp>
#include <boost/circular_buffer.hpp>
#include <boost/unordered_set.hpp>
void case2()
{
//遍历array
using namespace boost::assign;
boost::array<int,5> ar = (list_of(1),2,3,4,5); //注意这里是boost::array
foreach( int xx ,ar )
{
cout << xx << " "<<",";
}
cout << endl;
//迭代其的pair
pair< decltype(ar.begin()), decltype(ar.end()) > rng(ar.begin(),ar.end()-2);
foreach(auto x, rng)
cout << x << " ";
cout << endl;
//循环缓冲区boost::circular_buffer
boost::circular_buffer<int> cn = list_of(1)(2)(3);
foreach(auto x, cn)
{
cout << x << " ";
}
cout << endl;
//无序容器
boost::unordered_set<double> us = list_of(3.14)(2.717)(0.618);
foreach(auto x, us)
cout << x << " ";
cout << endl;
}
int main()
{
case1();
case2();
}
2. minmax
2.1 功能:
可以在一次处理中同时获得最大最小值,提高执行效率
2.2 头文件:
#include <boost/algorithm/minmax.hpp>
using namespace boost;
2.3 用法:
同时返回两个数值,故返回的是一个包含两个元素的tuple类型:tuple<T const& , T const& >,最好用auto来自动获得返回值的类型定义,第一个元素为最小,第二个为最大,可以使用boost::get()访问。
auto x= minmax(1,2);
cout << get<0>(x) << " " << get<1>(x);
#include <iostream>
using namespace std;
void case1()
{
cout << min(200,13) << endl;
cout << max(111,31) << endl;
}
#include <boost/algorithm/minmax.hpp>
void case2()
{
auto x= minmax(1,2);
cout << get<0>(x) << " " << get<1>(x) <<endl;
}
void case3()
{
string s1("5000"), s2("123");
auto x = std::minmax(s1,s2);
cout << get<0>(x) << " " << get<1>(x) << endl;
auto y = std::minmax({1,23,4,56,44}); //注意括号
cout << get<0>(y) << " " << get<1>(y) << endl;
}
int main(int argc, char** argv)
{
case1();
case2();
case3();
}
3. minmax_element
3.1 功能:
minmax_element组件包含一组算法,改进了std::min_element()和std::max_element()函数,可同时返回容器的最大最小值。
3.2 头文件:
#include <boost/alogrithm/minmax_element.hpp>
using namespace boost;
3.3 用法:
#include <iostream>
using namespace std;
#include <boost/algorithm/minmax_element.hpp>
using namespace boost;
#include<vector>
void case1()
{
vector<int> v = {313,1313,1344,555,66,3};
auto x= boost::minmax_element(v.begin(), v.end());
cout << "min: " << *x.first << endl;
cout << "max: " << *x.second<<endl;
}
#include <boost/assign.hpp>
void case2()
{
vector<int> v = {3,5,2,2,10,9,10,8};
//2, 2, 3, 5, 8, 9, 10,10
decltype(v.begin()) pos; //保存查找到的迭代器的位置
pos = first_min_element(v.begin(),v.end()); //找到第一个最小值
assert(pos - v.begin() == 2); //v[2]
pos = last_min_element(v.begin(),v.end()); //最后一个最小值
assert(pos - v.begin() == 3); //v[3]
auto x = first_min_last_max_element(v.begin(),v.end()); //第一个最小值和最后一个最大值
assert(x.first - v.begin() == 2 && x.second - v.begin() ==6);
}
int main()
{
case1();
case2();
}
4. algorithm
4.1 功能:
algoritm库是一个算法的集合,包含了很多小的算法比如all_of、none_of、equal、Boyer-Moore Search、KMP等等,这里至介绍clamp和hex。
4.2 头文件:
clamp:找到夹在别人中间的值
clamp_range:对一组元素执行clamp算法,然后把结果写入到一个输出迭代器里
#include<boost/algorithm/clamp.hpp>
using namespace boost::alogrithm;
#include <iostream>
using namespace std;
#include <boost/algorithm/clamp.hpp>
using namespace boost::algorithm;
#include <vector>
void case1()
{
assert(clamp(5, 1, 10) == 5);
assert(clamp(5, 5, 10) == 5);
assert(clamp(5, 1, 5) == 5);
assert(clamp(5, 10, 15) == 10);
assert(clamp(5, 0, 4) == 4);
}
void case2()
{
vector<int> v= {2,4,6,8,10};
clamp_range(v,ostream_iterator<int>(cout,","),3,9);
}
int main()
{
case1();
case2();
}
hex:用来执行十六进制的编码和解码,hex和unhex互逆
#include <boost/algorithm/hex.hpp>
using namespace boost::algorithm;
using namespace std;
#include <boost/algorithm/hex.hpp>
using namespace boost::algorithm;
#include<iostream>
void case1()
{
string s;
hex("123",ostream_iterator<char>(cout));//编码ASCII字符123
cout << endl;
hex("ABC", std::back_inserter(s));
cout << s << endl;
unhex(s, ostream_iterator<char>(cout));
cout << endl;
hex("+-*/", ostream_iterator<char>(cout));
cout << endl;
hex_lower("+-*/", ostream_iterator<char>(cout));
cout << endl;
}
void case2()
{
try
{
unhex("313", ostream_iterator<char>(cout));
unhex("xyz", ostream_iterator<char>(cout));
}
catch(const boost::exception& e) //捕获
{
cout << "unhex error" << endl;
}
}
int main()
{
case1();
case2();
}