头文件
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