string_alg简明用法(2)

首次用markdown写篇文章

本篇主要有如下字符串的操作:

  • 修剪
  • 查找
  • 替换/删除
  • 分割
  • 合并
  • 查找(分割)迭代器

每个函数基本有四个版本:
原版表示大小写敏感,在原串上操作。
前缀有i的表示大小写不敏感。
后缀有copy的表示不在原串上操作,返回操作后的结果。
后缀有if的表示可以增加判断条件谓词。

1.修剪

主要就是 trim 搭配 copyif 后缀,可选left,right

代码如下:

void Trim_Use()
{
    using namespace boost;
    format fmt("\|%s\|\n");
    std::string str("  some text  ");
    std::cout << fmt % trim_copy(str);
    std::cout << fmt % trim_right_copy(str);

    trim_left(str);
    std::cout << fmt % str;

    std::string str2("123 some text !!");
    std::cout << fmt % trim_left_copy_if(str2, is_digit());
    std::cout << fmt % trim_right_copy_if(str2,is_punct());
    std::cout << fmt % trim_copy_if(str2,is_punct() ||is_space() || is_digit());
}

输入结果:

|some text|
|  some text|
|some text  |
| some text !!|
|123 some text |
|some text|

2.查找

主要是find搭配 firstnthlastheadtail
查找的结果存一个iterator_range的迭代器中,包含了字符串信息和位置信息。
示例代码如下:

void Find_Use()
{
    using namespace boost;
    format fmt("\|%s\|. pos = %d \n");
    std::string str("Long long ago, there was a king.");

    iterator_range<std::string::iterator> rge;
    rge = find_first(str, "long");
    std::cout << fmt % rge % (rge.begin() - str.begin());

    rge = ifind_first(str, "long");
    std::cout << fmt % rge % (rge.begin() - str.begin());

    rge =find_nth(str, "ng",2);
    std::cout << fmt % rge % (rge.begin() - str.begin());

    rge =find_head(str,4);
    std::cout << fmt % rge % (rge.begin() - str.begin());

    rge =find_tail(str,5);
    std::cout << fmt % rge % (rge.begin() - str.begin());

    rge =find_first(str,"notexist");
    assert(rge.empty() && !rge);
    //TODO
    //find
    //find_token
    //find_regex
}

运行结果:

|long|. pos = 5 
|Long|. pos = 0 
|ng|. pos = 29 
|Long|. pos = 0 
|king.|. pos = 27 

3.替换/删除

主要使用replace/erase搭配firstnthlastallheadtail
同样都有copy后缀版本。
前8个有i前缀版本。
一个有20个。(2*(4*2+2))
示例代码:

void Replace_Delete_Use()
{
    using namespace boost;
    std::string str("There are some text and text.\n");

    std::cout << replace_first_copy(str, "This", "this");

    replace_last(str, "text", "string");
    std::cout << str;

    replace_tail(str,8, "text.\n");
    std::cout << str;

    std::cout << ierase_all_copy(str, "t");
    std::cout << replace_nth_copy(str,"t",3,"T");
    std::cout << erase_tail_copy(str, 8) << std::endl;
}

输出结果:

There are some text and text.
There are some text and string.
There are some text and text.
here are some ex and ex.
There are some text and texT.
There are some text an

4.分割

示例代码:

void Split_Use()
{
    using namespace boost;
    std::string str("Red,Green.Yellow::Black-White+Yellow");
    std::deque<std::string> d;
    ifind_all(d, str, "yELLOW");
    assert(d.size() == 2);
    for (BOOST_AUTO(pos, d.begin()); pos != d.end(); ++pos)
    {
        std::cout << "[" << *pos << "] ";
    }
    std::cout << std::endl;

    std::list<iterator_range<std::string::iterator> > ls;
    split(ls, str, is_any_of(",.:-+"));

    for (BOOST_AUTO(pos, ls.begin()); pos != ls.end(); ++pos)
    {
        std::cout << "[" << *pos << "] ";
    }
    std::cout << std::endl;

    ls.clear();
    split(ls, str, is_any_of(".:-"),token_compress_on);

    for (BOOST_AUTO(pos, ls.begin()); pos != ls.end(); ++pos)
    {
        std::cout << "[" << *pos << "] ";
    }
    std::cout << std::endl;
}

