Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

有一个关于 HashMap Milestone 4: Implement Special Member Functions 的问题想请教 #7

Open
Andy-xiaokang opened this issue Jul 29, 2024 · 0 comments

Comments

@Andy-xiaokang
Copy link

template <typename K, typename M, typename H>
HashMap<K, M, H>::HashMap(HashMap&& other) :
    _size(std::move(other._size)),
    _hash_function(std::move(other._hash_function)),
    _buckets_array(std::move(other._buckets_array)) {   // 这里在 move constructor time test 那里会报错
    other._buckets_array = {other._buckets_array.size(), nullptr}
}

就是为什么不能直接对 other._buckets_array 进行 std::move() 右值转换的操作
而是

// move constructor
template <typename K, typename M, typename H>
HashMap<K, M, H>::HashMap(HashMap&& other) :
    _size(std::move(other._size)),
    _hash_function(std::move(other._hash_function)),
    _buckets_array(other._buckets_array.size(), nullptr) {
    for (size_t i = 0; i < other._buckets_array.size(); i++) {
        _buckets_array[i] = std::move(other._buckets_array[i]);
        other._buckets_array[i] = nullptr;
    }
    other._size = 0;
}

遍历后挨个转换其中的 node* 元素

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant