gpt4 book ai didi

c++ - 获取多行的字符串

转载 作者:行者123 更新时间:2023-11-28 03:41:01 25 4
gpt4 key购买 nike

这里我有一个字符串,它被拆分并且每一行都在一个 vector 中的不同单元格中,如果前三个字母是 .N/ 我想检索所有行但是看起来我只能检索下面以 .N/ 开头的行之一是我正在使用的字符串。

std::string message = ".N/1TLIS/PART/123456789I/A/1234RFGH67323\n"
".N/AT0931/2DEC/GVA/Y\n"
".I/KL0967/02APR/AMS/F\n"
".O/123/MARRIOTT/27MAY/084512L//FEDEXVAN45\n";

我现在使用的代码

std::vector<std::string> el; //VECTOR
split(el,message,boost::is_any_of("\n"));// the string above is split line for line into vector el

for(int i = 0; i < el.size(); i++)
{
if(el[i].substr(0,3) == ".N/")
{

str = el[i].substr(3);

}
}
cout << str;

但是,当我打印出 str 时,我只会得到 "1TLIS/PART/123456789I/A/1234RFGH67323"而不是 1TLIS/PART/123456789I/A/1234RFGH67323 和“AT0931/2DEC/GVA/Y”

有没有一种方法可以检索以特定字符开头的所有行?

最佳答案

另一种使用算法的替代解决方案:

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

using namespace std;

ostream_iterator<string> out_it(cout, "\n");

remove_copy_if(el.begin(), el.end(), out_it,
!boost::bind(boost::algorithm::starts_with<string, string>, _1, ".N/"));

关于c++ - 获取多行的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9258017/

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