字典(dictionary)的设计

1. 简单接口

struct Dict{
    bool has(const string& key);
    void insert(const string& key, const string& value);
    string get(const string& key);
    void erase(const string& key);
};

2. 实现

struct Dict{
    vector<string> keys, values;

    bool has(const string& key){
        return find(keys.begin(), keys.end(), key) != keys.end();
    }

    void insert(const string& key, const string& value){
        if (has(key)) erase(key);
        keys.push_back(key);
        values.push_back(value);
    }
};
原文地址:https://www.cnblogs.com/mtcnn/p/9423787.html