输出结果:

[Yellow] [Yellow] 
[Red] [Green] [Yellow] [] [Black] [White] [Yellow] 
[Red,Green] [Yellow] [Black] [White+Yellow] 

5.合并

示例代码:

void Merger_Use()
{
    using namespace boost::assign;
    std::vector<std::string> v = list_of("Red")("Yellow")("Black")("Green");
    std::cout << boost::join(v, "+") << std::endl;;

    struct is_contains_e
    {
        bool operator()(const std::string & x)
        {
            return boost::contains(x, "e");
        }
    };

    std::cout << boost::join_if(v, "**", is_contains_e()) << std::endl;;
}

输出结果:

Red+Yellow+Black+Green
Red**Yellow**Green

6.查找(分割)迭代器

示例代码:

void Find_Spilt_Iterator()
{
    using namespace boost;
    std::string str("Red||red||yellow||black");
    typedef find_iterator<std::string::iterator> string_find_iterator;
    string_find_iterator pos, end;
    pos = make_find_iterator(str,first_finder("red", is_iequal()));
    for (; pos !=end; ++pos)
    {
        std::cout << "[" << *pos << "] ";
    }
    std::cout << std::endl;

    typedef split_iterator<std::string::iterator> string_split_iterator;

    string_split_iterator p, endp;
    p = make_split_iterator(str, first_finder("||", is_iequal()));
    for (; p != endp; ++p)
    {
        std::cout << "[" << *p << "] ";
    }
    std::cout << std::endl;

}

输出结果:

[Red] [red] 
[Red] [red] [yellow] [black] 

总结

时间仓促,只来得急帖代码。有空再来补充说明。
关于头文件。
format需要#include <boost/format.hpp>
BOOST_AUTO需要#include <boost/typeof/typeof.hpp>
list_of需要#include <boost/assign.hpp>
其余的都是STL里需要包含的。

这一系类函数可以完成绝大多数对字符串的处理了。

数据驱动的两阶段分布鲁棒(1-范数和∞-范数约束)的电热综合能源系统研究(Matlab代码实现)内容概要:本文围绕“数据驱动的两阶段分布鲁棒(1-范数和∞-范数约束)的电热综合能源系统研究”展开,提出了一种结合数据驱动与分布鲁棒优化方法的建模框架,用于解决电热综合能源系统在不确定性环境下的优化调度问题。研究采用两阶段优化结构,第一阶段进行预决策,第二阶段根据实际场景进行调整,通过引入1-范数和∞-范数约束来构建不确定集,有效刻画风电、负荷等不确定性变量的波动特性,提升模型的鲁棒性和实用性。文中提供了完整的Matlab代码实现,便于读者复现和验证算法性能,并结合具体案例分析了不同约束条件下系统运行的经济性与可靠性。; 适合人群:具备一定电力系统、优化理论和Matlab编程基础的研究生、科研人员及工程技术人员,尤其适合从事综合能源系统、鲁棒优化、不确定性建模等相关领域研究的专业人士。; 使用场景及目标:①掌握数据驱动的分布鲁棒优化方法在综合能源系统中的应用;②理解1-范数和∞-范数在构建不确定集中的作用与差异;③学习两阶段鲁棒优化模型的建模思路与Matlab实现技巧,用于科研复现、论文写作或工程项目建模。; 阅读建议:建议读者结合提供的Matlab代码逐段理解算法实现细节,重点关注不确定集构建、两阶段模型结构设计及求解器调用方式,同时可尝试更换数据或调整约束参数以加深对模型鲁棒性的理解。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值