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
    评论
boost::join函数是一个模板函数,用于将一个包含字符串的容器连接成一个字符串,并用指定的分隔符分隔每个连接段。它的输入参数是一个包含字符串的容器和一个分隔符字符串,返回值是一个连接后的字符串。 在引用的代码示例,首先定义了一个vector容器vct_str,并向其添加了一个字符串"123"。然后使用boost::join函数将vct_str的字符串连接起来,使用逗号作为分隔符,将连接结果存储在str2并打印出来。接着向vct_str添加了另一个字符串"456",再次使用boost::join函数将vct_str的字符串连接起来,同样使用逗号作为分隔符,将连接结果存储在str3并打印出来。 需要注意的是,在使用boost::join函数前,需要包含相应的头文件,并链接相应的库。具体的头文件路径和库路径可以参考引用的代码示例。 总结来说,boost::join函数可以方便地将一个容器的字符串连接成一个字符串,并可指定分隔符进行分隔。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [boost::join](https://blog.csdn.net/u011743621/article/details/131822780)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [boost 1.61 编译好的包 win32 TDM-GCC5.1.0 static 这个才是32位的](https://download.csdn.net/download/www_wudimei_com/9549613)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值