C ++中的类成为容器的资格是什么? [英] What are the qualifications for a class in C++ to become a container?

查看:184
本文介绍了C ++中的类成为容器的资格是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚接触C ++程序,遇到了 containers 这个术语,例如向量 c $ c> deque , map 等。



要求在C ++中 class 应该满足要调用容器



范围只有两个方法 - begin和end。它们都返回相同类型的迭代器(注意:有提案允许end返回一个Sentinel)。



迭代器被假定为读者理解。 / p>

高质量范围还可以暴露空,大小,前面,后面和操作符[](如果特别是随机访问)。



对于<:code> for(:)循环,您可以通过作为原始C来限定 Range 数组,具有 begin() end()方法,或在同一命名空间中有自由函数,将你的类型作为一个参数(并返回迭代器的东西)。在这篇文章中,标准中唯一使用范围的是<:code> for(:)循环。可以说,这个答案是C ++中概念范围的唯一实用定义。


$ b 输出范围通常不称为容器)拥有其元素。顺序和关联容器是不同的野兽,并且都在标准中定义。



标准中的容器有一组typedef - 值类型,迭代器,常量迭代器。大多数也有分配器(除数组)。它们是空的,大多数都有大小(除了forward_list)。



容器可以通过2个输入或前向迭代器构造为兼容的值类型,



顺序容器有推送和emplace回(除了转发列表)(和一些有emplace / push前面),插入和emplace在迭代器)。



关联容器具有键类型。它们中的许多是容器的对。存储的数据通常是部分const(数据的键部分,无论是键还是 set s的情况下的整个字段)。他们有插入和有无提示的地方 - 他们管理自己的顺序。它们还有 .find .count 方法。



目前在std库中没有依赖于Container-ness的函数。并且有一个积极的建议,使容器和范围被正式化为一个概念在C + + 17。 Container的实际技术定义是在标准中,以防您需要完全创建实际容器;然而,通常你真的需要一个范围与编辑它和所有权力学的手段。



在添加了Ranges-v3之后,Range和Container的概念将是代码中存在的实际事物,并且可能存在依赖于这些特征的算法。在此之前,他们是特别的概念比任何东西。


I'm new to C++ programming and came across the term containers with examples such as vector, deque, map, etc.

What should be the minimum requirements that a class should fulfill to be called a container in C++?

解决方案

I will start with the concept Range.

Range has only two methods -- begin and end. They both return iterators of the same type (note: there are proposals to permit end to return a Sentinel instead).

Iterators are presumed to be understood by the reader.

A high quality Range can also expose empty, size, front, back, and operator [] (if random access especially).

For a for(:) loop, you can qualify as a Range by being a raw C array, having begin() and end() methods, or having free functions in the same namespace as your type that take your type as one argument (and return iterator-like things). As of this post, the only thing in the standard that consumes Ranges is for(:) loops. One could argue that this answer is the only practical definition of the concept Range in C++.


Next, Container.

A Container is a Range of at least forward iterators (input and output Ranges are usually not called Containers) that owns its elements. Sequential and Associative containers are different beasts, and both are defined in the standard.

Containers in the standard have a set of typedefs -- value type, iterator, const iterator. Most also have allocator (except array). They have empty, and most have size (except forward_list).

Containers can all be constructed by 2 input or forward iterators to a compatible value type, and from an initializer list.

Sequential containers have push and emplace back (except forward list) (and some have emplace/push front), and insert and emplace at iterator (or after for forward list).

Associative containers have a key type. Many of them are containers of pairs. The data stored is usually partially const (the "key" part of the data, be it the key or the entire field in the case of sets). They have insert and emplace with and without hints -- they manage their own order. They also have a .find and .count methods.

There are currently no functions in the std library that depend on Container-ness. And there is an active proposal to make Container-ness and Range-ness be formalized as a concept in C++17. The actual technical definition of Container is in the standard in case you need to create an actual container exactly; however, usually you really need a Range with a means to edit it and ownership mechanics. The Container concept is, in my experience, mostly there to make specifying behaviour easier in the standard.

After something like Ranges-v3 is added, the concepts of Range and Container will be actual things that exist in code, and there may be algorithms that depend on exactly those features. Prior to that, they are ad-hoc concepts more than anything.

这篇关于C ++中的类成为容器的资格是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