gpt4 book ai didi

c++ - C++ 中的数据 block 构建逻辑

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

好吧,这对某些人来说可能很简单,但到目前为止,我无法为以下数据 block 构建找出任何强大的逻辑。

  1. 我有一大块 char* 或 std::string 数据,大小为 1505 字节
  2. 我需要将这些数据切割成每个最大 500 字节的大小
  3. 每条数据都应该以特定的字符串集开头

请看下面的数据 block (即 1505 字节的完整数据集)在这里我需要将这些数据切割成 500 字节的 block (例如,我将把它放入一个 vector 元素中)并且每个元素文本应该以 [SampleSet] 开头。因此有可能在一个 vector 元素中我得到两个或更多 SampleSet 但在其他 vector 元素中我只得到一个 [SampleSet]。

任何线索我该如何开始?

[SampleSet]
SampleTime=2014-08-13 T12:02:04
Data=Type:DataType|Value:0.11|Alert:false|Lead:II
Data=Type:DataType|Value:65|Alert:false
Data=Type:DataType|Value:95|Alert:false
Data=Type:DataType|Value:97.8|Alert:false|Channel:1
Data=Type:DataType|Value:90|Alert:false
Data=Type:DataType|Value:0.10|Alert:false|Lead:I
Data=Type:DataType|Value:0.20|Alert:true|Lead:II

[SampleSet]
SampleTime=2014-08-13 T12:05:09
Data=Type:DataType|Value:82|Alert:false
Data=Type:DataType|Value:98|Alert:true
Data=Type:DataType|Value:97.8|Alert:false|Channel:1
Data=Type:DataType|Value:97.8|Alert:false|Channel:1
Data=Type:DataType|Value:97.2|Alert:false|Channel:2
Data=Type:DataType|Value:31|Alert:false
Data=Type:DataType|Value:120|Alert:true
Data=Type:DataType|Value:95|Alert:true
Data=Type:DataType|Value:90|Alert:false
Data=Type:DataType|Value:0.10|Alert:false|Lead:I
Data=Type:DataType|Value:0.20|Alert:true|Lead:II

[SampleSet]
SampleTime=2014-08-13 T12:05:20
Data=Type:DataType|Value:82|Alert:false
Data=Type:DataType|Value:98|Alert:true
Data=Type:DataType|Value:31|Alert:false
Data=Type:DataType|Value:120|Alert:true
Data=Type:DataType|Value:95|Alert:true
Data=Type:DataType|Value:90|Alert:false
Data=Type:DataType|Value:0.10|Alert:false|Lead:I
Data=Type:DataType|Value:0.20|Alert:true|Lead:II

[SampleSet]
SampleTime=2014-08-13 T12:15:11
Data=Type:DataType|Value:82|Alert:false
Data=Type:DataType|Value:0.10|Alert:false|Lead:I
Data=Type:DataType|Value:0.20|Alert:true|Lead:II

最佳答案

最直观的方法可能是使用 std::string::find查找输入字符串中所有 header (="[SampleSet]") 的位置。然后你可以将这个输入字符串切割成标记,每个标记只包含一个样本集。然后,您可以使用这些标记来构建 500 字节最大块。但是,如果您的样本集通常比 block 大小小得多,那么您也会经常调用 find

因此,更好的方法是使用 std::string::rfind开始向后搜索 500 字节 block 边界右侧的 [SampleSet]-header。 IE。要找到第一个 block ,请开始向后搜索位置 500 处的 header [SampleSet]。您从中获得的位置(例如 467)是第一个 block 的右边界。要找到下一个 block ,请从 467+500 开始向后搜索,依此类推。我希望方法是明确的。这是一个示例实现:

std::string input; // your input data
std::vector<std::string> chunks;
std::string header("[SampleSet]");
std::string::size_type chunksize = 500; // size including header
std::string::size_type posL, posR = 0, posEnd = input.length()+1;

while(posR != posEnd)
{
posL = posR;
if (posEnd-posL <= chunksize)
posR = posEnd;
else
posR = input.rfind(header, posL+chunksize);

if (posR-posL)
chunks.push_back(input.substr(posL, posR-posL));
else
{
std::string toobigsample(input.substr(posL, input.find(header,posL+1)-posL));
std::cerr << "Error: SampleSet to big to store in one chunk ("
<< toobigsample.length() << " bytes):" << std::endl << std::endl
<< toobigsample;
break;
}
}

关于c++ - C++ 中的数据 block 构建逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29864994/

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