Boost中string模块的join用法

头文件

boost/algorithm/string/join.hpp

作用

string的join系列 包括如下API

join:将字符串 列表,使用指定的字符,连接起来,返新的字符串。

join_if:将字符串 列表,满足特定条件,使用指定的字符,连接起来,返新的字符串。

举例

#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/classification.hpp>
// equals predicate is used for result comparison
#include <boost/algorithm/string/predicate.hpp>

// Include unit test framework
#include <boost/test/unit_test.hpp>

#include <string>
#include <vector>
#include <iostream>

#include <boost/test/test_tools.hpp>


using namespace std;
using namespace boost;

bool is_not_empty(const std::string& str)
{
    return !str.empty();
}

void join_test()
{
    // Prepare inputs
    vector<string> tokens1;
    tokens1.push_back("xx");
    tokens1.push_back("abc");
    tokens1.push_back("xx");

    vector<string> tokens2;
    tokens2.push_back("");
    tokens2.push_back("xx");
    tokens2.push_back("abc");
    tokens2.push_back("");
    tokens2.push_back("abc");
    tokens2.push_back("xx");
    tokens2.push_back("");

    vector<string> tokens3;
    tokens3.push_back("");
    tokens3.push_back("");
    tokens3.push_back("");

    vector<string> empty_tokens;

    vector<vector<int> > vtokens;
    for(unsigned int n=0; n<tokens2.size(); ++n)
    {
        vtokens.push_back(vector<int>(tokens2[n].begin(), tokens2[n].end()));
    }

    BOOST_CHECK( equals(join(tokens1, "-"), "xx-abc-xx") );
    BOOST_CHECK( equals(join(tokens2, "-"), "-xx-abc--abc-xx-") );
    BOOST_CHECK( equals(join(vtokens, "-"), "-xx-abc--abc-xx-") );
    BOOST_CHECK( equals(join(empty_tokens, "-"), "") );

    BOOST_CHECK( equals(join_if(tokens2, "-", is_not_empty), "xx-abc-abc-xx") );
    BOOST_CHECK( equals(join_if(empty_tokens, "-", is_not_empty), "") );
    BOOST_CHECK( equals(join_if(tokens3, "-", is_not_empty), "") );
}


int main(int argc, char* [])
{
    join_test();

    return 0;
}

源代码

namespace boost {
    namespace algorithm {

//  join --------------------------------------------------------------//

        //! Join algorithm
        /*!
            This algorithm joins all strings in a 'list' into one long string.
            Segments are concatenated by given separator.

            \param Input A container that holds the input strings. It must be a container-of-containers.
            \param Separator A string that will separate the joined segments.
            \return Concatenated string.

            \note This function provides the strong exception-safety guarantee
        */
        template< typename SequenceSequenceT, typename Range1T>
        inline typename range_value<SequenceSequenceT>::type 
        join(
            const SequenceSequenceT& Input,
            const Range1T& Separator)
        {
            // Define working types
            typedef typename range_value<SequenceSequenceT>::type ResultT;
            typedef typename range_const_iterator<SequenceSequenceT>::type InputIteratorT;

            // Parse input
            InputIteratorT itBegin=::boost::begin(Input);
            InputIteratorT itEnd=::boost::end(Input);

            // Construct container to hold the result
            ResultT Result;
            
            // Append first element
            if(itBegin!=itEnd)
            {
                detail::insert(Result, ::boost::end(Result), *itBegin);
                ++itBegin;
            }

            for(;itBegin!=itEnd; ++itBegin)
            {
                // Add separator
                detail::insert(Result, ::boost::end(Result), ::boost::as_literal(Separator));
                // Add element
                detail::insert(Result, ::boost::end(Result), *itBegin);
            }

            return Result;
        }

// join_if ----------------------------------------------------------//

        //! Conditional join algorithm
        /*!
            This algorithm joins all strings in a 'list' into one long string.
            Segments are concatenated by given separator. Only segments that
            satisfy the predicate will be added to the result.

            \param Input A container that holds the input strings. It must be a container-of-containers.
            \param Separator A string that will separate the joined segments.
            \param Pred A segment selection predicate
            \return Concatenated string.

            \note This function provides the strong exception-safety guarantee
        */
        template< typename SequenceSequenceT, typename Range1T, typename PredicateT>
        inline typename range_value<SequenceSequenceT>::type 
        join_if(
            const SequenceSequenceT& Input,
            const Range1T& Separator,
            PredicateT Pred)
        {
            // Define working types
            typedef typename range_value<SequenceSequenceT>::type ResultT;
            typedef typename range_const_iterator<SequenceSequenceT>::type InputIteratorT;

            // Parse input
            InputIteratorT itBegin=::boost::begin(Input);
            InputIteratorT itEnd=::boost::end(Input);

            // Construct container to hold the result
            ResultT Result;

            // Roll to the first element that will be added
            while(itBegin!=itEnd && !Pred(*itBegin)) ++itBegin;
            // Add this element
            if(itBegin!=itEnd)
            {
                detail::insert(Result, ::boost::end(Result), *itBegin);
                ++itBegin;
            }

            for(;itBegin!=itEnd; ++itBegin)
            {
                if(Pred(*itBegin))
                {
                    // Add separator
                    detail::insert(Result, ::boost::end(Result), ::boost::as_literal(Separator));
                    // Add element
                    detail::insert(Result, ::boost::end(Result), *itBegin);
                }
            }

            return Result;
        }

    } // namespace algorithm

    // pull names to the boost namespace
    using algorithm::join;
    using algorithm::join_if;

} // namespace boost

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值