明白!🚀
你要我 繼續擴展,在這套 C++98 代碼里加一個功能:
根據完整的5位ID,反查出對應的路徑。
OK,我直接接著上面那版來,給你補充 getPathFromId 方法,并且保持整體風格統一,簡潔、高效!
?
? 完整加上 getPathFromId 的 STDTMDirTree
#include
#include
#include
#include
#include
namespace STDTM {
class STDTMDirTree {
private:
struct Node {
std::map<std::string, Node*> children;
std::string idPart;
};
Node* root;
static const size_t TOTAL_DIGITS = 5;void clear(Node* node) {std::map<std::string, Node*>::iterator it;for (it = node->children.begin(); it != node->children.end(); ++it) {clear(it->second);}delete node;
}std::vector<std::string> splitPath(const std::string& path) const {std::vector<std::string> parts;std::string part;for (size_t i = 0; i < path.length(); ++i) {if (path[i] == '/') {if (!part.empty()) {parts.push_back(part);part.clear();}} else {part += path[i];}}if (!part.empty()) {parts.push_back(part);}return parts;
}void buildIdString(const std::vector<std::string>& idParts, char result[TOTAL_DIGITS + 1]) const {size_t pos = 0;for (size_t i = 0; i < idParts.size(); ++i) {const std::string& part = idParts[i];for (size_t j = 0; j < part.size() && pos < TOTAL_DIGITS; ++j) {result[pos++] = part[j];}}while (pos < TOTAL_DIGITS) {result[pos++] = '0';}result[TOTAL_DIGITS] = '\0';
}bool findPathRecursive(Node* node, const char* id, size_t pos, std::string currentPath, std::string& foundPath) const {if (pos >= TOTAL_DIGITS) {foundPath = currentPath;return true;}std::map<std::string, Node*>::const_iterator it;for (it = node->children.begin(); it != node->children.end(); ++it) {Node* child = it->second;const std::string& part = child->idPart;if (pos + part.size() > TOTAL_DIGITS) {continue; // 超出,總位數不允許}bool match = true;for (size_t i = 0; i < part.size(); ++i) {if (id[pos + i] != part[i]) {match = false;break;}}if (match) {std::string newPath = currentPath + "/" + it->first;if (findPathRecursive(child, id, pos + part.size(), newPath, foundPath)) {return true;}}}return false;
}
public:
STDTMDirTree() {
root = new Node();
}
~STDTMDirTree() {clear(root);
}void insertPathWithIds(const std::string& path, const std::vector<std::string>& idParts) {Node* current = root;std::vector<std::string> parts = splitPath(path);if (parts.size() != idParts.size()) {throw std::string("Error: Path and idParts size mismatch");}for (size_t i = 0; i < parts.size(); ++i) {const std::string& name = parts[i];if (current->children.count(name) == 0) {Node* newNode = new Node();newNode->idPart = idParts[i];current->children[name] = newNode;}current = current->children[name];}
}std::string getIdFromPath(const std::string& path) const {Node* current = root;std::vector<std::string> parts = splitPath(path);std::vector<std::string> idParts;for (size_t i = 0; i < parts.size(); ++i) {const std::string& name = parts[i];if (current->children.count(name) == 0) {return "[Path not found]";}current = current->children.find(name)->second;idParts.push_back(current->idPart);}char result[TOTAL_DIGITS + 1];buildIdString(idParts, result);return std::string(result);
}std::string getPathFromId(const std::string& id) const {if (id.size() != TOTAL_DIGITS) {return "[Invalid ID]";}std::string foundPath;if (findPathRecursive(root, id.c_str(), 0, "", foundPath)) {return foundPath;} else {return "[Path not found]";}
}
};
} // namespace STDTM
?
🧠 說明
? findPathRecursive :從樹根出發,按ID去匹配,一層層往下走。
? 每次檢查節點的idPart,必須能對應上 id 數組的相應位置。
? 如果能一路匹配到5位數字填滿,就找到路徑了!
?
📦 使用示例
STDTM::STDTMDirTree tree;
tree.insertPathWithIds(“/robot/arm1”, std::vectorstd::string{“1”, “234”});
tree.insertPathWithIds(“/station/slot3”, std::vectorstd::string{“23”, “456”});
std::cout << tree.getIdFromPath(“/robot/arm1”) << std::endl; // 12340
std::cout << tree.getIdFromPath(“/station/slot3”) << std::endl; // 23456
std::cout << tree.getPathFromId(“12340”) << std::endl; // /robot/arm1
std::cout << tree.getPathFromId(“23456”) << std::endl; // /station/slot3
?
要不要我順便再教你一招,怎么加速查詢?比如維護一個id到路徑的哈希表,瞬間查出路徑,速度還能提一檔。要的話告訴我:「繼續加速版本!」🚀
要不要?😎