C++11 如何在boost 1.55中使用boost::split和boost::string_ref

C++11 如何在boost 1.55中使用boost::split和boost::string_ref,c++11,boost,C++11,Boost,守则: #include <iostream> #include <boost/algorithm/string.hpp> #include <boost/utility/string_ref.hpp> int main() { boost::string_ref str = "test_the_world"; std::vector<boost::string_ref> strs; boost::split(strs,

守则:

#include <iostream>
#include <boost/algorithm/string.hpp>
#include <boost/utility/string_ref.hpp>

int main() 
{
    boost::string_ref str = "test_the_world";
    std::vector<boost::string_ref> strs;
    boost::split(strs, str, boost::is_any_of("_"), boost::token_compress_on);
    for (auto& v : strs)
    {
        std::cout << v << std::endl;
    }
    return 0;
}
#包括
#包括
#包括
int main()
{
boost::string\u ref str=“测试世界”;
std::载体strs;
boost::split(strs、str、boost::是(“"”)中的任意一个,boost::token\u compress\u on);
用于(自动和验证:strs)
{
std::cout C:\boost\u 1\u 55\u 0\boost/utility/string\u ref.hpp(79):可以是“boost::basic\u string\u ref::basic\u string\u ref.(常量图表*,boost::basic\u string\u ref::size\u type)”
1> 与
1>          [
1> charT=char
1>          ]
1> 尝试匹配参数列表时“(常量字符*,常量字符*)”
1> C:\boost\u 1\u 55\u 0\boost/algorithm/string/detail/util.hpp(97):请参阅正在编译的函数模板实例化“SeqT boost::copy\u range(const range&)”的参考
1> 与
1>          [
1> SeqT=boost::基本字符串参考
1> ,Range=boost::迭代器\范围
1>          ]
1> C:\boost\u 1\u 55\u 0\boost/algorithm/string/detail/util.hpp(96):在编译类模板成员函数'boost::basic\u string\u ref boost::algorithm::detail::copy\u iterator\u rangeF::operator()(const boost::iterator\u range&)const'

等等..我怎样才能使它工作?不明白为什么它不工作?

廉价黑客:专门化
字符串引用的
复制范围,因为它不需要迭代器对

namespace boost
{
    template <>
    inline string_ref
    copy_range<string_ref, iterator_range<char const*>>
      ( iterator_range<char const*> const& r )
    {
        return string_ref( begin(r),  end(r) - begin(r) );
    }
}
namespace boost
{
模板
内联字符串\u ref
复制范围
(迭代器_范围常数&r)
{
返回字符串_ref(开始(r),结束(r)-开始(r));
}
}


然而,肯定有更好的解决方案,所以我会继续寻找。

Boost
iter\u split
非常适合这一点,尽管它自然倾向于
iterator\u range
而不是
string\u ref


嗯。我对此感到非常困惑。复制范围真的不应该进行用户定义的转换(超出人们从文档中期望的隐式转换)。然后,这又是非常优雅的。我在我的答案中创建了更传统的解决方案,用于比较,定义为“干净”回答,+1。@sehe我意识到时间有点晚了,但我只是想指出,您的C++03版本并不是真正的C++03。我尝试对其进行调整,但仍然失败(使用
boost::phoenix
namespace boost
{
    template <>
    inline string_ref
    copy_range<string_ref, iterator_range<char const*>>
      ( iterator_range<char const*> const& r )
    {
        return string_ref( begin(r),  end(r) - begin(r) );
    }
}
using R = boost::iterator_range<std::string::const_iterator>;
using V = std::vector<R>;
V v;

for (auto&& r : iter_split(v, input, token_finder(is_any_of(";"))))
    std::cout << r << "\n";
for (string_ref&& r : iter_split(v, input, token_finder(is_any_of(";"))) 
    | transformed([](R const& r){return boost::string_ref(&*r.begin(), r.size());})
    std::cout << r << "\n";
for (string_ref&& r : iter_split(v, input, token_finder(is_any_of(";"))) 
         | transformed(phx::construct<boost::string_ref>(&*phx::begin(_1), phx::size(_1))))
    std::cout << r << "\n";
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/finder.hpp>
#include <boost/range/adaptor/transformed.hpp>
#include <boost/phoenix.hpp>
#include <boost/phoenix/stl.hpp>
#include <boost/utility/string_ref.hpp>
#include <iostream>

using namespace boost::algorithm;
namespace phx = boost::phoenix;
using namespace phx::arg_names;
using boost::adaptors::transformed;

int main() {
    std::string input = "1;3;5;7";

    using boost::string_ref;
    using R = boost::iterator_range<std::string::const_iterator>;
    using V = std::vector<R>;
    V v;

    // just the iterator ranges:
    for (auto&& r : iter_split(v, input, token_finder(is_any_of(";"))))
        std::cout << r << "\n";

    // using a lambda to create the string_refs:
    for (string_ref&& r : iter_split(v, input, token_finder(is_any_of(";"))) 
            | transformed([](R const& r){return string_ref(&*r.begin(), r.size());}))
        std::cout << r << "\n";

    // c++03 version:
    for (string_ref&& r : iter_split(v, input, token_finder(is_any_of(";"))) 
            | transformed(phx::construct<string_ref>(&*phx::begin(_1), phx::size(_1))))
        std::cout << r << "\n";
}