C++ “需要帮助调试”;属性分析器";!C++;哈克朗克的问题
试图解决一个中等难度的问题。这是新手代码。 我基本上尝试为madeup标记语言编写一个属性解析器,您应该通过查询来检索标记的属性值 我尝试实现一个嵌套的哈希表(无序的_映射),用于将标记的名称映射到它的属性键值对。(标记名->(属性名,属性值)) 与问题的联系^C++ “需要帮助调试”;属性分析器";!C++;哈克朗克的问题,c++,c,parsing,debugging,question-answering,C++,C,Parsing,Debugging,Question Answering,试图解决一个中等难度的问题。这是新手代码。 我基本上尝试为madeup标记语言编写一个属性解析器,您应该通过查询来检索标记的属性值 我尝试实现一个嵌套的哈希表(无序的_映射),用于将标记的名称映射到它的属性键值对。(标记名->(属性名,属性值)) 与问题的联系^ #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorit
#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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
字符串反转(字符串str、int-begin、int-end){
str=str.substr(开始,结束-开始);
字符串res;
对于(int i=str.length()-1;i>=0;--i){
res+=str[i];
}
返回res;
}
int main(){
//哈希表
无序地图umap;
无序地图imap;
//N,Q
int n,q;
cin>>n>>q;
//解析并保存:标记名、属性名、属性值
对于(int i=0;i VAL[j];
++j;
}
//预处理输入子句:(1)标记名;(2)属性名;(3)属性值
字符串标记名=VAL[0]。substr(1);
字符串attrName=vals[1];
字符串temp_attrValue=vals[3]。substr(1);
temp_attrValue=反向(temp_attrValue,1,temp_attrValue.length());
字符串attrValue=reverse(temp_attrValue,2,temp_attrValue.length());
//将预处理的子句添加到哈希表
insert(对(attrName,attrValue));
umap.insert(成对(标记名,imap));
}
//循环浏览其余的源代码
对于(int i=0;i0){
字符串ID=segList[0];
串流ss(ID);
字符串seg;
向量分段;
while(getline(ss,seg,'.')){
segs.向后推(seg);
}
queryTagName=segs[segs.size()-1];
}
//输出==============
无序映射::迭代器u_itr=umap.find(queryTagName);
if(umap.find(queryTagName)==umap.end()){
cout您在字符串标记名=vals[0]处出错。substr(1);
因为您的vals
为空。
若要验证此错误,请将string tagName=vals[0].substr(1);
替换为string tagName=vals[0].substr(0);
,您将不会收到相同的错误。您在string tagName=vals[0].substr(1);
处收到错误,因为vals
为空。
若要验证此错误,请将string tagName=vals[0].substr(1);
替换为string tagName=vals[0].substr(0);
,您将不会收到相同的错误。您的代码可能因getline而出现问题。请尝试cin。忽略(100),\n');在执行getline之前。它将删除有问题的空白字符。您的代码可能因为getline而出现问题。请在执行getline之前尝试cin.ignore(100,“\n”);它将删除有问题的空白字符。