boost::split库将字符串按照指定的分隔符进行分割

分隔:boost::split

boost::split 是 Boost 库中的一个函数,用于将字符串按照指定的分隔符进行分割,并将分割得到的子字符串存储到一个容器中。它的作用是将一个字符串拆分成多个子字符串,方便进行处理和操作。

你需要包含 <boost/algorithm/string.hpp> 头文件,并使用命名空间 boost::algorithm

这是 boost::split 的用法示例:

#include <iostream>
#include <vector>
#include <string>
#include <boost/algorithm/string.hpp>

int main() {
    std::string str = "Hello,World,How,Are,You";
    std::vector<std::string> result;
    boost::split(result, str, boost::is_any_of(","));

    for (const auto& substr : result) {
        std::cout << substr << std::endl;
    }

    return 0;
}

以上代码中,我们首先定义了一个以逗号分隔的字符串 str。然后定义一个 std::vector<std::string> 类型的容器 result,用于存储分割得到的子字符串。接下来调用 boost::split 函数,将 str 按照逗号进行分割,并将分割结果存储到 result 中。

最后,我们使用循环遍历 result 中的每个子字符串,并输出到标准输出。输出结果为:

Hello
World
How
Are
You

可以看到,boost::split 函数成功将原始字符串按照逗号进行了分割,将分割得到的子字符串存储到了 result 容器中。

替换 :boost::algorithm::replace_all

替换本省的依稀符号存储到本省字符串中,即把本省的一些字符串替换掉

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

int main() {
    std::string input = "Hello, World!";
    std::string search = "o";
    std::string replace = "0";

    boost::algorithm::replace_all(input, search, replace);

    std::cout << "Modified string: " << input << std::endl;

    return 0;
}

结果  Modified string: Hell0, W0rld!
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值