【问题标题】:Using boost strings algorithms with iterators使用带有迭代器的 boost 字符串算法
【发布时间】:2014-04-18 15:41:12
【问题描述】:

我有一个用 2 个迭代器定义的字符串。我想检查它是否以一些字符串结尾。 现在我的代码看起来像

algorithm::ends_with(string(begin,end),"format(");

有没有什么方法可以在不构造字符串的情况下执行这个函数?类似的东西

algorithm::ends_with(begin,end,"format(");

【问题讨论】:

    标签: c++ string boost


    【解决方案1】:

    是的。

     std::string s = "something";
     bool b = boost::algorithm::ends_with( &s[0], "g");  // true
    

    迭代器也可以用来构造范围:

    #include <boost/range.hpp>
    
    std::string s = "somet0hing";
    std::string::iterator it = s.begin();
    bool b = boost::algorithm::ends_with( 
                            boost::make_iterator_range( it, s.end()), "g");  // true
    

    或:

    std::string s = "somet0hing";
    std::string::iterator it = s.begin();
    bool b = boost::algorithm::ends_with( &(*it), "g");  // true
    

    【讨论】:

    • 不是用0结尾的字符串代替范围吗?所以嵌入零的处理方式不同......无论如何通常都是合适的。
    • @Deduplicator 添加了使用 make_itearator_range 的版本
    猜你喜欢
    • 2017-11-26
    • 2013-03-14
    • 1970-01-01
    • 1970-01-01
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 2017-10-26
    • 1970-01-01
    相关资源
    最近更新 更多