c++11 boost - 算法 foreach、minmax

2018-07-12 18:18:28

c++11标准中新增了如下语法

for(arto& x:vec){
...
}

foreach 库实现了正向和反向遍历,遍历的时候不能改变容器的长度。

// Copyright (c) 2015
// Author: Chrono Law
#include <iostream>
using namespace std;

#include <boost/foreach.hpp>
#include <boost/assign.hpp>

void case1()
{
    using namespace boost::assign;
    vector<int> v = (list_of(1),2,3,4,5);

    BOOST_FOREACH(auto x, v)
    {
        cout << x << ",";
    }
    cout << endl;

    string str("www.freecls.com");
    BOOST_FOREACH(auto& c, str)
    {
        cout << c << "-";
    }
	
	cout << endl;
	
    set<int> s = list_of(10)(20)(30);

    int x;
    //另一种语法,拷贝循环
    BOOST_FOREACH (x, s)
    {
        if (++x % 7 == 0)
        {
            cout << x << endl;
            break;
        }
    }
}

#define foreach BOOST_FOREACH
#define reverse_foreach BOOST_REVERSE_FOREACH

void case2()
{
    int ar[] = {1,2,3,4,5};
    foreach(auto& x, ar)
        cout << x << " ";
    cout << endl;

    map<int, string> m = {{1, "111"},{2, "222"},{3, "333"}};

    foreach(auto& x, m)
        cout << x.first << x.second << endl;

    vector< vector<int> > v = {{1,2},{3,4}};
    foreach(auto& row, v)
    {
        reverse_foreach(auto& z, row)
            cout << z << ",";
        cout << endl;
    }
}

#include <boost/array.hpp>
#include <boost/circular_buffer.hpp>
#include <boost/unordered_set.hpp>

void case3()
{
    using namespace boost::assign;

    boost::array<int, 5> ar = (list_of(1), 2, 3, 4, 5);
    foreach(auto x, ar)
        cout << x << " ";
    cout << endl;

    pair<decltype(ar.begin()), decltype(ar.end())>
        rng(ar.begin(), ar.end() -2);
    foreach(auto x, rng)
        cout << x << " ";
    cout << endl;

    boost::circular_buffer<int> cb = list_of(1)(2)(3);
    foreach(auto x, cb)
        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();
    case3();
}
[root@192 c++]# g++ -std=c++11 main.cpp
[root@192 c++]# ./a.out 
1,2,3,4,5,
w-w-w-.-f-r-e-e-c-l-s-.-c-o-m-
21
1 2 3 4 5 
1111
2222
3333
2,1,
4,3,
1 2 3 4 5 
1 2 3 
1 2 3 
0.618 2.717 3.14


minmax 库是对c++98 标准库中的算法 std::min/max、std::min_element/max_element 的增强,可在一次处理中同时获得最大最小值,执行效率提高了很多,已被收录c++标准。

// Copyright (c) 2015
// Author: Chrono Law
#include <iostream>
using namespace std;

#include <boost/algorithm/minmax.hpp>
//using namespace boost;

//////////////////////////////////////////

void case1()
{
    std::cout << std::min(200, 12) << std::endl;
    std::cout << std::max(200, 12) << std::endl;

	//第一个是最小值,第二个是最大值
    auto x = boost::minmax(2, 1);
    std::cout << boost::get<0>(x) << " " << boost::get<1>(x);
    std::cout << std::endl;

}

//std::minmax返回的是pair而不是tuple,用auto可以屏蔽这种区别
void case2()
{
    std::string s1("5000"), s2("123");

    //auto xx = boost::minmax(s1, s2);
    auto x = std::minmax(s1, s2) ;
    cout << get<0>(x) << " " << get<1>(x) << endl;

    auto y = std::minmax({3,4,8,1}) ;
    cout << get<0>(y) << " " << get<1>(y);

}

int main()
{
    case1();
    case2();
}
1 8[root@192 c++]# g++ -std=c++11 main.cpp
[root@192 c++]# ./a.out 
12
200
1 2
123 5000
1 8


// Copyright (c) 2015
// Author: Chrono Law
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;

#include <boost/algorithm/minmax_element.hpp>
//using namespace boost;

void case1()
{
    vector<int> v = {633, 90, 67,83, 2, 100};

    auto x = boost::minmax_element(v.begin(), v.end());
    cout << "min : " << *x.first << endl;
    cout << "max : "<< *x.second <<endl;

}

void case2()
{
    vector<int> v = {3,5,2,2,10,9,10,8};

    decltype(v.begin()) pos;
    pos = boost::first_min_element(v.begin(),v.end());  //第一个最小值
    assert(pos - v.begin() == 2);

    pos = boost::last_min_element(v.begin(),v.end());  //最后一个最小值
    assert(pos - v.begin() == 3);
	
	//第一个最小值,第后个最大值
    auto x = boost::first_min_last_max_element(v.begin(),v.end());
    assert(x.first - v.begin() == 2 &&
            x.second - v.begin() == 6);

}

int main()
{
    case1();
    case2();
}
[root@192 c++]# g++ -std=c++11 main.cpp
[root@192 c++]# ./a.out 
min : 2
max : 633


 备注

1.编译器版本gcc4.8.5,运行环境centos7 64位
2.本文只做简单记录用,详细用法请参考 Boost Library,或者是罗剑锋的 boost程序库完全开发指南 书本
3..原文地址http://www.freecls.com/a/2712/aa


©著作权归作者所有
收藏
推荐阅读
简介
天降大任于斯人也,必先苦其心志。