提示:文章
文章目录
- 前言
- 一、背景
- 二、
- 2.1 处理代码
- 2.1 问题探讨
- 总结
前言
前期疑问:
本文目标:
一、背景
今天写了个代码,就是将pb文件中的某个字段的数值取出来,做差值计算。但是这个字段在很长的字段中,需要先截取存在的长字段,再从长字段中将这个字段再截取出来存在vector中,再单独做计算。
然后这个代码我之前是写过一次的,但是在今天再写又记不得了。再次整理一下
二、
2.1 处理代码
下面的就是之前写的代码,逻辑清晰,代码简洁,但是我想不起来怎么写的了,写了下面的代码
而这次写的代码就复杂很多,不够简洁,逻辑不是很简洁,如下:
void SplitTxtStr3()
{std::string txtStr = "timePeriod\":\"17:00 - 17:30\",\"relativelyTimeStamp\":[""{\"theLastStopPointLeaveTimestamp\":1730192750000,\"theLastStopPointToEndPointDistance\":20,\"timeWithDis\":1730192747929,\"theLastStopPointStayTime\":9000,\"checkRate\":19224363.888888888},""{\"theLastStopPointLeaveTimestamp\":1725354891000,\"theLastStopPointToEndPointDistance\":9.3,\"timeWithDis\":1725354889491,\"timeDistanceCmpLast\":4837858438,\"theLastStopPointStayTime\":10000,\"checkRate\":19170609.9},""{\"theLastStopPointLeaveTimestamp\":1725354193000,\"theLastStopPointToEndPointDistance\":16.3,\"timeWithDis\":1725354191449,\"timeDistanceCmpLast\":698042,\"theLastStopPointStayTime\":13000,\"checkRate\":19170602.144444443},""{\"theLastStopPointLeaveTimestamp\":1724750804000,\"theLastStopPointToEndPointDistance\":29.4,\"timeWithDis\":1724750800607,\"timeDistanceCmpLast\":603390842,\"theLastStopPointStayTime\":18000,\"checkRate\":19163897.822222222},";size_t startPos = 0;size_t endPos = 0;std::vector<std::string> strVec;while (true) {std::string keyWordStart = "{";startPos = txtStr.find(keyWordStart, endPos);std::string keyWordEnd = "}";endPos = txtStr.find(keyWordEnd, startPos);if (startPos != std::string::npos && endPos != std::string::npos) {std::string targetStr = txtStr.substr(startPos + 1, endPos - startPos);strVec.push_back(targetStr);} else {break;}}
}
之前逻辑清晰的代码如下:
void GetTimeWithDis::SplitTxtStr2()
{std::string txtStr = "timePeriod\":\"17:00 - 17:30\",\"relativelyTimeStamp\":[""{\"theLastStopPointLeaveTimestamp\":1730192750000,\"theLastStopPointToEndPointDistance\":20,\"timeWithDis\":1730192747929,\"theLastStopPointStayTime\":9000,\"checkRate\":19224363.888888888},""{\"theLastStopPointLeaveTimestamp\":1725354891000,\"theLastStopPointToEndPointDistance\":9.3,\"timeWithDis\":1725354889491,\"timeDistanceCmpLast\":4837858438,\"theLastStopPointStayTime\":10000,\"checkRate\":19170609.9},""{\"theLastStopPointLeaveTimestamp\":1725354193000,\"theLastStopPointToEndPointDistance\":16.3,\"timeWithDis\":1725354191449,\"timeDistanceCmpLast\":698042,\"theLastStopPointStayTime\":13000,\"checkRate\":19170602.144444443},""{\"theLastStopPointLeaveTimestamp\":1724750804000,\"theLastStopPointToEndPointDistance\":29.4,\"timeWithDis\":1724750800607,\"timeDistanceCmpLast\":603390842,\"theLastStopPointStayTime\":18000,\"checkRate\":19163897.822222222},";std::string keyWordStart = "{";std::string keyWordEnd = "}";size_t startPos = 0;std::vector<std::string> strVec;while ((startPos = txtStr.find(keyWordStart, startPos)) != std::string::npos) {int strLength = txtStr.find(keyWordEnd, startPos) - startPos;strVec.push_back(txtStr.substr(startPos + 1, strLength));startPos += strLength;}int d = 0;
}
2.1 问题探讨
这个就涉及到一个问题,就是怎么样简化变量,只需要使用一个startPos变量就可以完成处理过程,我设计了两个变量。并且while循环设计的也不是很好。
上述处理过程涉及到两个点,一个是find函数的用法。find函数第二个参数是查找起始位置。不写参数默认是0。
第二个是subStr函数的用法,两个参数分别是裁剪起始位置和终止位置。
总结
未完待续