You need to enable JavaScript to run this app. C++如何按指定条件检测字符串、提取内容并实现去重输出
优惠活动
大模型
产品
解决方案
定价
更多

C++如何按指定条件检测字符串、提取内容并实现去重输出

实现方案

核心修改逻辑

  • 使用std::unordered_set<std::string>存储已经输出过的函数名,利用集合元素唯一的特性自动完成去重
  • 对包含bl和<的行,定位<和>的下标位置,截取二者中间的函数名称
  • 仅当提取到的函数名未在集合中存在时,执行输出操作,并将该名称存入集合避免后续重复输出

修改后完整代码

#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
// 假设你已有readFile的实现,这里不再重复编写

using namespace std;

class CallFunction {
    private:
        vector<string> content;
    public:
        CallFunction(vector<string> content) {
            this->content = content;
        }
        void print() {
            // 存储已输出的函数名,自动去重
            unordered_set<string> existedFuncs;
            for(string line: content) {
                if(line.find("bl") != string::npos && line.find("<") != string::npos) {
                    size_t leftPos = line.find('<');
                    size_t rightPos = line.find('>', leftPos);
                    if(rightPos == string::npos) {
                        continue;
                    }
                    // 截取<和>中间的函数名
                    string funcName = line.substr(leftPos + 1, rightPos - leftPos - 1);
                    // 仅未出现过的函数名才输出
                    if(existedFuncs.find(funcName) == existedFuncs.end()) {
                        cout << funcName << endl;
                        existedFuncs.insert(funcName);
                    }
                }
            }
        }
};

int main() {
    string fileName = "libndkmod.s";
    vector<string> content = readFile(fileName);
    CallFunction cf = CallFunction(content);
    cf.print();
    return 0;
}

效果验证

针对你给出的输入片段,运行修改后的代码只会输出一次memcpy@plt,符合需求。

内容的提问来源于stack exchange,提问作者deppensido

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.18 16:24:03