从列表中删除特定格式的字符串

Remove specific format of string from a List?

本文关键字:格式 字符串 定格 列表 删除      更新时间:2023-10-16

我正在为Arduino编写一个程序,该程序以类型的NMEA格式获取信息,该格式从存储在List&lt中的.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;
}
(演示)