栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

继承Java集合接口(Set,Map,List等)的C ++等效项是什么?还是扩展AbstractCollection?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

继承Java集合接口(Set,Map,List等)的C ++等效项是什么?还是扩展AbstractCollection?

简短的答案是:没有等效项,因为C ++的处理方式有所不同。

毫无疑问,这就是事实。如果您不喜欢这样,请使用其他语言。

长答案是:有一个等效项,但这会让您有些不满意,因为尽管Java的容器和算法模型很大程度上基于继承,而C 却并非如此。C

的模型很大程度上基于通用迭代器。

举个例子,您要实现一个集合。无视事实,C
++已经有

std::set
std::multiset
std::unordered_set
std::unordered_multiset

,这些都是定制不同的比较器和分配器,以及无序那些有定制的散列函数,当然。

因此,假设您要重新实现

std::set
。例如,您可能是计算机科学专业的学生,​​并且想要比较AVL树,2-3棵树,红黑树和八角树。

你会怎么做?你会这样写:

template<class Key, class Compare = std::less<Key>, class Allocator = std::allocator<Key>> class set {    using key_type = Key;    using value_type = Key;    using size_type = std::size_t;    using difference_type = std::ptrdiff_t;    using key_compare = Compare;    using value_compare = Compare;    using allocator_type = Allocator;    using reference = value_type&;    using const_reference = const value_type&;    using pointer = std::allocator_traits<Allocator>::pointer;    using const_pointer = std::allocator_traits<Allocator>::const_pointer;    using iterator = ;    using const_iterator = ;    using reverse_iterator = std::reverse_iterator<iterator>;    using const_reverse_iterator = std::reverse_iterator<const_iterator>    iterator begin() const;    iterator end() const;    const_iterator cbegin() const;    const_iterator cend() const;    reverse_iterator rbegin() const;    reverse_iterator rend() const;    const_reverse_iterator crbegin() const;    const_reverse_iterator crend() const;    bool empty() const;    size_type size() const;    size_type max_size() const;    void clear();    std::pair<iterator, bool> insert(const value_type& value);    std::pair<iterator, bool> insert(value_type&& value);    iterator insert(const_iterator hint, const value_type& value);    iterator insert(const_iterator hint, value_type&& value);    template <typename InputIterator>    void insert(InputIterator first, InputIterator last);    void insert(std::initializer_list<value_type> ilist);    template <class ...Args>    std::pair<iterator, bool> emplace(Args&&... args);    void erase(iterator pos);    iterator erase(const_iterator pos);    void erase(iterator first, iterator last);    iterator erase(const_iterator first, const_iterator last);    size_type erase(const key_type& key);    void swap(set& other);    size_type count(const Key& key) const;    iterator find(const Key& key);    const_iterator find(const Key& key) const;    std::pair<iterator, iterator> equal_range(const Key& key);    std::pair<const_iterator, const_iterator> equal_range(const Key& key) const;    iterator lower_bound(const Key& key);    const_iterator lower_bound(const Key& key) const;    iterator upper_bound(const Key& key);    const_iterator upper_bound(const Key& key) const;    key_compare key_comp() const;    value_compare value_comp() const;}; // offtopic: don't forget the ; if you've come from Java!template<class Key, class Compare, class Alloc>void swap(set<Key,Compare,Alloc>& lhs,set<Key,Compare,Alloc>& rhs);template <class Key, class Compare, class Alloc>bool operator==(const set<Key,Compare,Alloc>& lhs,     const set<Key,Compare,Alloc>& rhs);template <class Key, class Compare, class Alloc>bool operator!=(const set<Key,Compare,Alloc>& lhs,     const set<Key,Compare,Alloc>& rhs);template <class Key, class Compare, class Alloc>bool operator<(const set<Key,Compare,Alloc>& lhs,    const set<Key,Compare,Alloc>& rhs);template <class Key, class Compare, class Alloc>bool operator<=(const set<Key,Compare,Alloc>& lhs,     const set<Key,Compare,Alloc>& rhs);template <class Key, class Compare, class Alloc>bool operator>(const set<Key,Compare,Alloc>& lhs,    const set<Key,Compare,Alloc>& rhs);template <class Key, class Compare, class Alloc>bool operator>=(const set<Key,Compare,Alloc>& lhs,     const set<Key,Compare,Alloc>& rhs);

当然,您不必全部编写这些代码,特别是如果您只是编写一些代码来测试其中的一部分。但是,如果您编写了所有这些内容(为了清楚起见,我将其排除在外),那么您将拥有的是功能全面的set类。那个固定类有什么特别之处?

您可以在任何地方使用它。与a搭配使用的任何物品

std::set
都可以与您的套装搭配使用。不必为此专门编程。它不需要任何东西。任何适用于任何set类型的东西都应该适用于它。Boost的任何算法都可以在场景上使用。

您编写的用于集合的任何算法都可以在集合上使用,并可以增强集合和许多其他集合。但不仅限于现场。如果他们写得不错,他们将在支持特定迭代器类型的任何容器上工作。如果他们需要随机访问,他们将需要RandomAccessIterators,它

std::vector
提供了,但是
std::list
没有。如果他们需要BidirectionalIterators,那么
std::vector
and
std::list
(和其他)将可以正常工作,但
std::forward_list
不会。

迭代器/算法/容器的工作原理非常好。考虑在C ++中将文件读取为字符串的清洁度:

using namespace std;ifstream file("file.txt");string file_contents(istreambuf_iterator<char>(file),          istreambuf_iterator<char>{});


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/454060.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号