字符串和指针位置

Strings and Pointer locations

本文关键字:位置 指针 字符串      更新时间:2023-10-16

我正在开发一个程序,该程序需要筛选一个充斥着 HTML/XML 垃圾.txt文件,以查找末尾有一个数字的特定模式。此模式应出现 10 次。模式如下:"<p class="wx-temp"> 93." 93 是一个温度读数,我最终试图收获什么,但是,我找不到一种方法将 93 与字符串的其余部分隔离开来,因为它会随着程序理想运行的每一天而变化。我一直在尝试找到一种方法来定义一个不能常量的整数数据类型,(即我不能在字符串末尾输入 93,因为它会破坏目的)并将其放在字符串或类似的东西中,我可以在模式结束后设置为 X 个字符开始, 换句话说,指针位置。对不起,漫无边际。有人可以帮助我吗?

假设您已将整个文件加载到单个字符串中,这并非不合理。

string html;
//(Some code that reads into a big string)

现在您只需要查找该标签。

string delimiter( "<p class="wx-temp">" );
vector<int> temperatures;
size_t pos = html.find_first_of(delimiter);
while( pos != string::npos ) 
{
    // Skip past the tag (to the temperature)
    pos += delimiter.size();
    if( pos >= html.size() ) break;
    // Extract it (C-style) and chuck it into the vector.
    int temperature = atoi( html.c_str() + pos );
    temperatures.push_back(temperature);
    // If you want to stop after the first 10:
    if( temperatures.size() == 10 ) break; 
    // Find the next tag
    pos = html.find_first_of(delimiter, pos);
}