从列表中删除特定格式的字符串
Remove specific format of string from a List?
我正在为Arduino编写一个程序,该程序以类型的NMEA格式获取信息,该格式从存储在List<中的.txt文件中读取;字符串>。我需要去掉以某些前缀开头的字符串($GPZDA, $GPGSA, $GPGSV),因为这些对我来说是无用的,因此我只需要$GPRMC和$GPGGA,其中包含一个基本的时间戳和位置,这是我所使用的。我希望使用尽可能少的外部库(SPRINT, BOOST),因为DUE没有足够的空间。
我真正需要的是一个方法来删除行从列表<<em>STRING>,不以特定的前缀开始,有什么想法吗?
我目前使用的方法似乎已经将整个输出替换为一个特定的字符串,但保持文件长度/大小相同(分别为1676和2270),这些输出是使用两个While语句实现的,将两个输入文件放入List<<em> string >
下面是一个小剪掉我想使用,它应该排序文件到一个正确的顺序(工作,他们当前命令的数值,它适用于第二个字段的字符串)然而".unique();"似乎每个"独特"的值,用它取代其他列表,所以现在我有一个1676行基本上1,1,1、2、2、2、3、3、4……1676 ? ?
while (std::getline(GPS1,STRLINE1)){
ListOne.push_back("GPS1: " + STRLINE1 + "n");
ListOne.sort();
ListOne.unique();
std::cout << ListOne.back() << std::endl;
GPSO1 << ListOne.back();
}
谢谢
如果我理解正确,你想有某种形式的前缀白名单。您可以使用remove_if
来查找它们,并使用一个小函数来检查其中一个前缀是否合适(使用mismatch
,如这里),例如:
#include <iostream>
#include <algorithm>
#include <string>
#include <list>
using namespace std;
int main() {
list<string> l = {"aab", "aac", "abb", "123", "aaw", "wws"};
list<string> whiteList = {"aa", "ab"};
auto end = remove_if(l.begin(), l.end(), [&whiteList](string item)
{
for(auto &s : whiteList)
{
auto res = mismatch(s.begin(), s.end(), item.begin());
if (res.first == s.end()){
return false; //found allowed prefix
}
}
return true;
});
for (auto it = l.begin(); it != end; ++it){
cout<< *it << endl;
}
return 0;
}
(演示)相关文章:
- 是否可以从格式字符串中检索"width"
- 我应该如何从 stdin C++ 中读取可变长度的格式字符串?
- 从用户定义的类生成格式字符串?
- 如何连接格式字符串?
- 如何在C++中将"%Y%m%d"格式字符串转换为time_t变量?
- CPPCHECK C++成员函数上的格式字符串
- scanf() 语句中"%*[^n]"的格式字符串指示什么?分配抑制器 (*) 和否定扫描集 ([^) 如何协同工作?
- 自定义 {fmt} 格式化函数,具有编译时格式字符串检查功能
- '|' scanf 格式字符串是什么意思
- 如何使用 msgfmt 验证格式字符串中的位置表示法占位符
- sscanf_s:格式字符串"%d"需要类型为"int *"的参数,但可变参数 4 的类型为"WORD *"
- C4477: 'fprintf' :格式字符串 '%s' 需要类型为"char *"的参数,但可变参数 1 的类型为"int *"
- 如何使用OpenSSL API从其PEM格式字符串中读取RSA公钥
- C 格式字符串宏
- C++等效于 C 的格式字符串
- 如何使用 C++ 中的 rapidjson 库从 json 格式字符串中获取值
- _vsnprintf和_vsnprintf_s的格式字符串中的可用百分比字符
- 如何将格式字符串发送到iostream
- 跨平台格式字符串
- 使用 swprintf() 打印"0x0"的格式字符串应该是什么?