需要帮助调试“属性解析器”!来自HackerRank的C ++问题

问题描述 投票:1回答:1

试图解决中级难度问题之一。这是新手代码。我基本上是尝试为一种修饰标记语言编写一个属性解析器,在这种语言中,您应该通过查询来获取标签的属性值。

我尝试实现一个嵌套的哈希表(unordered_map),用于将标签名称映射到其属性键值对。 (tagName->(attrName,attrValue))

https://www.hackerrank.com/challenges/attribute-parser/problem?isFullScreen=true链接到问题^

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>

#include <string>
#include <unordered_map>
#include <array>
#include <sstream>
using namespace std;

string reverse(string str, int begin, int end) {
    str = str.substr(begin, end - begin);

    string res;
    for (int i = str.length() - 1; i >= 0; --i) {
        res += str[i];
    }
    return res;
}

int main() {
    // HASH TABLE
    unordered_map<string, unordered_map<string, string>> umap;
    unordered_map<string, string> imap;

    // N, Q
    int n, q;
    cin >> n >> q;

    // parse and save: tag name, attr name, attr value
    for (int i = 0; i < n/2; ++i) {
        // parse input string
        string input;
        getline(cin, input);
        stringstream ssin(input);
        array<string, 4> vals;
        // <tagName ; attrName ; = ; attrValue
        // parse 4 clauses 
        int j = 0;
        while (j < vals.size()) {
            ssin >> vals[j];
            ++j;
        }
        // preprocess input clauses: (1)tagName ; (2)attrName ; (3)attrValue
        string tagName = vals[0].substr(1);
        string attrName = vals[1];
        string temp_attrValue = vals[3].substr(1);
        temp_attrValue = reverse(temp_attrValue, 1, temp_attrValue.length());
        string attrValue = reverse(temp_attrValue, 2, temp_attrValue.length());

        // add preprocessed clauses to hash-table
        imap.insert(pair<string, string>(attrName, attrValue));
        umap.insert(pair<string, unordered_map<string, string>>(tagName, imap));
    }
    // loop through rest of source code
    for (int i = 0; i < n/2; ++i) {
        string input;
        getline(cin, input);
    }

    // queries
    for (int i = 0; i < q; ++i) {
        //preprocess clauses: (1)tagName ; (2)attrName
        string query;
        getline(cin, query);
        stringstream ss(query);
        string segment;
        vector<string> segList;
        while (getline(ss, segment, '~')) {
            segList.push_back(segment);
        }

        // condition if tagName is a nested subtag
        short res = 0;
        for (int i = 0; i < segList[0].length(); ++i) {
            if (segList[0][i] == '.') {
                ++res;
            }
        }
        // QUERIED VALUES
        string queryTagName = segList[0];
        string queryAttrName = segList[1];

        // if there exists '.', parse tagName from ID
        if (res > 0) {
            string ID = segList[0];
            stringstream ss(ID);
            string seg;
            vector<string> segs;
            while (getline(ss, seg, '.')) {
                segs.push_back(seg);
            }
            queryTagName = segs[segs.size() - 1];
        }

        // OUTPUT ==============
        unordered_map<string, unordered_map<string, string>>::iterator u_itr = umap.find(queryTagName);
        if (umap.find(queryTagName) == umap.end()) {
            cout << "Not Found!" << endl;
        } else {
            if (imap.find(queryAttrName) == imap.end()) {
                cout << "Not Found!" << endl;
            } else {
                cout << u_itr->second.find(queryAttrName)->second << endl;
            }
        }
    }

    return 0;
}

我一直收到“超出范围”错误。有人知道这是怎么回事吗?

c++ c parsing debugging question-answering
1个回答
0
投票

由于string tagName = vals[0].substr(1);为空,因此vals出现错误。要验证这一点,请将string tagName = vals[0].substr(1);替换为string tagName = vals[0].substr(0);,您将不会遇到相同的错误。

© www.soinside.com 2019 - 2024. All rights reserved.