gpt4 book ai didi

c++ - 在子字符串上拆分

转载 作者:可可西里 更新时间:2023-11-01 18:16:38 25 4
gpt4 key购买 nike

如何以简单的方式基于另一个子字符串拆分字符串?

例如拆分为“\r\n”

message1\r\nmessage2 

=>

message1
message2

据我所知,boost::tokenizer 和 boost::split 仅对单个字符起作用。

编辑:

我知道我可以通过使用 std::string::find 和 std::string::substr 并有一个循环等来做到这一点......但这不是我所说的“简单”的意思。

最佳答案

尽管 boost::split 确实采用了一个对字符进行操作的谓词,但还是有一个提升 string algorithm可以拆分子字符串:

#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <boost/algorithm/string/iter_find.hpp>
#include <boost/algorithm/string/finder.hpp>
int main()
{
std::string input = "message1foomessage2foomessage3";

std::vector<std::string> v;
iter_split(v, input, boost::algorithm::first_finder("foo"));

copy(v.begin(), v.end(), std::ostream_iterator<std::string>(std::cout, " "));
std::cout << '\n';
}

关于c++ - 在子字符串上拆分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3739280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com