标签:
#include "map" #include "iostream" #include "string" using namespace std; int main() { int lineNum, varNum; cin >> lineNum >> varNum; string inputStr, outputStr, tempStr; map<string, string> varDict; getchar(); // consume next new line character for(int i=0; i<lineNum; i++) { getline(cin, tempStr); inputStr += tempStr + ‘\n‘; } //cout << inputStr; string varKey, varValue; for(int i=0; i<varNum; i++) { cin >> varKey; getline(cin, tempStr); int startQuote = tempStr.find("\""); int endQuote = tempStr.rfind("\""); varValue = tempStr.substr(startQuote+1, endQuote-startQuote-1); //cout << value; varDict[varKey] = varValue; } int startPos, endPos; // startPos->{{ endPos->}} int startPoint = 0; // start position for string.find while(true) { startPos = inputStr.find("{{", startPoint); endPos = inputStr.find("}}", startPos); if(startPos<0 || endPos <0) break; varKey = inputStr.substr(startPos+3, endPos-startPos-4); varValue = ""; if(varDict.find(varKey) != varDict.end()) varValue = varDict[varKey]; outputStr += inputStr.substr(startPoint, startPos-startPoint) + varValue; startPoint = endPos + 2; } outputStr += inputStr.substr(startPoint, inputStr.length()); cout << outputStr; }
标签:
原文地址:http://www.cnblogs.com/meelo/p/5823504.html