首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >包装unordered_map以构建不可修改(不可变)的映射

包装unordered_map以构建不可修改(不可变)的映射
EN

Stack Overflow用户
提问于 2013-02-15 22:28:55
回答 1查看 1.1K关注 0票数 2

我正在尝试容器类型,更深入地了解细节,并尝试构建一个不可修改(或不可变)的映射。

为此,我构建了某种类似复制构造函数的方法,将另一个unordered_map中的所有元素添加到我的UnmodifiableMap中(它基本上是std::unordered_map的包装器)。为了使其不可修改,我只提供了常量迭代器和只读方法。但是我被构造器卡住了,我肯定我遗漏了什么,也许有人能指出问题所在。也许这是一种完全错误的方式,但他的方式是目前为止尝试过的:

代码语言:javascript
复制
template <typename Key, typename T,
    typename HashFcn = std::hash<Key>,
    typename EqualKey = std::equal_to<Key>,
    typename Alloc = std::allocator<std::pair<const Key, T> > > 
class UnmodifiableMap {
public:
    // The actual data
    typedef std::unordered_map<T, Key, HashFcn, EqualKey> base;

    typedef Key key_type;
    typedef T data_type;
    typedef T mapped_type;
    typedef std::pair<const key_type, data_type> value_type;
    typedef HashFcn hasher;
    typedef EqualKey key_equal;
    typedef Alloc allocator_type;

    typedef typename base::size_type size_type;
    typedef typename base::const_iterator const_iterator;
    typedef typename base::iterator iterator;

private:
    base _map;

    /**
     * Constructs an empty unordered_map 
     */
    UnmodifiableMap(
        size_type n = 0, const hasher& hf = hasher(),
        const key_equal& eql = key_equal(),
        const allocator_type &alloc = allocator_type())
            : _map(n, hf, eql, alloc) {}

public:
    /** Constructs a copy of unordered_map */
    UnmodifiableMap(const base& other)
    : _map(static_cast<const base&>(other)) {}

    ~UnmodifiableMap() {}   

    iterator begin() { return _map.begin(); }
    iterator end() { return _map.end(); }
    const_iterator begin() const { return _map.begin(); }
    const_iterator end() const { return _map.end(); }

    bool empty() const { return _map.empty(); }

    bool contains(const key_type& key) const { 
        return _map.find(key) != _map.end(); }
};

这里是主体:

代码语言:javascript
复制
int main(int argc, char **argv) {
    typedef std::unordered_map<int, std::string> Items;
    Items map;

    map[1] = "first string";
    map[4] = "string 4";
    map[5] = "string 5";
    map[22] = "string 22";
    map[12] = "string 12";
    map[18] = "string 18";

    typedef UnmodifiableMap<int, std::string> ReadOnlyItems;
    ReadOnlyItems readonlymap(map);

    return 0;
}

我得到的错误是

代码语言:javascript
复制
Unmodifiable_map.cpp: In function ‘int main(int, char**)’:
Unmodifiable_map.cpp:56:25: error: no matching function for call to ‘UnmodifiableMap<int, std::basic_string<char> >::UnmodifiableMap(Items&)’
Unmodifiable_map.cpp:56:25: note: candidates are:
Unmodifiable_map.h:45:2: note: UnmodifiableMap<Key, T, HashFcn, EqualKey, Alloc>::UnmodifiableMap(const base&) [with Key = int, T = std::basic_string<char>, HashFcn = std::hash<int>, EqualKey = std::equal_to<int>, Alloc = std::allocator<std::pair<const int, std::basic_string<char> > >, UnmodifiableMap<Key, T, HashFcn, EqualKey, Alloc>::base = std::unordered_map<std::basic_string<char>, int, std::hash<int>, std::equal_to<int>, std::allocator<std::pair<const std::basic_string<char>, int> > >]
Unmodifiable_map.h:45:2: note:   no known conversion for argument 1 from ‘Items {aka std::unordered_map<int, std::basic_string<char> >}’ to ‘const base& {aka const std::unordered_map<std::basic_string<char>, int, std::hash<int>, std::equal_to<int>, std::allocator<std::pair<const std::basic_string<char>, int> > >&}’
Unmodifiable_map.h:37:2: note: UnmodifiableMap<Key, T, HashFcn, EqualKey, Alloc>::UnmodifiableMap(UnmodifiableMap<Key, T, HashFcn, EqualKey, Alloc>::size_type, const hasher&, const key_equal&, const allocator_type&) [with Key = int, T = std::basic_string<char>, HashFcn = std::hash<int>, EqualKey = std::equal_to<int>, Alloc = std::allocator<std::pair<const int, std::basic_string<char> > >, UnmodifiableMap<Key, T, HashFcn, EqualKey, Alloc>::size_type = long unsigned int, UnmodifiableMap<Key, T, HashFcn, EqualKey, Alloc>::hasher = std::hash<int>, UnmodifiableMap<Key, T, HashFcn, EqualKey, Alloc>::key_equal = std::equal_to<int>, UnmodifiableMap<Key, T, HashFcn, EqualKey, Alloc>::allocator_type = std::allocator<std::pair<const int, std::basic_string<char> > >]
Unmodifiable_map.h:37:2: note:   no known conversion for argument 1 from ‘Items {aka std::unordered_map<int, std::basic_string<char> >}’ to ‘long unsigned int’
Unmodifiable_map.h:14:7: note: UnmodifiableMap<int, std::basic_string<char> >::UnmodifiableMap(const UnmodifiableMap<int, std::basic_string<char> >&)
Unmodifiable_map.h:14:7: note:   no known conversion for argument 1 from ‘Items {aka std::unordered_map<int, std::basic_string<char> >}’ to ‘const UnmodifiableMap<int, std::basic_string<char> >&’
Unmodifiable_map.h:14:7: note: UnmodifiableMap<int, std::basic_string<char> >::UnmodifiableMap(UnmodifiableMap<int, std::basic_string<char> >&&)
Unmodifiable_map.h:14:7: note:   no known conversion for argument 1 from ‘Items {aka std::unordered_map<int, std::basic_string<char> >}’ to ‘UnmodifiableMap<int, std::basic_string<char> >&&’

希望有人能对此有所了解。另外,我认为我需要在复制构造函数中做更多的工作,可能需要复制元素(像swap函数)和operator=的实现?!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-15 22:42:06

您在包装器类中的base类型定义中交换了TKey的角色:

代码语言:javascript
复制
template <typename Key, typename T,
    typename HashFcn = std::hash<Key>,
    typename EqualKey = std::equal_to<Key>,
    typename Alloc = std::allocator<std::pair<const Key, T> > > 
class UnmodifiableMap {
public:
    // typedef std::unordered_map<T, Key, HashFcn, EqualKey> base; // ERROR
    typedef std::unordered_map<Key, T, HashFcn, EqualKey> base; // OK
    ...

因此,底层的映射最终是一个unordered_map<string, int>而不是unordered_map<int, string>,编译器会抱怨不匹配。

还要注意,您的构造函数中有一个不必要的static_cast

代码语言:javascript
复制
UnmodifiableMap(const base& other)
// : _map(static_cast<const base&>(other)) {} // NOT NEEDED. Just do:
   : _map(other) {}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14896841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档