简短的回答是:没有等价物,因为 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 = /* depends on your implementation */;
using const_iterator = /* depends on your implementation */;
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 类有什么特别之处?
您可以在任何地方使用它。任何适用于std::set 的东西都适用于您的设置。它不必专门为它编程。它不需要任何东西。任何适用于任何集合类型的东西都应该适用于它。 Boost 的任何算法都适用于集合。
您编写的用于集合的任何算法都将适用于您的集合、boost 的集合以及许多其他集合。但不仅仅是在片场。如果它们写得很好,它们将适用于任何支持特定类型迭代器的容器。如果他们需要随机访问,他们将需要 RandomAccessIterators,std::vector 提供,但 std::list 没有。如果他们需要双向迭代器,那么 std::vector 和 std::list(以及其他)可以正常工作,但 std::forward_list 不会。
迭代器/算法/容器的东西真的很好用。考虑在 C++ 中将文件读入字符串的简洁性:
using namespace std;
ifstream file("file.txt");
string file_contents(istreambuf_iterator<char>(file),
istreambuf_iterator<char>{});